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

Merged
merged 17 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 11 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
31 changes: 15 additions & 16 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,13 +152,13 @@ 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 schemaObjectType = fn.getSchemaObjectType(schema)
const schemaItemsType = fn.getSchemaObjectType(schema?.get("items"))

if(initialValue !== undefined) {
this.onChangeWrapper(initialValue)
} else if(
type === "object"
schemaObjectType === "object"
&& generatedSampleValue
&& !paramWithMeta.get("examples")
) {
Expand All @@ -174,10 +174,10 @@ export default class ParameterRow extends Component {
stringify(generatedSampleValue)
)
)
}
}
else if (
type === "array"
&& itemType === "object"
schemaObjectType === "array"
&& schemaItemsType === "object"
&& generatedSampleValue
&& !paramWithMeta.get("examples")
) {
Expand Down Expand Up @@ -251,17 +251,16 @@ 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 schemaObjectType = fn.getSchemaObjectType(schema)
const schemaItemsType = fn.getSchemaObjectType(schema?.get("items"))
const isObject = !bodyParam && schemaObjectType === "object"
const isArrayOfObjects = !bodyParam && schemaItemsType === "object"

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

{ (isObject || isArrayOfObjects) ? (
<ModelExample
<ModelExample
getComponent={getComponent}
specPath={specPath.push("schema")}
getConfigs={getConfigs}
Expand All @@ -380,7 +379,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
4 changes: 3 additions & 1 deletion src/core/plugins/json-schema-2020-12-samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import makeGetJsonSampleSchema from "./fn/get-json-sample-schema"
import makeGetYamlSampleSchema from "./fn/get-yaml-sample-schema"
import makeGetXmlSampleSchema from "./fn/get-xml-sample-schema"
import makeGetSampleSchema from "./fn/get-sample-schema"
import { immutableToJS } from "../../utils"

const JSONSchema202012SamplesPlugin = ({ getSystem }) => {
const getJsonSampleSchema = makeGetJsonSampleSchema(getSystem)
const getYamlSampleSchema = makeGetYamlSampleSchema(getSystem)
const getXmlSampleSchema = makeGetXmlSampleSchema(getSystem)
const getSampleSchema = makeGetSampleSchema(getSystem)
const makeFoldType = (schema) => foldType(immutableToJS(schema)?.type)

return {
fn: {
Expand All @@ -42,7 +44,7 @@ const JSONSchema202012SamplesPlugin = ({ getSystem }) => {
getXmlSampleSchema,
getSampleSchema,
mergeJsonSchema,
foldType,
foldType: makeFoldType,
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion 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 } from "core/utils"

export const upperFirst = (value) => {
if (typeof value === "string") {
Expand Down Expand Up @@ -164,7 +165,7 @@ export const makeGetType = (fnAccessor) => {
return combinedStrings || "any"
}

return getType
return (schema, ...rest) => getType(immutableToJS(schema), ...rest)
}

export const isBooleanJSONSchema = (schema) => typeof schema === "boolean"
Expand Down
38 changes: 37 additions & 1 deletion src/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import XML from "xml"
import RandExp from "randexp"
import isEmpty from "lodash/isEmpty"
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
import { deeplyStripKey, isFunc, normalizeArray, objectify } from "core/utils"
import memoizeN from "core/utils/memoizeN"
import { immutableToJS } from "../../../utils"

const generateStringFromRegex = (pattern) => {
try {
Expand Down Expand Up @@ -627,6 +628,37 @@ export const createXMLExample = (schema, config, o) => {
return XML(json, { declaration: true, indent: "\t" })
}

const getType = (schema, processedSchemas = new WeakSet()) => {
if (schema == null) {
return "any"
}

if (processedSchemas.has(schema)) {
return "any" // detect a cycle
}

processedSchemas.add(schema)

const { type, items } = schema

const getArrayType = () => {
if (items) {
const itemsType = getType(items, processedSchemas)
return `array<${itemsType}>`
} else {
return "array<any>"
}
}

if (
Object.hasOwn(schema, "items")
) {
return getArrayType()
}
return type
}


export const sampleFromSchema = (schema, config, o) =>
sampleFromSchemaGeneric(schema, config, o, false)

Expand All @@ -635,3 +667,7 @@ 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) => getType(immutableToJS(schema))

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 objectType = fn.getSchemaObjectType(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) && (objectType === "array" || objectType === "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 schemaItemsType = fn.getSchemaObjectType(schemaItems)
const schemaItemsTypeLabel = fn.getSchemaObjectTypeLabel(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
let isArrayItemFile = (schemaItemsType === "file" || (schemaItemsType === "string" && schemaItemsFormat === "binary"))
if (schemaItemsType && schemaItemsFormat) {
ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}_${schemaItemsFormat}`)
} else if (schemaItemsType === "boolean" || schemaItemsType === "array" || schemaItemsType === "object") {
ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}`)
}

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

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 schemaItemsType = fn.getSchemaObjectType(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" && objectType === "object") {
initialValue = stringify(initialValue)
}

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

Expand All @@ -212,7 +210,7 @@ const RequestBody = ({
{ !required ? null : <span>&nbsp;*</span> }
</div>
<div className="parameter__type">
{ typeLabel }
{ objectTypeLabel }
{ 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") ? (
{(objectType === "object" || schemaItemsType === "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.getType,
getSchemaObjectType: fn.jsonSchema202012.foldType,
},
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