From 4ea28a931008a021b4ab6216e51e0cc56c3a35d1 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Thu, 20 Apr 2023 15:04:46 +0200 Subject: [PATCH] feat(json-schema-2020-12): add support for allOf keyword Refs #8513 --- .../components/JSONSchema/JSONSchema.jsx | 6 +- .../json-schema-2020-12/components/_all.scss | 1 + .../components/keywords/AllOf/AllOf.jsx | 56 +++++++++++++++++++ .../components/keywords/AllOf/_all-of.scss | 18 ++++++ 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, 87 insertions(+), 1 deletion(-) create mode 100644 src/core/plugins/json-schema-2020-12/components/keywords/AllOf/AllOf.jsx create mode 100644 src/core/plugins/json-schema-2020-12/components/keywords/AllOf/_all-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 ec807685..114fdb19 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 @@ -41,6 +41,7 @@ const JSONSchema = ({ schema, name }) => { const Keyword$dynamicRef = useComponent("Keyword$dynamicRef") const Keyword$defs = useComponent("Keyword$defs") const Keyword$comment = useComponent("Keyword$comment") + const KeywordAllOf = useComponent("KeywordAllOf") const KeywordProperties = useComponent("KeywordProperties") const KeywordType = useComponent("KeywordType") const KeywordFormat = useComponent("KeywordFormat") @@ -104,14 +105,17 @@ const JSONSchema = ({ schema, name }) => { {!isCircular && isExpandable && ( )} + + {!isCircular && isExpandable && ( + + )} - )} 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 6b1489ed..6a43cb62 100644 --- a/src/core/plugins/json-schema-2020-12/components/_all.scss +++ b/src/core/plugins/json-schema-2020-12/components/_all.scss @@ -3,6 +3,7 @@ @import './ExpandDeepButton/expand-deep-button'; @import './keywords/$vocabulary/$vocabulary'; @import './keywords/$defs/$defs'; +@import './keywords/AllOf/all-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/AllOf/AllOf.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/AllOf/AllOf.jsx new file mode 100644 index 00000000..b6cdcedb --- /dev/null +++ b/src/core/plugins/json-schema-2020-12/components/keywords/AllOf/AllOf.jsx @@ -0,0 +1,56 @@ +/** + * @prettier + */ +import React, { useCallback, useState } from "react" + +import { schema } from "../../../prop-types" +import { useFn, useComponent, useIsExpandedDeeply } from "../../../hooks" + +const AllOf = ({ schema }) => { + const allOf = schema?.allOf || [] + + if (!Array.isArray(allOf) || allOf.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 ( +
+ + + AllOf + + + {fn.getType({ allOf })} + + + {expanded && ( +
    + {allOf.map((schema, index) => ( +
  • + +
  • + ))} +
+ )} +
+ ) +} + +AllOf.propTypes = { + schema: schema.isRequired, +} + +export default AllOf diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/AllOf/_all-of.scss b/src/core/plugins/json-schema-2020-12/components/keywords/AllOf/_all-of.scss new file mode 100644 index 00000000..aae4c3f9 --- /dev/null +++ b/src/core/plugins/json-schema-2020-12/components/keywords/AllOf/_all-of.scss @@ -0,0 +1,18 @@ +.json-schema-2020-12 { + &__allOf { + & ul { + padding: 0; + margin: 0 0 0 20px; + border-left: 1px dashed rgba($section-models-model-container-background-color, 0.1); + } + } + + &-core-keyword { + &--allOf { + 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 e107a05e..1e2887d2 100644 --- a/src/core/plugins/json-schema-2020-12/fn.js +++ b/src/core/plugins/json-schema-2020-12/fn.js @@ -122,6 +122,7 @@ export const isExpandable = (schema) => { schema?.$dynamicRef || schema?.$defs || schema?.$comment || + schema?.allOf || 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 c180d969..d922ba4a 100644 --- a/src/core/plugins/json-schema-2020-12/hoc.jsx +++ b/src/core/plugins/json-schema-2020-12/hoc.jsx @@ -13,6 +13,7 @@ import Keyword$ref from "./components/keywords/$ref" 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 KeywordProperties from "./components/keywords/Properties/Properties" import KeywordType from "./components/keywords/Type/Type" import KeywordFormat from "./components/keywords/Format/Format" @@ -43,6 +44,7 @@ export const withJSONSchemaContext = (Component, overrides = {}) => { Keyword$dynamicRef, Keyword$defs, Keyword$comment, + KeywordAllOf, 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 f95632f6..d20b4cde 100644 --- a/src/core/plugins/json-schema-2020-12/index.js +++ b/src/core/plugins/json-schema-2020-12/index.js @@ -12,6 +12,7 @@ import Keyword$ref from "./components/keywords/$ref" 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 KeywordType from "./components/keywords/Type/Type" import KeywordFormat from "./components/keywords/Format/Format" import KeywordTitle from "./components/keywords/Title/Title" @@ -34,6 +35,7 @@ const JSONSchema202012Plugin = () => ({ JSONSchema202012Keyword$dynamicRef: Keyword$dynamicRef, JSONSchema202012Keyword$defs: Keyword$defs, JSONSchema202012Keyword$comment: Keyword$comment, + JSONSchema202012KeywordAllOf: KeywordAllOf, 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 3e5a65ed..c14eb3a7 100644 --- a/src/core/plugins/oas31/wrap-components/models.jsx +++ b/src/core/plugins/oas31/wrap-components/models.jsx @@ -20,6 +20,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => { const Keyword$dynamicRef = getComponent("JSONSchema202012Keyword$dynamicRef") const Keyword$defs = getComponent("JSONSchema202012Keyword$defs") const Keyword$comment = getComponent("JSONSchema202012Keyword$comment") + const KeywordAllOf = getComponent("JSONSchema202012KeywordAllOf") const KeywordProperties = getComponent("JSONSchema202012KeywordProperties") const KeywordType = getComponent("JSONSchema202012KeywordType") const KeywordFormat = getComponent("JSONSchema202012KeywordFormat") @@ -48,6 +49,7 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => { Keyword$dynamicRef, Keyword$defs, Keyword$comment, + KeywordAllOf, KeywordProperties, KeywordType, KeywordFormat,