From 0aed3bc831615265bba9a3dfdc591c2d2481b3a4 Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Wed, 14 Feb 2024 10:16:34 +0100 Subject: [PATCH] fix(json-schema-2020-12): expand deeply all Schema Objects and complex keywords (#9581) Refs #9508 Supersedes #9510 Co-authored-by: Julien Bourges --- .../components/keywords/$defs.jsx | 5 +- .../keywords/$vocabulary/$vocabulary.jsx | 9 +- .../components/keywords/AllOf.jsx | 10 +- .../components/keywords/AnyOf.jsx | 10 +- .../components/keywords/DependentSchemas.jsx | 5 +- .../components/keywords/OneOf.jsx | 10 +- .../components/keywords/PrefixItems.jsx | 10 +- .../json-schema-2020-12/expansion.cy.js | 20 ++++ .../expansion.yaml | 100 ++++++++++++++++++ .../json-schema-2020-12-expansion/index.html | 65 ++++++++++++ 10 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 test/e2e-cypress/e2e/features/plugins/json-schema-2020-12/expansion.cy.js create mode 100644 test/e2e-cypress/static/pages/json-schema-2020-12-expansion/expansion.yaml create mode 100644 test/e2e-cypress/static/pages/json-schema-2020-12-expansion/index.html diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx index 200d6ca2..3c0a6b08 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx @@ -5,13 +5,14 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../prop-types" -import { useComponent, useIsExpandedDeeply } from "../../hooks" +import { useComponent, useIsExpanded, useIsExpandedDeeply } from "../../hooks" import { JSONSchemaDeepExpansionContext } from "../../context" const $defs = ({ schema }) => { const $defs = schema?.$defs || {} + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(false) const Accordion = useComponent("Accordion") const ExpandDeepButton = useComponent("ExpandDeepButton") diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/$vocabulary/$vocabulary.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/$vocabulary/$vocabulary.jsx index 8ff0bc7a..f33ba920 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/$vocabulary/$vocabulary.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/$vocabulary/$vocabulary.jsx @@ -5,11 +5,16 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../../prop-types" -import { useComponent, useIsExpandedDeeply } from "../../../hooks" +import { + useComponent, + useIsExpanded, + useIsExpandedDeeply, +} from "../../../hooks" const $vocabulary = ({ schema }) => { + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const Accordion = useComponent("Accordion") const handleExpansion = useCallback(() => { diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/AllOf.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/AllOf.jsx index c38e9445..8d01800f 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/AllOf.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/AllOf.jsx @@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../prop-types" -import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks" +import { + useFn, + useComponent, + useIsExpanded, + useIsExpandedDeeply, +} from "../../hooks" import { JSONSchemaDeepExpansionContext } from "../../context" const AllOf = ({ schema }) => { const allOf = schema?.allOf || [] const fn = useFn() + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(false) const Accordion = useComponent("Accordion") const ExpandDeepButton = useComponent("ExpandDeepButton") diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf.jsx index 634e6c25..d9684091 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/AnyOf.jsx @@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../prop-types" -import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks" +import { + useFn, + useComponent, + useIsExpanded, + useIsExpandedDeeply, +} from "../../hooks" import { JSONSchemaDeepExpansionContext } from "../../context" const AnyOf = ({ schema }) => { const anyOf = schema?.anyOf || [] const fn = useFn() + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(false) const Accordion = useComponent("Accordion") const ExpandDeepButton = useComponent("ExpandDeepButton") diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/DependentSchemas.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/DependentSchemas.jsx index a9a94405..63c3596a 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/DependentSchemas.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/DependentSchemas.jsx @@ -5,13 +5,14 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../prop-types" -import { useComponent, useIsExpandedDeeply } from "../../hooks" +import { useComponent, useIsExpanded, useIsExpandedDeeply } from "../../hooks" import { JSONSchemaDeepExpansionContext } from "../../context" const DependentSchemas = ({ schema }) => { const dependentSchemas = schema?.dependentSchemas || [] + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(false) const Accordion = useComponent("Accordion") const ExpandDeepButton = useComponent("ExpandDeepButton") diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/OneOf.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/OneOf.jsx index 6ad7b824..1d93bcff 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/OneOf.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/OneOf.jsx @@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../prop-types" -import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks" +import { + useFn, + useComponent, + useIsExpanded, + useIsExpandedDeeply, +} from "../../hooks" import { JSONSchemaDeepExpansionContext } from "../../context" const OneOf = ({ schema }) => { const oneOf = schema?.oneOf || [] const fn = useFn() + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(false) const Accordion = useComponent("Accordion") const ExpandDeepButton = useComponent("ExpandDeepButton") diff --git a/src/core/plugins/json-schema-2020-12/components/keywords/PrefixItems.jsx b/src/core/plugins/json-schema-2020-12/components/keywords/PrefixItems.jsx index 798a40a8..cb83c4c1 100644 --- a/src/core/plugins/json-schema-2020-12/components/keywords/PrefixItems.jsx +++ b/src/core/plugins/json-schema-2020-12/components/keywords/PrefixItems.jsx @@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react" import classNames from "classnames" import { schema } from "../../prop-types" -import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks" +import { + useFn, + useComponent, + useIsExpandedDeeply, + useIsExpanded, +} from "../../hooks" import { JSONSchemaDeepExpansionContext } from "../../context" const PrefixItems = ({ schema }) => { const prefixItems = schema?.prefixItems || [] const fn = useFn() + const isExpanded = useIsExpanded() const isExpandedDeeply = useIsExpandedDeeply() - const [expanded, setExpanded] = useState(isExpandedDeeply) + const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply) const [expandedDeeply, setExpandedDeeply] = useState(false) const Accordion = useComponent("Accordion") const ExpandDeepButton = useComponent("ExpandDeepButton") diff --git a/test/e2e-cypress/e2e/features/plugins/json-schema-2020-12/expansion.cy.js b/test/e2e-cypress/e2e/features/plugins/json-schema-2020-12/expansion.cy.js new file mode 100644 index 00000000..9a9ff18b --- /dev/null +++ b/test/e2e-cypress/e2e/features/plugins/json-schema-2020-12/expansion.cy.js @@ -0,0 +1,20 @@ +/** + * @prettier + */ + +describe("JSON Schema 2020-12 complex keywords expansion", () => { + it("should deeply expand all Schemas and complex keywords", () => { + cy.visit("/pages/json-schema-2020-12-expansion/").then( + () => { + cy.get(".json-schema-2020-12-accordion") + .find(".json-schema-2020-12-accordion__icon--collapsed") + .should("not.exist") + cy.contains("anyOf1-p1-p2-p1").should("exist") + cy.contains("oneOf1-p1-p2-p1").should("exist") + cy.contains("Prefix items").should("exist") + cy.contains("exampleDef").should("exist") + cy.contains("https://json-schema.org/draft/2020-12/vocab/core").should("exist") + } + ) + }) +}) diff --git a/test/e2e-cypress/static/pages/json-schema-2020-12-expansion/expansion.yaml b/test/e2e-cypress/static/pages/json-schema-2020-12-expansion/expansion.yaml new file mode 100644 index 00000000..5e7f7958 --- /dev/null +++ b/test/e2e-cypress/static/pages/json-schema-2020-12-expansion/expansion.yaml @@ -0,0 +1,100 @@ +openapi: "3.1.0" +info: + version: "0.0.1" + title: "Swagger UI Webpack Setup" + description: "Demonstrates Swagger UI" +components: + schemas: {} +security: [] +paths: + /pets: + get: + responses: + 200: + description: "OK" + content: + application/json: + schema: + $vocabulary: + https://json-schema.org/draft/2020-12/vocab/core: true + https://json-schema.org/draft/2020-12/vocab/applicator: true + https://json-schema.org/draft/2020-12/vocab/validation: true + https://json-schema.org/draft/2020-12/vocab/meta-data: true + https://json-schema.org/draft/2020-12/vocab/format-annotation: false + https://example.com/my-custom-vocab: true + $defs: + exampleDef: + type: string + allOf: + - oneOf: + - type: object + properties: + oneOf1-p1: + type: object + properties: + oneOf1-p1-p1: + type: string + oneOf1-p1-p2: + type: object + properties: + oneOf1-p1-p2-p1: + type: string + oneOf1-p2: + type: string + - type: object + properties: + oneOf2-p1: + type: string + oneOf2-p2: + type: string + - anyOf: + - type: object + properties: + anyOf1-p1: + type: object + properties: + anyOf1-p1-p1: + type: string + anyOf1-p1-p2: + type: object + properties: + anyOf1-p1-p2-p1: + type: string + anyOf1-p2: + type: string + - type: object + properties: + anyOf2-p1: + type: object + properties: + anyOf2-p1-p1: + type: string + anyOf2-p2: + type: string + - type: object + properties: + p1: + type: object + properties: + p1-p1: + type: string + p1-p2: + type: object + properties: + p1-p2-p1: + type: string + dependentSchemas: + p1-p1: + properties: + p3: + type: array + prefixItems: + - type: string + enum: [a, b, c] + required: [p3] + p2: + type: string + prefixItems: + - type: string + enum: [a, b, c] +tags: [] diff --git a/test/e2e-cypress/static/pages/json-schema-2020-12-expansion/index.html b/test/e2e-cypress/static/pages/json-schema-2020-12-expansion/index.html new file mode 100644 index 00000000..93a26c93 --- /dev/null +++ b/test/e2e-cypress/static/pages/json-schema-2020-12-expansion/index.html @@ -0,0 +1,65 @@ + + + + + + + Swagger UI + + + + + + +
+ + + + + + +