From ff2d4b23111ab068bf79a2732de996d93f7b5195 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Fri, 21 Apr 2023 08:30:14 +0200 Subject: [PATCH] feat(json-schema-2020-12): add support for anyOf keyword Refs #8513 --- .../components/JSONSchema/JSONSchema.jsx | 2 + .../json-schema-2020-12/components/_all.scss | 1 + .../components/keywords/AnyOf/AnyOf.jsx | 56 +++++++++++++++++++ .../components/keywords/AnyOf/_any-of.scss | 19 +++++++ src/core/plugins/json-schema-2020-12/fn.js | 1 + src/core/plugins/json-schema-2020-12/hoc.jsx | 2 + src/core/plugins/json-schema-2020-12/index.js | 2 + .../plugins/oas31/wrap-components/models.jsx | 2 + 8 files changed, 85 insertions(+) create mode 100644 src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/AnyOf.jsx create mode 100644 src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/_any-of.scss 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 4c3c043b..18a71f6e 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 @@ -42,6 +42,7 @@ const JSONSchema = ({ schema, name }) => { const Keyword$defs = useComponent("Keyword$defs") const Keyword$comment = useComponent("Keyword$comment") const KeywordAllOf = useComponent("KeywordAllOf") + const KeywordAnyOf = useComponent("KeywordAnyOf") const KeywordProperties = useComponent("KeywordProperties") const KeywordType = useComponent("KeywordType") const KeywordFormat = useComponent("KeywordFormat") @@ -107,6 +108,7 @@ const JSONSchema = ({ schema, name }) => { )} + diff --git a/src/core/plugins/json-schema-2020-12/components/_all.scss b/src/core/plugins/json-schema-2020-12/components/_all.scss index 355fa5e7..d7f31f6e 100644 --- a/src/core/plugins/json-schema-2020-12/components/_all.scss +++ b/src/core/plugins/json-schema-2020-12/components/_all.scss @@ -9,6 +9,7 @@ @import './keywords/$vocabulary/$vocabulary'; @import './keywords/$defs/$defs'; @import './keywords/AllOf/all-of'; +@import './keywords/AnyOf/any-of'; @import './keywords/Type/type'; @import './keywords/Format/format'; @import './keywords/Description/description'; diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/AnyOf.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/AnyOf.jsx new file mode 100644 index 00000000..e9287612 --- /dev/null +++ b/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/AnyOf.jsx @@ -0,0 +1,56 @@ +/** + * @prettier + */ +import React, { useCallback, useState } from "react" + +import { schema } from "../../../prop-types" +import { useFn, useComponent, useIsExpandedDeeply } from "../../../hooks" + +const AnyOf = ({ schema }) => { + const anyOf = schema?.anyOf || [] + + if (!Array.isArray(anyOf) || anyOf.length === 0) { + return null + } + + const fn = useFn() + const isExpandedDeeply = useIsExpandedDeeply() + const [expanded, setExpanded] = useState(isExpandedDeeply) + const Accordion = useComponent("Accordion") + const JSONSchema = useComponent("JSONSchema") + + const handleExpansion = useCallback(() => { + setExpanded((prev) => !prev) + }, []) + + return ( +
+ + + AnyOf + + + {fn.getType({ anyOf })} + + + {expanded && ( +
    + {anyOf.map((schema, index) => ( +
  • + +
  • + ))} +
+ )} +
+ ) +} + +AnyOf.propTypes = { + schema: schema.isRequired, +} + +export default AnyOf diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/_any-of.scss b/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/_any-of.scss new file mode 100644 index 00000000..dfcf4664 --- /dev/null +++ b/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf/_any-of.scss @@ -0,0 +1,19 @@ +.json-schema-2020-12 { + &__anyOf { + margin: 5px 0 5px 0; + + & > ul { + @include expansion-border; + padding: 0; + } + } + + &-core-keyword { + &--anyOf { + color: $text-code-default-font-color; + font-style: normal; + } + } +} + + diff --git a/src/core/plugins/json-schema-2020-12/fn.js b/src/core/plugins/json-schema-2020-12/fn.js index 1e2887d2..b9935ea7 100644 --- a/src/core/plugins/json-schema-2020-12/fn.js +++ b/src/core/plugins/json-schema-2020-12/fn.js @@ -123,6 +123,7 @@ export const isExpandable = (schema) => { schema?.$defs || schema?.$comment || schema?.allOf || + schema?.anyOf || schema?.description || schema?.properties ) diff --git a/src/core/plugins/json-schema-2020-12/hoc.jsx b/src/core/plugins/json-schema-2020-12/hoc.jsx index d922ba4a..c5a96f49 100644 --- a/src/core/plugins/json-schema-2020-12/hoc.jsx +++ b/src/core/plugins/json-schema-2020-12/hoc.jsx @@ -14,6 +14,7 @@ import Keyword$dynamicRef from "./components/keywords/$dynamicRef" import Keyword$defs from "./components/keywords/$defs/$defs" import Keyword$comment from "./components/keywords/$comment" import KeywordAllOf from "./components/keywords/AllOf/AllOf" +import KeywordAnyOf from "./components/keywords/AnyOf/AnyOf" import KeywordProperties from "./components/keywords/Properties/Properties" import KeywordType from "./components/keywords/Type/Type" import KeywordFormat from "./components/keywords/Format/Format" @@ -45,6 +46,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => { Keyword$defs, Keyword$comment, KeywordAllOf, + KeywordAnyOf, KeywordProperties, KeywordType, KeywordFormat, diff --git a/src/core/plugins/json-schema-2020-12/index.js b/src/core/plugins/json-schema-2020-12/index.js index d20b4cde..928aa8b2 100644 --- a/src/core/plugins/json-schema-2020-12/index.js +++ b/src/core/plugins/json-schema-2020-12/index.js @@ -13,6 +13,7 @@ import Keyword$dynamicRef from "./components/keywords/$dynamicRef" import Keyword$defs from "./components/keywords/$defs/$defs" import Keyword$comment from "./components/keywords/$comment" import KeywordAllOf from "./components/keywords/AllOf/AllOf" +import KeywordAnyOf from "./components/keywords/AnyOf/AnyOf" import KeywordType from "./components/keywords/Type/Type" import KeywordFormat from "./components/keywords/Format/Format" import KeywordTitle from "./components/keywords/Title/Title" @@ -36,6 +37,7 @@ const JSONSchema202012Plugin = () => ({ JSONSchema202012Keyword$defs: Keyword$defs, JSONSchema202012Keyword$comment: Keyword$comment, JSONSchema202012KeywordAllOf: KeywordAllOf, + JSONSchema202012KeywordAnyOf: KeywordAnyOf, JSONSchema202012KeywordProperties: KeywordProperties, JSONSchema202012KeywordType: KeywordType, JSONSchema202012KeywordFormat: KeywordFormat, diff --git a/src/core/plugins/oas31/wrap-components/models.jsx b/src/core/plugins/oas31/wrap-components/models.jsx index c14eb3a7..7be04f7d 100644 --- a/src/core/plugins/oas31/wrap-components/models.jsx +++ b/src/core/plugins/oas31/wrap-components/models.jsx @@ -21,6 +21,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => { const Keyword$defs = getComponent("JSONSchema202012Keyword$defs") const Keyword$comment = getComponent("JSONSchema202012Keyword$comment") const KeywordAllOf = getComponent("JSONSchema202012KeywordAllOf") + const KeywordAnyOf = getComponent("JSONSchema202012KeywordAnyOf") const KeywordProperties = getComponent("JSONSchema202012KeywordProperties") const KeywordType = getComponent("JSONSchema202012KeywordType") const KeywordFormat = getComponent("JSONSchema202012KeywordFormat") @@ -50,6 +51,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => { Keyword$defs, Keyword$comment, KeywordAllOf, + KeywordAnyOf, KeywordProperties, KeywordType, KeywordFormat,