-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(oas31): add support for Schema Object discriminator keyword
Refs #8513
- Loading branch information
Showing
6 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
.../oas31/json-schema-2020-12-extensions/components/keywords/Discriminator/Discriminator.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import React, { useCallback, useState } from "react" | ||
import PropTypes from "prop-types" | ||
import classNames from "classnames" | ||
|
||
import DiscriminatorMapping from "./DiscriminatorMapping" | ||
|
||
const Discriminator = ({ schema, getSystem }) => { | ||
const discriminator = schema?.discriminator || {} | ||
const { fn, getComponent } = getSystem() | ||
const { useIsExpandedDeeply, useComponent } = fn.jsonSchema202012 | ||
const isExpandedDeeply = useIsExpandedDeeply() | ||
const [expanded, setExpanded] = useState(isExpandedDeeply) | ||
const [expandedDeeply, setExpandedDeeply] = useState(false) | ||
const Accordion = useComponent("Accordion") | ||
const ExpandDeepButton = useComponent("ExpandDeepButton") | ||
const JSONSchemaDeepExpansionContext = getComponent( | ||
"JSONSchema202012DeepExpansionContext" | ||
)() | ||
|
||
/** | ||
* Event handlers. | ||
*/ | ||
const handleExpansion = useCallback(() => { | ||
setExpanded((prev) => !prev) | ||
}, []) | ||
const handleExpansionDeep = useCallback((e, expandedDeepNew) => { | ||
setExpanded(expandedDeepNew) | ||
setExpandedDeeply(expandedDeepNew) | ||
}, []) | ||
|
||
/** | ||
* Rendering. | ||
*/ | ||
if (Object.keys(discriminator).length === 0) { | ||
return null | ||
} | ||
|
||
return ( | ||
<JSONSchemaDeepExpansionContext.Provider value={expandedDeeply}> | ||
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--discriminator"> | ||
<Accordion expanded={expanded} onChange={handleExpansion}> | ||
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary"> | ||
Discriminator | ||
</span> | ||
</Accordion> | ||
<ExpandDeepButton expanded={expanded} onClick={handleExpansionDeep} /> | ||
{discriminator.propertyName && ( | ||
<span className="json-schema-2020-12__attribute json-schema-2020-12__attribute--muted"> | ||
{discriminator.propertyName} | ||
</span> | ||
)} | ||
<ul | ||
className={classNames("json-schema-2020-12-keyword__children", { | ||
"json-schema-2020-12-keyword__children--collapsed": !expanded, | ||
})} | ||
> | ||
{expanded && ( | ||
<li className="json-schema-2020-12-property"> | ||
<DiscriminatorMapping discriminator={discriminator} /> | ||
</li> | ||
)} | ||
</ul> | ||
</div> | ||
</JSONSchemaDeepExpansionContext.Provider> | ||
) | ||
} | ||
|
||
Discriminator.propTypes = { | ||
schema: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]), | ||
getSystem: PropTypes.func.isRequired, | ||
} | ||
|
||
export default Discriminator |
36 changes: 36 additions & 0 deletions
36
...json-schema-2020-12-extensions/components/keywords/Discriminator/DiscriminatorMapping.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import React from "react" | ||
import PropTypes from "prop-types" | ||
|
||
const DiscriminatorMapping = ({ discriminator }) => { | ||
const mapping = discriminator?.mapping || {} | ||
|
||
if (Object.keys(mapping).length === 0) { | ||
return null | ||
} | ||
|
||
return Object.entries(mapping).map(([key, value]) => ( | ||
<div key={`${key}-${value}`} className="json-schema-2020-12-keyword"> | ||
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary"> | ||
{key} | ||
</span> | ||
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--secondary"> | ||
{value} | ||
</span> | ||
</div> | ||
)) | ||
} | ||
|
||
DiscriminatorMapping.propTypes = { | ||
discriminator: PropTypes.shape({ | ||
mapping: PropTypes.any, | ||
}), | ||
} | ||
|
||
DiscriminatorMapping.defaultProps = { | ||
mapping: undefined, | ||
} | ||
|
||
export default DiscriminatorMapping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters