Skip to content

Commit

Permalink
feat(oas31): add support for Schema Object discriminator keyword (#8658)
Browse files Browse the repository at this point in the history
Refs #8513
  • Loading branch information
char0n committed May 11, 2023
1 parent 9bb5a21 commit 11bb4f9
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/core/plugins/oas31/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
import { selectLicenseUrl as selectOAS31LicenseUrl } from "./selectors"
import JSONSchema202012KeywordExample from "./json-schema-2020-12-extensions/components/keywords/Example"
import JSONSchema202012KeywordXml from "./json-schema-2020-12-extensions/components/keywords/Xml"
import JSONSchema202012KeywordDiscriminator from "./json-schema-2020-12-extensions/components/keywords/Discriminator/Discriminator"
import JSONSchema202012KeywordDescriptionWrapper from "./json-schema-2020-12-extensions/wrap-components/keywords/Description"
import JSONSchema202012KeywordDefaultWrapper from "./json-schema-2020-12-extensions/wrap-components/keywords/Default"
import { makeIsExpandable } from "./json-schema-2020-12-extensions/fn"
Expand Down Expand Up @@ -86,6 +87,7 @@ const OAS31Plugin = ({ getSystem }) => {
OAS31Models: Models,
JSONSchema202012KeywordExample,
JSONSchema202012KeywordXml,
JSONSchema202012KeywordDiscriminator,
},
wrapComponents: {
InfoContainer: InfoWrapper,
Expand Down
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Example = ({ schema, getSystem }) => {

return (
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--example">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--primary">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
Example
</span>
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--const">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const Xml = ({ schema, getSystem }) => {
</span>
</div>
)}

</li>
<li className="json-schema-2020-12-property">
{xml.namespace && (
<div className="json-schema-2020-12-keyword">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
Expand All @@ -87,7 +88,8 @@ const Xml = ({ schema, getSystem }) => {
</span>
</div>
)}

</li>
<li className="json-schema-2020-12-property">
{xml.prefix && (
<div className="json-schema-2020-12-keyword">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import { createOnlyOAS31ComponentWrapper } from "../../../fn"
const DefaultWrapper = createOnlyOAS31ComponentWrapper(
({ schema, getSystem, originalComponent: KeywordDefault }) => {
const { getComponent } = getSystem()
const KeywordExample = getComponent("JSONSchema202012KeywordExample")
const KeywordDiscriminator = getComponent(
"JSONSchema202012KeywordDiscriminator"
)
const KeywordXml = getComponent("JSONSchema202012KeywordXml")
const KeywordExample = getComponent("JSONSchema202012KeywordExample")

return (
<>
<KeywordDefault schema={schema} />
<KeywordExample schema={schema} getSystem={getSystem} />
<KeywordDiscriminator schema={schema} getSystem={getSystem} />
<KeywordXml schema={schema} getSystem={getSystem} />
<KeywordExample schema={schema} getSystem={getSystem} />
</>
)
}
Expand Down

0 comments on commit 11bb4f9

Please sign in to comment.