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

refactor: introduce function for getting Schema Object type #10330

Open
wants to merge 1 commit 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
18 changes: 9 additions & 9 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
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(immutableToJS(schema?.get("type")))
const itemType = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.getIn(["items", "type"])))

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,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))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit confusing. For getting a typeLabel we're using getSchemaObjectType. For actual type we're using getSchemaObjectTypeLabel. Shouldn't it be reversed?

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"

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
4 changes: 4 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,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
Copy link
Member Author

@char0n char0n Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input is type not a label.

Suggested change
export const getSchemaObjectTypeLabel = (label) => label
export const getSchemaObjectTypeLabel = (type) => type

But anyway we can make this point-free by:

import identity from "lodash/identity"

export const getSchemaObjectTypeLabel = identity


export const getSchemaObjectType = (schema) => schema.type ?? "any"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with JSON Schema 2020-12 processing, can we make the fallback type the string type?

Suggested change
export const getSchemaObjectType = (schema) => schema.type ?? "any"
export const getSchemaObjectType = (schema) => schema.type ?? "string"

Also, may I suggest to make this function safe:

Suggested change
export const getSchemaObjectType = (schema) => schema.type ?? "any"
export const getSchemaObjectType = (schema) => schema?.type ?? "string"

Copy link
Member Author

@char0n char0n Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferred alternative: What if both the functions would have the following signature?

getSchemaObjectType: (schema: object) -> string
getSchemaObjectTypeLabel: (schema: object) -> string

This would simplify their usage inside the code as it is now abstracted how the the type or label are computed from the schema.

Instead of

const typeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.get("type")))

you'll end up having

const typeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schema))

If you further abstract and allow the functions to consume immutable along with POJO, it can look like:

const typeLabel = fn.getSchemaObjectTypeLabel(schema)

NOTE: this applies both for Draft 5 and 2020-12 functions.

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 @@ -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,
Expand Down Expand Up @@ -50,15 +48,15 @@ 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 ?
getComponentSilently(`JsonSchema_${type}_${format}`) :
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")
}

Expand Down Expand Up @@ -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
Expand All @@ -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`)
}

Expand Down Expand Up @@ -285,7 +283,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 @@ -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()
Expand Down Expand Up @@ -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"])
Expand All @@ -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 = <JsonSchemaForm
fn={fn}
Expand All @@ -213,7 +211,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 @@ -224,7 +222,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 @@ -32,6 +32,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
30 changes: 10 additions & 20 deletions test/unit/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ describe("<ParameterRow/>", () => {
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
mergeJsonSchema,
jsonSchema202012: {
foldType,
getType: makeGetType(() => ({ isBooleanJSONSchema })),
},
getSchemaObjectTypeLabel: foldType,
getSchemaObjectType: makeGetType(() => ({ isBooleanJSONSchema })),
},
oas3Selectors: { activeExamplesMember: () => {} },
getConfigs: () => ({}),
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
Loading