Skip to content

Commit

Permalink
feat(oas31): add support for Schema Object xml keyword
Browse files Browse the repository at this point in the history
Refs #8513
  • Loading branch information
char0n committed May 11, 2023
1 parent f74ff82 commit 6e7759c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/core/plugins/json-schema-2020-12/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import Accordion from "./components/Accordion/Accordion"
import ExpandDeepButton from "./components/ExpandDeepButton/ExpandDeepButton"
import ChevronRightIcon from "./components/icons/ChevronRight"
import { upperFirst, hasKeyword, isExpandable } from "./fn"
import { useFn } from "./hooks"
import { JSONSchemaDeepExpansionContext } from "./context"
import { useFn, useComponent, useIsExpandedDeeply } from "./hooks"
import { withJSONSchemaContext } from "./hoc"

const JSONSchema202012Plugin = () => ({
Expand Down Expand Up @@ -92,13 +93,16 @@ const JSONSchema202012Plugin = () => ({
JSONSchema202012ExpandDeepButton: ExpandDeepButton,
JSONSchema202012ChevronRightIcon: ChevronRightIcon,
withJSONSchema202012Context: withJSONSchemaContext,
JSONSchema202012DeepExpansionContext: () => JSONSchemaDeepExpansionContext,
},
fn: {
upperFirst,
jsonSchema202012: {
isExpandable,
hasKeyword,
useFn,
useComponent,
useIsExpandedDeeply,
},
},
})
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugins/oas31/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
} from "./spec-extensions/wrap-selectors"
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 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 @@ -84,6 +85,7 @@ const OAS31Plugin = ({ getSystem }) => {
OAS31VersionPragmaFilter: VersionPragmaFilter,
OAS31Models: Models,
JSONSchema202012KeywordExample,
JSONSchema202012KeywordXml,
},
wrapComponents: {
InfoContainer: InfoWrapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import React from "react"
import PropTypes from "prop-types"

const Example = ({ schema, fn }) => {
const Example = ({ schema, getSystem }) => {
const { fn } = getSystem()
const { hasKeyword, stringify } = fn.jsonSchema202012.useFn()

if (!hasKeyword(schema, "example")) return null
Expand All @@ -23,11 +24,7 @@ const Example = ({ schema, fn }) => {

Example.propTypes = {
schema: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
fn: PropTypes.shape({
jsonSchema202012: PropTypes.shape({
useFn: PropTypes.func.isRequired,
}).isRequired,
}).isRequired,
getSystem: PropTypes.func.isRequired,
}

export default Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* @prettier
*/
import React, { useCallback, useState } from "react"
import PropTypes from "prop-types"
import classNames from "classnames"

const Xml = ({ schema, getSystem }) => {
const xml = schema?.xml || {}
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(xml).length === 0) {
return null
}

return (
<JSONSchemaDeepExpansionContext.Provider value={expandedDeeply}>
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword--xml">
<Accordion expanded={expanded} onChange={handleExpansion}>
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
XML
</span>
</Accordion>
<ExpandDeepButton expanded={expanded} onClick={handleExpansionDeep} />
{xml.attribute === true && (
<span className="json-schema-2020-12__attribute json-schema-2020-12__attribute--muted">
attribute
</span>
)}
{xml.wrapped === true && (
<span className="json-schema-2020-12__attribute json-schema-2020-12__attribute--muted">
wrapped
</span>
)}
<strong className="json-schema-2020-12__attribute json-schema-2020-12__attribute--primary">
object
</strong>
<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">
{xml.name && (
<div className="json-schema-2020-12-keyword json-schema-2020-12-keyword">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
name
</span>
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--secondary">
{xml.name}
</span>
</div>
)}

{xml.namespace && (
<div className="json-schema-2020-12-keyword">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
namespace
</span>
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--secondary">
{xml.namespace}
</span>
</div>
)}

{xml.prefix && (
<div className="json-schema-2020-12-keyword">
<span className="json-schema-2020-12-keyword__name json-schema-2020-12-keyword__name--secondary">
prefix
</span>
<span className="json-schema-2020-12-keyword__value json-schema-2020-12-keyword__value--secondary">
{xml.prefix}
</span>
</div>
)}
</li>
</>
)}
</ul>
</div>
</JSONSchemaDeepExpansionContext.Provider>
)
}

Xml.propTypes = {
schema: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
fn: PropTypes.shape({
jsonSchema202012: PropTypes.shape({
useIsExpandedDeeply: PropTypes.func.isRequired,
useComponent: PropTypes.func.isRequired,
}).isRequired,
}).isRequired,
}

export default Xml
3 changes: 2 additions & 1 deletion src/core/plugins/oas31/json-schema-2020-12-extensions/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export const makeIsExpandable = (original, { fn }) => {

const { hasKeyword } = fn.jsonSchema202012

return (schema) => original(schema) || hasKeyword(schema, "example")
return (schema) =>
original(schema) || hasKeyword(schema, "example") || schema?.xml
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const DefaultWrapper = createOnlyOAS31ComponentWrapper(
({ schema, getSystem, originalComponent: KeywordDefault }) => {
const { getComponent, fn } = getSystem()
const KeywordExample = getComponent("JSONSchema202012KeywordExample")
const KeywordXml = getComponent("JSONSchema202012KeywordXml")

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

0 comments on commit 6e7759c

Please sign in to comment.