From 536f636c502d01daadc55edc75b744c4c09a248f Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Fri, 28 Feb 2025 14:17:01 +0100 Subject: [PATCH] refactor: introduce function for getting Schema Object type --- src/core/components/operation-tag.jsx | 2 - src/core/components/parameter-row.jsx | 18 ++++----- src/core/components/response.jsx | 5 +-- .../plugins/json-schema-5-samples/fn/index.js | 4 ++ .../plugins/json-schema-5-samples/index.js | 4 ++ .../components/json-schema-components.jsx | 14 +++---- .../json-schema-5/components/models.jsx | 2 - .../plugins/oas3/components/request-body.jsx | 18 ++++----- src/core/plugins/oas31/after-load.js | 2 + .../bugs/4557-default-parameter-values.jsx | 12 ++---- test/unit/components/parameter-row.jsx | 30 +++++--------- .../components/json-schema-form.jsx | 40 +++++-------------- 12 files changed, 59 insertions(+), 92 deletions(-) diff --git a/src/core/components/operation-tag.jsx b/src/core/components/operation-tag.jsx index fe9124bb4f3..f3400c526de 100644 --- a/src/core/components/operation-tag.jsx +++ b/src/core/components/operation-tag.jsx @@ -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 = { diff --git a/src/core/components/parameter-row.jsx b/src/core/components/parameter-row.jsx index ed099951963..f1f068645c5 100644 --- a/src/core/components/parameter-row.jsx +++ b/src/core/components/parameter-row.jsx @@ -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(immutableToJS(schema?.get("type"))) + const itemType = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.getIn(["items", "type"]))) if(initialValue !== undefined) { this.onChangeWrapper(initialValue) @@ -174,7 +174,7 @@ export default class ParameterRow extends Component { stringify(generatedSampleValue) ) ) - } + } else if ( type === "array" && itemType === "object" @@ -251,15 +251,15 @@ 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 typeLabel = fn.getSchemaObjectType(immutableToJS(schema)) + const type = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.get("type"))) + const itemType = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.getIn(["items", "type"]))) const isObject = !bodyParam && type === "object" const isArrayOfObjects = !bodyParam && itemType === "object" @@ -371,7 +371,7 @@ export default class ParameterRow extends Component { } { (isObject || isArrayOfObjects) ? ( - - ) : jsonSchemaForm + ) : jsonSchemaForm } { diff --git a/src/core/components/response.jsx b/src/core/components/response.jsx index 6cf5d3d1d89..7efbc4925f6 100644 --- a/src/core/components/response.jsx +++ b/src/core/components/response.jsx @@ -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 @@ -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) { diff --git a/src/core/plugins/json-schema-5-samples/fn/index.js b/src/core/plugins/json-schema-5-samples/fn/index.js index e1fcf8623fe..3e5d1a47ad1 100644 --- a/src/core/plugins/json-schema-5-samples/fn/index.js +++ b/src/core/plugins/json-schema-5-samples/fn/index.js @@ -635,3 +635,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 = (label) => label + +export const getSchemaObjectType = (schema) => schema.type ?? "any" diff --git a/src/core/plugins/json-schema-5-samples/index.js b/src/core/plugins/json-schema-5-samples/index.js index ca905c6484f..42895873537 100644 --- a/src/core/plugins/json-schema-5-samples/index.js +++ b/src/core/plugins/json-schema-5-samples/index.js @@ -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" @@ -47,6 +49,8 @@ const JSONSchema5SamplesPlugin = ({ getSystem }) => { getXmlSampleSchema, getSampleSchema, mergeJsonSchema, + getSchemaObjectTypeLabel, + getSchemaObjectType, }, } } diff --git a/src/core/plugins/json-schema-5/components/json-schema-components.jsx b/src/core/plugins/json-schema-5/components/json-schema-components.jsx index 1899ffcdf68..fc03579019b 100644 --- a/src/core/plugins/json-schema-5/components/json-schema-components.jsx +++ b/src/core/plugins/json-schema-5/components/json-schema-components.jsx @@ -6,8 +6,6 @@ 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 */ - const noop = ()=> {} const JsonSchemaPropShape = { getComponent: PropTypes.func.isRequired, @@ -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(immutableToJS(type)) let getComponentSilently = (name) => getComponent(name, false, { failSilently: true }) let Comp = type ? format ? @@ -58,7 +56,7 @@ export class JsonSchemaForm extends Component { getComponentSilently(`JsonSchema_${type}`) : getComponent("JsonSchema_string") - if (List.isList(type) && (foldedType === "array" || foldedType === "object")) { + if (List.isList(type) && (objectTypeLabel === "array" || objectTypeLabel === "object")) { Comp = getComponent("JsonSchema_object") } @@ -194,8 +192,8 @@ export class JsonSchema_array extends PureComponent { value && value.count && value.count() > 0 ? true : false 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 objectTypeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schemaItemsType)) + const objectType = fn.getSchemaObjectType(immutableToJS(schema.get("items"))) const schemaItemsFormat = schema.getIn(["items", "format"]) const schemaItemsSchema = schema.get("items") let ArrayItemsComponent @@ -207,7 +205,7 @@ export class JsonSchema_array extends PureComponent { ArrayItemsComponent = getComponent(`JsonSchema_${schemaItemsType}`) } - if (List.isList(schemaItemsType) && (foldedSchemaItemsType === "array" || foldedSchemaItemsType === "object")) { + if (List.isList(schemaItemsType) && (objectTypeLabel === "array" || objectTypeLabel === "object")) { ArrayItemsComponent = getComponent(`JsonSchema_object`) } @@ -285,7 +283,7 @@ export class JsonSchema_array extends PureComponent { title={arrayErrors.length ? arrayErrors : ""} onClick={this.addItem} > - Add {schemaItemsTypeLabel} item + Add {objectType} item ) : null} diff --git a/src/core/plugins/json-schema-5/components/models.jsx b/src/core/plugins/json-schema-5/components/models.jsx index e9023f36f3e..d0efe893444 100644 --- a/src/core/plugins/json-schema-5/components/models.jsx +++ b/src/core/plugins/json-schema-5/components/models.jsx @@ -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, diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index 7c80040be50..eec5b2ee306 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -5,8 +5,6 @@ import { Map, OrderedMap, List, fromJS } from "immutable" import { getCommonExtensions, stringify, isEmptyValue, immutableToJS } 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() @@ -161,9 +159,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(immutableToJS(schema)) + const objectTypeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.get("type"))) + const itemTypeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.getIn(["items", "type"]))) const format = schema.get("format") const description = schema.get("description") const currentValue = requestBodyValue.getIn([key, "value"]) @@ -182,15 +180,15 @@ 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) } - const isFile = type === "string" && (format === "binary" || format === "base64") + const isFile = objectTypeLabel === "string" && (format === "binary" || format === "base64") const jsonSchemaForm =  * }
- { typeLabel } + { objectType } { format && (${format})} {!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => )}
@@ -224,7 +222,7 @@ const RequestBody = ({ {isExecute ?
- {(type === "object" || itemType === "object") ? ( + {(objectTypeLabel === "object" || itemTypeLabel === "object") ? ( ({ isBooleanJSONSchema })), - }, + getSchemaObjectTypeLabel: foldType, + getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })), }, }) const props = { @@ -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 = { diff --git a/test/unit/components/parameter-row.jsx b/test/unit/components/parameter-row.jsx index 9a5f380b7df..b1683936db9 100644 --- a/test/unit/components/parameter-row.jsx +++ b/test/unit/components/parameter-row.jsx @@ -38,10 +38,8 @@ describe("", () => { getXmlSampleSchema: makeGetXmlSampleSchema(getSystem), getSampleSchema: makeGetSampleSchema(getSystem), mergeJsonSchema, - jsonSchema202012: { - foldType, - getType: makeGetType(() => ({ isBooleanJSONSchema })), - }, + getSchemaObjectTypeLabel: foldType, + getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })), }, oas3Selectors: { activeExamplesMember: () => {} }, getConfigs: () => ({}), @@ -182,10 +180,8 @@ describe("bug #5573: zero default and example values", function () { getYamlSampleSchema: makeGetYamlSampleSchema(getSystem), getXmlSampleSchema: makeGetXmlSampleSchema(getSystem), getSampleSchema: makeGetSampleSchema(getSystem), - jsonSchema202012: { - foldType, - getType: makeGetType(() => ({ isBooleanJSONSchema })), - }, + getSchemaObjectTypeLabel: foldType, + getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })), }, getConfigs: () => { return {} @@ -238,10 +234,8 @@ describe("bug #5573: zero default and example values", function () { getYamlSampleSchema: makeGetYamlSampleSchema(getSystem), getXmlSampleSchema: makeGetXmlSampleSchema(getSystem), getSampleSchema: makeGetSampleSchema(getSystem), - jsonSchema202012: { - foldType, - getType: makeGetType(() => ({ isBooleanJSONSchema })), - }, + getSchemaObjectTypeLabel: foldType, + getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })), }, }) const props = { @@ -296,10 +290,8 @@ describe("bug #5573: zero default and example values", function () { getXmlSampleSchema: makeGetXmlSampleSchema(getSystem), getSampleSchema: makeGetSampleSchema(getSystem), mergeJsonSchema, - jsonSchema202012: { - foldType, - getType: makeGetType(() => ({ isBooleanJSONSchema })), - }, + getSchemaObjectTypeLabel: foldType, + getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })), }, }) const props = { @@ -354,10 +346,8 @@ describe("bug #5573: zero default and example values", function () { getXmlSampleSchema: makeGetXmlSampleSchema(getSystem), getSampleSchema: makeGetSampleSchema(getSystem), mergeJsonSchema, - jsonSchema202012: { - foldType, - getType: makeGetType(() => ({ isBooleanJSONSchema })), - }, + getSchemaObjectTypeLabel: foldType, + getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })), }, }) const props = { diff --git a/test/unit/core/plugins/json-schema-5/components/json-schema-form.jsx b/test/unit/core/plugins/json-schema-5/components/json-schema-form.jsx index 73d0d2292a3..58a2a6bb57d 100644 --- a/test/unit/core/plugins/json-schema-5/components/json-schema-form.jsx +++ b/test/unit/core/plugins/json-schema-5/components/json-schema-form.jsx @@ -23,9 +23,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "string", @@ -50,9 +48,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "string", @@ -75,9 +71,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, required: true, schema: Immutable.fromJS({ @@ -103,9 +97,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "boolean" @@ -130,9 +122,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "boolean", @@ -157,9 +147,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "boolean", @@ -185,9 +173,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, required: true, schema: Immutable.fromJS({ @@ -216,9 +202,7 @@ describe("", function(){ }, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, errors: List(), schema: Immutable.fromJS({ @@ -249,9 +233,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "NotARealType" @@ -275,9 +257,7 @@ describe("", function(){ onChange: () => {}, keyName: "", fn: { - jsonSchema202012: { - foldType, - }, + getSchemaObjectTypeLabel: foldType, }, schema: Immutable.fromJS({ type: "NotARealType",