Skip to content

refactor: introduce function for getting Schema Object type #10330

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions src/core/components/operation-tag.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Im from "immutable"
import { createDeepLinkPath, escapeDeepLinkPath, isFunc } from "core/utils"
import { safeBuildUrl, sanitizeUrl } from "core/utils/url"

/* eslint-disable react/jsx-no-bind */

export default class OperationTag extends React.Component {

static defaultProps = {
Expand Down
26 changes: 13 additions & 13 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, fromJS } from "immutable"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import win from "core/window"
import { getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue, immutableToJS } from "core/utils"
import { getExtensions, getCommonExtensions, numberToString, stringify, isEmptyValue } from "core/utils"
import getParameterSchema from "core/utils/get-parameter-schema.js"

export default class ParameterRow extends Component {
Expand Down Expand Up @@ -152,8 +152,8 @@ export default class ParameterRow extends Component {

//// Dispatch the initial value

const type = fn.jsonSchema202012.foldType(immutableToJS(schema?.get("type")))
const itemType = fn.jsonSchema202012.foldType(immutableToJS(schema?.getIn(["items", "type"])))
const type = fn.getSchemaObjectTypeLabel(schema)
const itemType = fn.getSchemaObjectTypeLabel(schema?.get("items"))

if(initialValue !== undefined) {
this.onChangeWrapper(initialValue)
Expand All @@ -174,7 +174,7 @@ export default class ParameterRow extends Component {
stringify(generatedSampleValue)
)
)
}
}
else if (
type === "array"
&& itemType === "object"
Expand Down Expand Up @@ -251,17 +251,17 @@ export default class ParameterRow extends Component {
if (isOAS3) {
schema = this.composeJsonSchema(schema)
}

let format = schema ? schema.get("format") : null
let isFormData = inType === "formData"
let isFormDataSupported = "FormData" in win
let required = param.get("required")

const typeLabel = fn.jsonSchema202012.getType(immutableToJS(schema))
const type = fn.jsonSchema202012.foldType(immutableToJS(schema?.get("type")))
const itemType = fn.jsonSchema202012.foldType(immutableToJS(schema?.getIn(["items", "type"])))
const isObject = !bodyParam && type === "object"
const isArrayOfObjects = !bodyParam && itemType === "object"
const type = fn.getSchemaObjectType(schema)
const typeLabel = fn.getSchemaObjectTypeLabel(schema)
const itemTypeLabel = fn.getSchemaObjectTypeLabel(schema?.get("items"))
const isObject = !bodyParam && typeLabel === "object"
const isArrayOfObjects = !bodyParam && itemTypeLabel === "object"

let value = paramWithMeta ? paramWithMeta.get("value") : ""
let commonExt = showCommonExtensions ? getCommonExtensions(schema) : null
Expand Down Expand Up @@ -322,7 +322,7 @@ export default class ParameterRow extends Component {
{ !required ? null : <span>&nbsp;*</span> }
</div>
<div className="parameter__type">
{ typeLabel }
{ type }
{ format && <span className="prop-format">(${format})</span>}
</div>
<div className="parameter__deprecated">
Expand Down Expand Up @@ -371,7 +371,7 @@ export default class ParameterRow extends Component {
}

{ (isObject || isArrayOfObjects) ? (
<ModelExample
<ModelExample
getComponent={getComponent}
specPath={specPath.push("schema")}
getConfigs={getConfigs}
Expand All @@ -380,7 +380,7 @@ export default class ParameterRow extends Component {
schema={schema}
example={jsonSchemaForm}
/>
) : jsonSchemaForm
) : jsonSchemaForm
}

{
Expand Down
5 changes: 2 additions & 3 deletions src/core/components/response.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { fromJS, Seq, Iterable, List, Map } from "immutable"
import { getExtensions, fromJSOrdered, stringify } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"

/* eslint-disable react/jsx-no-bind */

const getExampleComponent = ( sampleResponse, HighlightCode ) => {
if (sampleResponse == null) return null
Expand Down Expand Up @@ -140,8 +139,8 @@ export default class Response extends React.Component {
const targetExample = examplesForMediaType
.get(targetExamplesKey, Map({}))
const getMediaTypeExample = (targetExample) =>
Map.isMap(targetExample)
? targetExample.get("value")
Map.isMap(targetExample)
? targetExample.get("value")
: undefined
mediaTypeExample = getMediaTypeExample(targetExample)
if(mediaTypeExample === undefined) {
Expand Down
9 changes: 9 additions & 0 deletions src/core/plugins/json-schema-2020-12-samples/fn/core/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ALL_TYPES } from "./constants"
import { isJSONSchemaObject } from "./predicates"
import { pick as randomPick } from "./random"
import { hasExample, extractExample } from "./example"
import { immutableToJS, isImmutable } from "../../../../utils"

const inferringKeywords = {
array: [
Expand Down Expand Up @@ -154,3 +155,11 @@ export const inferType = (schema, processedSchemas = new WeakSet()) => {
export const getType = (schema) => {
return inferType(schema)
}

export const getFoldType = (schema) => {
if (isImmutable(schema)) {
schema = immutableToJS(schema)
}

return foldType(schema?.type)
}
2 changes: 0 additions & 2 deletions src/core/plugins/json-schema-2020-12-samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
mediaTypeAPI,
formatAPI,
mergeJsonSchema,
foldType,
} from "./fn/index"
import makeGetJsonSampleSchema from "./fn/get-json-sample-schema"
import makeGetYamlSampleSchema from "./fn/get-yaml-sample-schema"
Expand Down Expand Up @@ -42,7 +41,6 @@ const JSONSchema202012SamplesPlugin = ({ getSystem }) => {
getXmlSampleSchema,
getSampleSchema,
mergeJsonSchema,
foldType,
},
},
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/plugins/json-schema-2020-12/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @prettier
*/
import { List, Map } from "immutable"
import { immutableToJS, isImmutable } from "../../utils"

export const upperFirst = (value) => {
if (typeof value === "string") {
Expand Down Expand Up @@ -37,6 +38,10 @@ export const makeGetType = (fnAccessor) => {
return "any"
}

if (isImmutable(schema)) {
schema = immutableToJS(schema)
}

if (fn.isBooleanJSONSchema(schema)) {
return schema ? "any" : "never"
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugins/json-schema-2020-12/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
useLevel,
} from "./hooks"
import { withJSONSchemaContext, makeWithJSONSchemaSystemContext } from "./hoc"
import { getFoldType } from "../json-schema-2020-12-samples/fn/core/type"

const JSONSchema202012Plugin = ({ getSystem, fn }) => {
const fnAccessor = () => ({
Expand Down Expand Up @@ -133,6 +134,7 @@ const JSONSchema202012Plugin = ({ getSystem, fn }) => {
jsonSchema202012: {
getTitle: makeGetTitle(fnAccessor),
getType: makeGetType(fnAccessor),
foldType: getFoldType,
isExpandable: makeIsExpandable(fnAccessor),
isBooleanJSONSchema,
hasKeyword,
Expand Down
5 changes: 5 additions & 0 deletions src/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,8 @@ const resolver = (arg1, arg2, arg3) => [arg1, JSON.stringify(arg2), JSON.stringi
export const memoizedCreateXMLExample = memoizeN(createXMLExample, resolver)

export const memoizedSampleFromSchema = memoizeN(sampleFromSchema, resolver)

export const getSchemaObjectTypeLabel = (schema) => schema?.get("type")

export const getSchemaObjectType = (schema) => schema?.get("type") ?? "string"

4 changes: 4 additions & 0 deletions src/core/plugins/json-schema-5-samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
memoizedCreateXMLExample,
memoizedSampleFromSchema,
mergeJsonSchema,
getSchemaObjectType,
getSchemaObjectTypeLabel,
} from "./fn/index"
import makeGetJsonSampleSchema from "./fn/get-json-sample-schema"
import makeGetYamlSampleSchema from "./fn/get-yaml-sample-schema"
Expand Down Expand Up @@ -47,6 +49,8 @@ const JSONSchema5SamplesPlugin = ({ getSystem }) => {
getXmlSampleSchema,
getSampleSchema,
mergeJsonSchema,
getSchemaObjectTypeLabel,
getSchemaObjectType,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +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, isImmutable, immutableToJS } from "core/utils"

/* eslint-disable react/jsx-no-bind */
import { stringify, isImmutable } from "core/utils"

const noop = ()=> {}
const JsonSchemaPropShape = {
Expand Down Expand Up @@ -50,7 +48,7 @@ export class JsonSchemaForm extends Component {
let { schema, errors, value, onChange, getComponent, fn, disabled } = this.props
const format = schema && schema.get ? schema.get("format") : null
const type = schema && schema.get ? schema.get("type") : null
const foldedType = fn.jsonSchema202012.foldType(immutableToJS(type))
const objectTypeLabel = fn.getSchemaObjectTypeLabel(schema)
const isFileUploadIntended = fn.isFileUploadIntended(schema)

let getComponentSilently = (name) => getComponent(name, false, { failSilently: true })
Expand All @@ -59,7 +57,7 @@ export class JsonSchemaForm extends Component {
getComponentSilently(`JsonSchema_${type}`) :
getComponent("JsonSchema_string")

if (!isFileUploadIntended && List.isList(type) && (foldedType === "array" || foldedType === "object")) {
if (!isFileUploadIntended && List.isList(type) && (objectTypeLabel === "array" || objectTypeLabel === "object")) {
Comp = getComponent("JsonSchema_object")
}

Expand Down Expand Up @@ -192,23 +190,23 @@ export class JsonSchema_array extends PureComponent {
.map(e => e.error)
const value = this.state.value // expect Im List
const shouldRenderValue =
value && value.count && value.count() > 0 ? true : false
!!(value && value.count && value.count() > 0)
const schemaItemsEnum = schema.getIn(["items", "enum"])
const schemaItemsType = schema.getIn(["items", "type"])
const foldedSchemaItemsType = fn.jsonSchema202012.foldType(immutableToJS(schemaItemsType))
const schemaItemsTypeLabel = fn.jsonSchema202012.getType(immutableToJS(schema.get("items")))
const schemaItems = schema.get("items")
const objectTypeLabel = fn.getSchemaObjectTypeLabel(schemaItems)
const objectType = fn.getSchemaObjectType(schemaItems)
const schemaItemsFormat = schema.getIn(["items", "format"])
const schemaItemsSchema = schema.get("items")
let ArrayItemsComponent
let isArrayItemText = false
let isArrayItemFile = (schemaItemsType === "file" || (schemaItemsType === "string" && schemaItemsFormat === "binary")) ? true : false
if (schemaItemsType && schemaItemsFormat) {
ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}_${schemaItemsFormat}`)
} else if (schemaItemsType === "boolean" || schemaItemsType === "array" || schemaItemsType === "object") {
ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}`)
let isArrayItemFile = (objectTypeLabel === "file" || (objectTypeLabel === "string" && schemaItemsFormat === "binary"))
if (objectTypeLabel && schemaItemsFormat) {
ArrayItemsComponent = getComponent(`JsonSchema_${objectTypeLabel}_${schemaItemsFormat}`)
} else if (objectTypeLabel === "boolean" || objectTypeLabel === "array" || objectTypeLabel === "object") {
ArrayItemsComponent = getComponent(`JsonSchema_${objectTypeLabel}`)
}

if (List.isList(schemaItemsType) && (foldedSchemaItemsType === "array" || foldedSchemaItemsType === "object")) {
if (List.isList(objectTypeLabel) && (objectTypeLabel === "array" || objectTypeLabel === "object")) {
ArrayItemsComponent = getComponent(`JsonSchema_object`)
}

Expand Down Expand Up @@ -286,7 +284,7 @@ export class JsonSchema_array extends PureComponent {
title={arrayErrors.length ? arrayErrors : ""}
onClick={this.addItem}
>
Add {schemaItemsTypeLabel} item
Add {objectType} item
</Button>
) : null}
</div>
Expand Down
2 changes: 0 additions & 2 deletions src/core/plugins/json-schema-5/components/models.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React, { Component } from "react"
import Im, { Map } from "immutable"
import PropTypes from "prop-types"

/* eslint-disable react/jsx-no-bind */

export default class Models extends Component {
static propTypes = {
getComponent: PropTypes.func,
Expand Down
18 changes: 8 additions & 10 deletions src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import React from "react"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import { Map, OrderedMap, List, fromJS } from "immutable"
import { getCommonExtensions, stringify, isEmptyValue, immutableToJS } from "core/utils"
import { getCommonExtensions, stringify, isEmptyValue } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"

/* eslint-disable react/jsx-no-bind */

export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey, fn) => {
const mediaTypeValue = requestBody.getIn(["content", mediaType]) ?? OrderedMap()
const schema = mediaTypeValue.get("schema", OrderedMap()).toJS()
Expand Down Expand Up @@ -160,9 +158,9 @@ const RequestBody = ({

let commonExt = showCommonExtensions ? getCommonExtensions(schema) : null
const required = schemaForMediaType.get("required", List()).includes(key)
const typeLabel = fn.jsonSchema202012.getType(immutableToJS(schema))
const type = fn.jsonSchema202012.foldType(immutableToJS(schema?.get("type")))
const itemType = fn.jsonSchema202012.foldType(immutableToJS(schema?.getIn(["items", "type"])))
const objectType = fn.getSchemaObjectType(schema)
const objectTypeLabel = fn.getSchemaObjectTypeLabel(schema)
const itemTypeLabel = fn.getSchemaObjectTypeLabel(schema?.get("items"))
const format = schema.get("format")
const description = schema.get("description")
const currentValue = requestBodyValue.getIn([key, "value"])
Expand All @@ -181,11 +179,11 @@ const RequestBody = ({
initialValue = "0"
}

if (typeof initialValue !== "string" && type === "object") {
if (typeof initialValue !== "string" && objectTypeLabel === "object") {
initialValue = stringify(initialValue)
}

if (typeof initialValue === "string" && type === "array") {
if (typeof initialValue === "string" && objectTypeLabel === "array") {
initialValue = JSON.parse(initialValue)
}

Expand All @@ -212,7 +210,7 @@ const RequestBody = ({
{ !required ? null : <span>&nbsp;*</span> }
</div>
<div className="parameter__type">
{ typeLabel }
{ objectType }
{ format && <span className="prop-format">(${format})</span>}
{!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} />)}
</div>
Expand All @@ -223,7 +221,7 @@ const RequestBody = ({
<td className="parameters-col_description">
<Markdown source={ description }></Markdown>
{isExecute ? <div>
{(type === "object" || itemType === "object") ? (
{(objectTypeLabel === "object" || itemTypeLabel === "object") ? (
<ModelExample
getComponent={getComponent}
specPath={specPath.push("schema")}
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugins/oas31/after-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function afterLoad({ fn, getSystem }) {
getXmlSampleSchema: fn.jsonSchema202012.getXmlSampleSchema,
getSampleSchema: fn.jsonSchema202012.getSampleSchema,
mergeJsonSchema: fn.jsonSchema202012.mergeJsonSchema,
getSchemaObjectTypeLabel: fn.jsonSchema202012.foldType,
getSchemaObjectType: fn.jsonSchema202012.getType,
},
getSystem()
)
Expand Down
12 changes: 4 additions & 8 deletions test/unit/bugs/4557-default-parameter-values.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ describe("bug #4557: default parameter values", function () {
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
jsonSchema202012: {
foldType,
getType: makeGetType(() => ({ isBooleanJSONSchema })),
},
getSchemaObjectTypeLabel: foldType,
getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })),
},
})
const props = {
Expand Down Expand Up @@ -114,10 +112,8 @@ describe("bug #4557: default parameter values", function () {
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
mergeJsonSchema,
jsonSchema202012: {
foldType,
getType: makeGetType(() => ({ isBooleanJSONSchema })),
},
getSchemaObjectTypeLabel: foldType,
getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })),
},
})
const props = {
Expand Down
Loading