From 8e4fde5ddd47534284539e91eb60c5708fc8c512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Sun, 7 May 2023 16:49:23 +0200 Subject: [PATCH] feat(json-schema-2020-12): add support for const keyword (#8622) Refs #8513 --- .../components/JSONSchema/JSONSchema.jsx | 2 ++ .../components/keywords/Const.jsx | 30 +++++++++++++++++++ .../components/keywords/_all.scss | 12 ++++++++ src/core/plugins/json-schema-2020-12/fn.js | 18 ++++++++++- src/core/plugins/json-schema-2020-12/hoc.jsx | 4 +++ src/core/plugins/json-schema-2020-12/index.js | 2 ++ .../plugins/oas31/wrap-components/models.jsx | 2 ++ 7 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/core/plugins/json-schema-2020-12/components/keywords/Const.jsx diff --git a/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx b/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx index 02f28353..ba0f6621 100644 --- a/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx +++ b/src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx @@ -63,6 +63,7 @@ const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => { "KeywordUnevaluatedProperties" ) const KeywordType = useComponent("KeywordType") + const KeywordConst = useComponent("KeywordConst") const KeywordFormat = useComponent("KeywordFormat") const KeywordTitle = useComponent("KeywordTitle") const KeywordDescription = useComponent("KeywordDescription") @@ -157,6 +158,7 @@ const JSONSchema = forwardRef(({ schema, name, onExpand }, ref) => { )} + diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/Const.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/Const.jsx new file mode 100644 index 00000000..50f73c43 --- /dev/null +++ b/src/core/plugins/json-schema-2020-12/components/keywords/Const.jsx @@ -0,0 +1,30 @@ +/** + * @prettier + */ +import React from "react" + +import { schema } from "../../prop-types" +import { useFn } from "../../hooks" + +const Const = ({ schema }) => { + const fn = useFn() + + if (!fn.hasKeyword(schema, "const")) return null + + return ( +
+ + Const + + + {fn.stringify(schema.const)} + +
+ ) +} + +Const.propTypes = { + schema: schema.isRequired, +} + +export default Const diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/_all.scss b/src/core/plugins/json-schema-2020-12/components/keywords/_all.scss index 54d8ef68..4ef2d233 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/_all.scss +++ b/src/core/plugins/json-schema-2020-12/components/keywords/_all.scss @@ -41,6 +41,18 @@ color: #6b6b6b; font-style: italic; } + + &--const { + @include text_code(); + color: #6b6b6b; + font-style: normal; + display: inline-block; + margin-left: 10px; + line-height: 1.5; + padding: 1px 4px 1px 4px; + border: 1px dashed #6b6b6b; + border-radius: 4px; + } } } .json-schema-2020-12-keyword__name--secondary + .json-schema-2020-12-keyword__value--secondary::before { diff --git a/src/core/plugins/json-schema-2020-12/fn.js b/src/core/plugins/json-schema-2020-12/fn.js index 20dcf7fd..178d4400 100644 --- a/src/core/plugins/json-schema-2020-12/fn.js +++ b/src/core/plugins/json-schema-2020-12/fn.js @@ -159,6 +159,22 @@ export const isExpandable = (schema) => { fn.hasKeyword(schema, "propertyNames") || fn.hasKeyword(schema, "unevaluatedItems") || fn.hasKeyword(schema, "unevaluatedProperties") || - schema?.description + schema?.description || + fn.hasKeyword(schema, "const") ) } + +export const stringify = (value) => { + if ( + value === null || + ["number", "bigint", "boolean"].includes(typeof value) + ) { + return String(value) + } + + if (Array.isArray(value)) { + return `[${value.map(stringify).join(", ")}]` + } + + return JSON.stringify(value) +} diff --git a/src/core/plugins/json-schema-2020-12/hoc.jsx b/src/core/plugins/json-schema-2020-12/hoc.jsx index 365d3e3f..f5ac3723 100644 --- a/src/core/plugins/json-schema-2020-12/hoc.jsx +++ b/src/core/plugins/json-schema-2020-12/hoc.jsx @@ -31,6 +31,7 @@ import KeywordPropertyNames from "./components/keywords/PropertyNames" import KeywordUnevaluatedItems from "./components/keywords/UnevaluatedItems" import KeywordUnevaluatedProperties from "./components/keywords/UnevaluatedProperties" import KeywordType from "./components/keywords/Type/Type" +import KeywordConst from "./components/keywords/Const" import KeywordFormat from "./components/keywords/Format/Format" import KeywordTitle from "./components/keywords/Title/Title" import KeywordDescription from "./components/keywords/Description/Description" @@ -45,6 +46,7 @@ import { getType, hasKeyword, isExpandable, + stringify, } from "./fn" export const withJSONSchemaContext = (Component, overrides = {}) => { @@ -78,6 +80,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => { KeywordUnevaluatedItems, KeywordUnevaluatedProperties, KeywordType, + KeywordConst, KeywordFormat, KeywordTitle, KeywordDescription, @@ -106,6 +109,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => { isBooleanJSONSchema, hasKeyword, isExpandable, + stringify, ...overrides.fn, }, } diff --git a/src/core/plugins/json-schema-2020-12/index.js b/src/core/plugins/json-schema-2020-12/index.js index 593c0fab..c7cc795d 100644 --- a/src/core/plugins/json-schema-2020-12/index.js +++ b/src/core/plugins/json-schema-2020-12/index.js @@ -29,6 +29,7 @@ import KeywordPropertyNames from "./components/keywords/PropertyNames" import KeywordUnevaluatedItems from "./components/keywords/UnevaluatedItems" import KeywordUnevaluatedProperties from "./components/keywords/UnevaluatedProperties" import KeywordType from "./components/keywords/Type/Type" +import KeywordConst from "./components/keywords/Const" import KeywordFormat from "./components/keywords/Format/Format" import KeywordTitle from "./components/keywords/Title/Title" import KeywordDescription from "./components/keywords/Description/Description" @@ -68,6 +69,7 @@ const JSONSchema202012Plugin = () => ({ JSONSchema202012KeywordUnevaluatedItems: KeywordUnevaluatedItems, JSONSchema202012KeywordUnevaluatedProperties: KeywordUnevaluatedProperties, JSONSchema202012KeywordType: KeywordType, + JSONSchema202012KeywordConst: KeywordConst, JSONSchema202012KeywordFormat: KeywordFormat, JSONSchema202012KeywordTitle: KeywordTitle, JSONSchema202012KeywordDescription: KeywordDescription, diff --git a/src/core/plugins/oas31/wrap-components/models.jsx b/src/core/plugins/oas31/wrap-components/models.jsx index 7b1647ba..0890a99c 100644 --- a/src/core/plugins/oas31/wrap-components/models.jsx +++ b/src/core/plugins/oas31/wrap-components/models.jsx @@ -56,6 +56,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => { "JSONSchema202012KeywordUnevaluatedProperties" ) const KeywordType = getComponent("JSONSchema202012KeywordType") + const KeywordConst = getComponent("JSONSchema202012KeywordConst") const KeywordFormat = getComponent("JSONSchema202012KeywordFormat") const KeywordTitle = getComponent("JSONSchema202012KeywordTitle") const KeywordDescription = getComponent( @@ -101,6 +102,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => { KeywordUnevaluatedItems, KeywordUnevaluatedProperties, KeywordType, + KeywordConst, KeywordFormat, KeywordTitle, KeywordDescription,