Skip to content

Commit 0aed3bc

Browse files
glowcloudJulien Bourges
andauthored
fix(json-schema-2020-12): expand deeply all Schema Objects and complex keywords (#9581)
Refs #9508 Supersedes #9510 Co-authored-by: Julien Bourges <[email protected]>
1 parent 0115054 commit 0aed3bc

File tree

10 files changed

+230
-14
lines changed

10 files changed

+230
-14
lines changed

src/core/plugins/json-schema-2020-12/components/keywords/$defs.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../prop-types"
8-
import { useComponent, useIsExpandedDeeply } from "../../hooks"
8+
import { useComponent, useIsExpanded, useIsExpandedDeeply } from "../../hooks"
99
import { JSONSchemaDeepExpansionContext } from "../../context"
1010

1111
const $defs = ({ schema }) => {
1212
const $defs = schema?.$defs || {}
13+
const isExpanded = useIsExpanded()
1314
const isExpandedDeeply = useIsExpandedDeeply()
14-
const [expanded, setExpanded] = useState(isExpandedDeeply)
15+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1516
const [expandedDeeply, setExpandedDeeply] = useState(false)
1617
const Accordion = useComponent("Accordion")
1718
const ExpandDeepButton = useComponent("ExpandDeepButton")

src/core/plugins/json-schema-2020-12/components/keywords/$vocabulary/$vocabulary.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../../prop-types"
8-
import { useComponent, useIsExpandedDeeply } from "../../../hooks"
8+
import {
9+
useComponent,
10+
useIsExpanded,
11+
useIsExpandedDeeply,
12+
} from "../../../hooks"
913

1014
const $vocabulary = ({ schema }) => {
15+
const isExpanded = useIsExpanded()
1116
const isExpandedDeeply = useIsExpandedDeeply()
12-
const [expanded, setExpanded] = useState(isExpandedDeeply)
17+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1318
const Accordion = useComponent("Accordion")
1419

1520
const handleExpansion = useCallback(() => {

src/core/plugins/json-schema-2020-12/components/keywords/AllOf.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../prop-types"
8-
import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks"
8+
import {
9+
useFn,
10+
useComponent,
11+
useIsExpanded,
12+
useIsExpandedDeeply,
13+
} from "../../hooks"
914
import { JSONSchemaDeepExpansionContext } from "../../context"
1015

1116
const AllOf = ({ schema }) => {
1217
const allOf = schema?.allOf || []
1318
const fn = useFn()
19+
const isExpanded = useIsExpanded()
1420
const isExpandedDeeply = useIsExpandedDeeply()
15-
const [expanded, setExpanded] = useState(isExpandedDeeply)
21+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1622
const [expandedDeeply, setExpandedDeeply] = useState(false)
1723
const Accordion = useComponent("Accordion")
1824
const ExpandDeepButton = useComponent("ExpandDeepButton")

src/core/plugins/json-schema-2020-12/components/keywords/AnyOf.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../prop-types"
8-
import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks"
8+
import {
9+
useFn,
10+
useComponent,
11+
useIsExpanded,
12+
useIsExpandedDeeply,
13+
} from "../../hooks"
914
import { JSONSchemaDeepExpansionContext } from "../../context"
1015

1116
const AnyOf = ({ schema }) => {
1217
const anyOf = schema?.anyOf || []
1318
const fn = useFn()
19+
const isExpanded = useIsExpanded()
1420
const isExpandedDeeply = useIsExpandedDeeply()
15-
const [expanded, setExpanded] = useState(isExpandedDeeply)
21+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1622
const [expandedDeeply, setExpandedDeeply] = useState(false)
1723
const Accordion = useComponent("Accordion")
1824
const ExpandDeepButton = useComponent("ExpandDeepButton")

src/core/plugins/json-schema-2020-12/components/keywords/DependentSchemas.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../prop-types"
8-
import { useComponent, useIsExpandedDeeply } from "../../hooks"
8+
import { useComponent, useIsExpanded, useIsExpandedDeeply } from "../../hooks"
99
import { JSONSchemaDeepExpansionContext } from "../../context"
1010

1111
const DependentSchemas = ({ schema }) => {
1212
const dependentSchemas = schema?.dependentSchemas || []
13+
const isExpanded = useIsExpanded()
1314
const isExpandedDeeply = useIsExpandedDeeply()
14-
const [expanded, setExpanded] = useState(isExpandedDeeply)
15+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1516
const [expandedDeeply, setExpandedDeeply] = useState(false)
1617
const Accordion = useComponent("Accordion")
1718
const ExpandDeepButton = useComponent("ExpandDeepButton")

src/core/plugins/json-schema-2020-12/components/keywords/OneOf.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../prop-types"
8-
import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks"
8+
import {
9+
useFn,
10+
useComponent,
11+
useIsExpanded,
12+
useIsExpandedDeeply,
13+
} from "../../hooks"
914
import { JSONSchemaDeepExpansionContext } from "../../context"
1015

1116
const OneOf = ({ schema }) => {
1217
const oneOf = schema?.oneOf || []
1318
const fn = useFn()
19+
const isExpanded = useIsExpanded()
1420
const isExpandedDeeply = useIsExpandedDeeply()
15-
const [expanded, setExpanded] = useState(isExpandedDeeply)
21+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1622
const [expandedDeeply, setExpandedDeeply] = useState(false)
1723
const Accordion = useComponent("Accordion")
1824
const ExpandDeepButton = useComponent("ExpandDeepButton")

src/core/plugins/json-schema-2020-12/components/keywords/PrefixItems.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import React, { useCallback, useState } from "react"
55
import classNames from "classnames"
66

77
import { schema } from "../../prop-types"
8-
import { useFn, useComponent, useIsExpandedDeeply } from "../../hooks"
8+
import {
9+
useFn,
10+
useComponent,
11+
useIsExpandedDeeply,
12+
useIsExpanded,
13+
} from "../../hooks"
914
import { JSONSchemaDeepExpansionContext } from "../../context"
1015

1116
const PrefixItems = ({ schema }) => {
1217
const prefixItems = schema?.prefixItems || []
1318
const fn = useFn()
19+
const isExpanded = useIsExpanded()
1420
const isExpandedDeeply = useIsExpandedDeeply()
15-
const [expanded, setExpanded] = useState(isExpandedDeeply)
21+
const [expanded, setExpanded] = useState(isExpanded || isExpandedDeeply)
1622
const [expandedDeeply, setExpandedDeeply] = useState(false)
1723
const Accordion = useComponent("Accordion")
1824
const ExpandDeepButton = useComponent("ExpandDeepButton")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @prettier
3+
*/
4+
5+
describe("JSON Schema 2020-12 complex keywords expansion", () => {
6+
it("should deeply expand all Schemas and complex keywords", () => {
7+
cy.visit("/pages/json-schema-2020-12-expansion/").then(
8+
() => {
9+
cy.get(".json-schema-2020-12-accordion")
10+
.find(".json-schema-2020-12-accordion__icon--collapsed")
11+
.should("not.exist")
12+
cy.contains("anyOf1-p1-p2-p1").should("exist")
13+
cy.contains("oneOf1-p1-p2-p1").should("exist")
14+
cy.contains("Prefix items").should("exist")
15+
cy.contains("exampleDef").should("exist")
16+
cy.contains("https://json-schema.org/draft/2020-12/vocab/core").should("exist")
17+
}
18+
)
19+
})
20+
})
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
openapi: "3.1.0"
2+
info:
3+
version: "0.0.1"
4+
title: "Swagger UI Webpack Setup"
5+
description: "Demonstrates Swagger UI"
6+
components:
7+
schemas: {}
8+
security: []
9+
paths:
10+
/pets:
11+
get:
12+
responses:
13+
200:
14+
description: "OK"
15+
content:
16+
application/json:
17+
schema:
18+
$vocabulary:
19+
https://json-schema.org/draft/2020-12/vocab/core: true
20+
https://json-schema.org/draft/2020-12/vocab/applicator: true
21+
https://json-schema.org/draft/2020-12/vocab/validation: true
22+
https://json-schema.org/draft/2020-12/vocab/meta-data: true
23+
https://json-schema.org/draft/2020-12/vocab/format-annotation: false
24+
https://example.com/my-custom-vocab: true
25+
$defs:
26+
exampleDef:
27+
type: string
28+
allOf:
29+
- oneOf:
30+
- type: object
31+
properties:
32+
oneOf1-p1:
33+
type: object
34+
properties:
35+
oneOf1-p1-p1:
36+
type: string
37+
oneOf1-p1-p2:
38+
type: object
39+
properties:
40+
oneOf1-p1-p2-p1:
41+
type: string
42+
oneOf1-p2:
43+
type: string
44+
- type: object
45+
properties:
46+
oneOf2-p1:
47+
type: string
48+
oneOf2-p2:
49+
type: string
50+
- anyOf:
51+
- type: object
52+
properties:
53+
anyOf1-p1:
54+
type: object
55+
properties:
56+
anyOf1-p1-p1:
57+
type: string
58+
anyOf1-p1-p2:
59+
type: object
60+
properties:
61+
anyOf1-p1-p2-p1:
62+
type: string
63+
anyOf1-p2:
64+
type: string
65+
- type: object
66+
properties:
67+
anyOf2-p1:
68+
type: object
69+
properties:
70+
anyOf2-p1-p1:
71+
type: string
72+
anyOf2-p2:
73+
type: string
74+
- type: object
75+
properties:
76+
p1:
77+
type: object
78+
properties:
79+
p1-p1:
80+
type: string
81+
p1-p2:
82+
type: object
83+
properties:
84+
p1-p2-p1:
85+
type: string
86+
dependentSchemas:
87+
p1-p1:
88+
properties:
89+
p3:
90+
type: array
91+
prefixItems:
92+
- type: string
93+
enum: [a, b, c]
94+
required: [p3]
95+
p2:
96+
type: string
97+
prefixItems:
98+
- type: string
99+
enum: [a, b, c]
100+
tags: []
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!-- HTML for dev server -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Swagger UI</title>
8+
<link rel="stylesheet" type="text/css" href="/swagger-ui.css">
9+
<style>
10+
html {
11+
box-sizing: border-box;
12+
overflow: -moz-scrollbars-vertical;
13+
overflow-y: scroll;
14+
}
15+
*,
16+
*:before,
17+
*:after {
18+
box-sizing: inherit;
19+
}
20+
body {
21+
margin:0;
22+
background: #fafafa;
23+
}
24+
</style>
25+
</head>
26+
27+
<body>
28+
29+
<div id="swagger-ui"></div>
30+
31+
<script src="/swagger-ui-bundle.js" charset="UTF-8"> </script>
32+
<script src="/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
33+
<script>
34+
window.onload = function() {
35+
window["SwaggerUIBundle"] = window["swagger-ui-bundle"]
36+
window["SwaggerUIStandalonePreset"] = window["swagger-ui-standalone-preset"]
37+
// Build a system
38+
const ui = SwaggerUIBundle({
39+
url: "./expansion.yaml",
40+
dom_id: '#swagger-ui',
41+
presets: [
42+
SwaggerUIBundle.presets.apis,
43+
SwaggerUIStandalonePreset
44+
],
45+
plugins: [],
46+
layout: "StandaloneLayout",
47+
docExpansion: "full",
48+
defaultModelExpandDepth: 100,
49+
defaultModelRendering: "model",
50+
onComplete: () => {
51+
if(window.completeCount) {
52+
window.completeCount++
53+
} else {
54+
window.completeCount = 1
55+
}
56+
},
57+
queryConfigEnabled: true,
58+
})
59+
60+
window.ui = ui
61+
}
62+
</script>
63+
</body>
64+
65+
</html>

0 commit comments

Comments
 (0)