Skip to content

Commit d2fff7c

Browse files
authored
fix(json-schema-2020-12): avoid accessing properties of null schemas (#10397)
1 parent 25daec9 commit d2fff7c

File tree

9 files changed

+45
-19
lines changed

9 files changed

+45
-19
lines changed

‎src/core/plugins/json-schema-2020-12/components/JSONSchema/JSONSchema.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const JSONSchema = forwardRef(
3535
) => {
3636
const fn = useFn()
3737
// this implementation assumes that $id is always non-relative URI
38-
const pathToken = identifier || schema.$id || name
38+
const pathToken = identifier || schema?.$id || name
3939
const { path } = usePath(pathToken)
4040
const { isExpanded, setExpanded, setCollapsed } = useIsExpanded(pathToken)
4141
const [level, nextLevel] = useLevel()

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useFn, useComponent } from "../../hooks"
88

99
const AdditionalProperties = ({ schema }) => {
1010
const fn = useFn()
11-
const { additionalProperties } = schema
1211
const JSONSchema = useComponent("JSONSchema")
1312

1413
if (!fn.hasKeyword(schema, "additionalProperties")) return null
@@ -24,14 +23,14 @@ const AdditionalProperties = ({ schema }) => {
2423

2524
return (
2625
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--additionalProperties">
27-
{additionalProperties === true ? (
26+
{schema.additionalProperties === true ? (
2827
<>
2928
{name}
3029
<span className="json-schema-2020-12__attribute json-schema-2020-12__attribute--primary">
3130
allowed
3231
</span>
3332
</>
34-
) : additionalProperties === false ? (
33+
) : schema.additionalProperties === false ? (
3534
<>
3635
{name}
3736
<span className="json-schema-2020-12__attribute json-schema-2020-12__attribute--primary">
@@ -41,7 +40,7 @@ const AdditionalProperties = ({ schema }) => {
4140
) : (
4241
<JSONSchema
4342
name={name}
44-
schema={additionalProperties}
43+
schema={schema.additionalProperties}
4544
identifier="additionalProperties"
4645
/>
4746
)}

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44
import React from "react"
55
import PropTypes from "prop-types"
66

7+
import { isPlainObject } from "../../../fn"
8+
79
/**
810
* This component represents various constraint keywords
911
* from JSON Schema 2020-12 validation vocabulary.
1012
*/
11-
const Constraint = ({ constraint }) => (
12-
<span
13-
className={`json-schema-2020-12__constraint json-schema-2020-12__constraint--${constraint.scope}`}
14-
>
15-
{constraint.value}
16-
</span>
17-
)
13+
const Constraint = ({ constraint }) => {
14+
if (
15+
!isPlainObject(constraint) ||
16+
typeof constraint.scope !== "string" ||
17+
typeof constraint.value !== "string"
18+
) {
19+
return null
20+
}
21+
22+
return (
23+
<span
24+
className={`json-schema-2020-12__constraint json-schema-2020-12__constraint--${constraint.scope}`}
25+
>
26+
{constraint.value}
27+
</span>
28+
)
29+
}
1830

1931
Constraint.propTypes = {
2032
constraint: PropTypes.shape({

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import PropTypes from "prop-types"
77
import * as propTypes from "../../../prop-types"
88

99
const DependentRequired = ({ dependentRequired }) => {
10-
if (dependentRequired.length === 0) return null
10+
if (!Array.isArray(dependentRequired) || dependentRequired.length === 0) {
11+
return null
12+
}
1113

1214
return (
1315
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--dependentRequired">

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useFn, useComponent } from "../../hooks"
88

99
const PropertyNames = ({ schema }) => {
1010
const fn = useFn()
11-
const { propertyNames } = schema
1211
const JSONSchema = useComponent("JSONSchema")
1312
const name = (
1413
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--primary">
@@ -25,7 +24,7 @@ const PropertyNames = ({ schema }) => {
2524
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--propertyNames">
2625
<JSONSchema
2726
name={name}
28-
schema={propertyNames}
27+
schema={schema.propertyNames}
2928
identifier="propertyNames"
3029
/>
3130
</div>

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useFn, useComponent } from "../../hooks"
88

99
const UnevaluatedItems = ({ schema }) => {
1010
const fn = useFn()
11-
const { unevaluatedItems } = schema
1211
const JSONSchema = useComponent("JSONSchema")
1312

1413
/**
@@ -26,7 +25,7 @@ const UnevaluatedItems = ({ schema }) => {
2625
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--unevaluatedItems">
2726
<JSONSchema
2827
name={name}
29-
schema={unevaluatedItems}
28+
schema={schema.unevaluatedItems}
3029
identifier="unevaluatedItems"
3130
/>
3231
</div>

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useFn, useComponent } from "../../hooks"
88

99
const UnevaluatedProperties = ({ schema }) => {
1010
const fn = useFn()
11-
const { unevaluatedProperties } = schema
1211
const JSONSchema = useComponent("JSONSchema")
1312

1413
/**
@@ -26,7 +25,7 @@ const UnevaluatedProperties = ({ schema }) => {
2625
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--unevaluatedProperties">
2726
<JSONSchema
2827
name={name}
29-
schema={unevaluatedProperties}
28+
schema={schema.unevaluatedProperties}
3029
identifier="unevaluatedProperties"
3130
/>
3231
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @prettier
3+
*/
4+
5+
describe("Empty JSON Schema 2020-12", () => {
6+
it("should render as schema of type `any`", () => {
7+
cy.visit("/?url=/documents/features/json-schema-2020-12-empty-schema.yaml")
8+
9+
cy.get(".json-schema-2020-12__title").should("contain", "Test")
10+
cy.get(".json-schema-2020-12__attribute").should("contain", "any")
11+
})
12+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
openapi: 3.1.0
2+
components:
3+
schemas:
4+
Test:

0 commit comments

Comments
 (0)