Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce extensible framework for generating data from JSON Schema 2020-12 #8724

Merged
merged 6 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/core/components/param-body.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { PureComponent } from "react"
import PropTypes from "prop-types"
import { fromJS, List } from "immutable"
import { getSampleSchema } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"

const NOOP = Function.prototype
Expand Down Expand Up @@ -67,10 +66,10 @@ export default class ParamBody extends PureComponent {
}

sample = (xml) => {
let { param, fn:{inferSchema} } = this.props
let schema = inferSchema(param.toJS())
let { param, fn} = this.props
let schema = fn.inferSchema(param.toJS())

return getSampleSchema(schema, xml, {
return fn.getSampleSchema(schema, xml, {
includeWriteOnly: true
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Map, List } from "immutable"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import win from "core/window"
import { getSampleSchema, getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
import { getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
import getParameterSchema from "../../helpers/get-parameter-schema.js"

export default class ParameterRow extends Component {
Expand Down Expand Up @@ -94,7 +94,7 @@ export default class ParameterRow extends Component {
}

setDefaultValue = () => {
let { specSelectors, pathMethod, rawParam, oas3Selectors } = this.props
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props

const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
const { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
Expand All @@ -104,7 +104,7 @@ export default class ParameterRow extends Component {
.first()

// getSampleSchema could return null
const generatedSampleValue = schema ? getSampleSchema(schema.toJS(), parameterMediaType, {
const generatedSampleValue = schema ? fn.getSampleSchema(schema.toJS(), parameterMediaType, {

includeWriteOnly: true
}) : null
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import cx from "classnames"
import { fromJS, Seq, Iterable, List, Map } from "immutable"
import { getExtensions, getSampleSchema, fromJSOrdered, stringify } from "core/utils"
import { getExtensions, fromJSOrdered, stringify } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"


Expand Down Expand Up @@ -93,7 +93,7 @@ export default class Response extends React.Component {
oas3Actions,
} = this.props

let { inferSchema } = fn
let { inferSchema, getSampleSchema } = fn
let isOAS3 = specSelectors.isOAS3()
const { showExtensions } = getConfigs()

Expand Down
6 changes: 3 additions & 3 deletions src/core/json-schema-components.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { List, fromJS } from "immutable"
import cx from "classnames"
import ImPropTypes from "react-immutable-proptypes"
import DebounceInput from "react-debounce-input"
import { stringify, getSampleSchema } from "core/utils"
//import "less/json-schema-form"
import { stringify } from "core/utils"

const noop = ()=> {}
const JsonSchemaPropShape = {
Expand Down Expand Up @@ -156,9 +155,10 @@ export class JsonSchema_array extends PureComponent {
}

addItem = () => {
const { fn } = this.props
let newValue = valueOrEmptyList(this.state.value)
this.setState(() => ({
value: newValue.push(getSampleSchema(this.state.schema.get("items"), false, {
value: newValue.push(fn.getSampleSchema(this.state.schema.get("items"), false, {
includeWriteOnly: true
}))
}), this.onChange)
Expand Down
10 changes: 7 additions & 3 deletions src/core/plugins/json-schema-2020-12/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ export const getType = (schema, processedSchemas = new WeakSet()) => {
}

const inferType = () => {
if (prefixItems || items) {
if (prefixItems || items || schema.contains) {
return getArrayType()
} else if (schema.properties || schema.additionalProperties) {
} else if (
schema.properties ||
schema.additionalProperties ||
schema.patternProperties
) {
return "object"
} else if (
schema.pattern ||
Expand All @@ -77,7 +81,7 @@ export const getType = (schema, processedSchemas = new WeakSet()) => {
schema.multipleOf
) {
return "number | integer"
} else if (schema.const !== undefined) {
} else if (typeof schema.const !== "undefined") {
if (schema.const === null) {
return "null"
} else if (typeof schema.const === "boolean") {
Expand Down
12 changes: 12 additions & 0 deletions src/core/plugins/json-schema-2020-12/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ 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 {
sampleFromSchema,
sampleFromSchemaGeneric,
createXMLExample,
memoizedSampleFromSchema,
memoizedCreateXMLExample,
} from "./samples-extensions/fn"
import { JSONSchemaDeepExpansionContext } from "./context"
import { useFn, useConfig, useComponent, useIsExpandedDeeply } from "./hooks"
import { withJSONSchemaContext } from "./hoc"
Expand Down Expand Up @@ -104,6 +111,11 @@ const JSONSchema202012Plugin = () => ({
useConfig,
useComponent,
useIsExpandedDeeply,
sampleFromSchema,
sampleFromSchemaGeneric,
createXMLExample,
memoizedSampleFromSchema,
memoizedCreateXMLExample,
},
},
})
Expand Down
Loading