Skip to content

Commit aa33415

Browse files
committedApr 11, 2025
feat: support single type comparison
1 parent 0c10671 commit aa33415

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
lines changed
 

Diff for: ‎src/core/plugins/json-schema-2020-12/fn.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -505,18 +505,21 @@ export const makeGetExtensionKeywords = (fnAccessor) => {
505505
return getExtensionKeywords
506506
}
507507

508-
export const schemaHasType = (schema, types) => {
508+
export const hasSchemaType = (schema, type) => {
509509
const isSchemaImmutable = Map.isMap(schema)
510510

511511
if (!isSchemaImmutable && !isPlainObject(schema)) {
512512
return false
513513
}
514514

515-
const type = isSchemaImmutable ? schema.get("type") : schema.type
515+
const hasType = (schemaType) =>
516+
type === schemaType || (Array.isArray(type) && type.includes(schemaType))
516517

517-
if (List.isList(type) || Array.isArray(type)) {
518-
return type.some((t) => types.includes(t))
518+
const schemaType = isSchemaImmutable ? schema.get("type") : schema.type
519+
520+
if (List.isList(schemaType) || Array.isArray(schemaType)) {
521+
return schemaType.some((t) => hasType(t))
519522
}
520523

521-
return types.includes(type)
524+
return hasType(schemaType)
522525
}

Diff for: ‎src/core/plugins/json-schema-2020-12/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import {
5555
isBooleanJSONSchema,
5656
getSchemaKeywords,
5757
makeGetExtensionKeywords,
58-
schemaHasType,
58+
hasSchemaType,
5959
} from "./fn"
6060
import { JSONSchemaPathContext, JSONSchemaLevelContext } from "./context"
6161
import {
@@ -144,7 +144,7 @@ const JSONSchema202012Plugin = ({ getSystem, fn }) => {
144144
useLevel,
145145
getSchemaKeywords,
146146
getExtensionKeywords: makeGetExtensionKeywords(fnAccessor),
147-
schemaHasType,
147+
hasSchemaType,
148148
},
149149
},
150150
}

Diff for: ‎src/core/plugins/json-schema-5/fn.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import { Map } from "immutable"
55
import isPlainObject from "lodash/isPlainObject"
66

7-
export const schemaHasType = (schema, types) => {
7+
export const hasSchemaType = (schema, type) => {
88
const isSchemaImmutable = Map.isMap(schema)
99

1010
if (!isSchemaImmutable && !isPlainObject(schema)) {
1111
return false
1212
}
1313

14-
const type = isSchemaImmutable ? schema.get("type") : schema.type
14+
const schemaType = isSchemaImmutable ? schema.get("type") : schema.type
1515

16-
return types.includes(type)
16+
return (
17+
type === schemaType || (Array.isArray(type) && type.includes(schemaType))
18+
)
1719
}

Diff for: ‎src/core/plugins/json-schema-5/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Schemes from "./components/schemes"
1414
import SchemesContainer from "./containers/schemes"
1515
import * as JSONSchemaComponents from "./components/json-schema-components"
1616
import { ModelExtensions } from "./components/model-extensions"
17-
import { schemaHasType } from "./fn"
17+
import { hasSchemaType } from "./fn"
1818

1919
const JSONSchema5Plugin = () => ({
2020
components: {
@@ -33,7 +33,7 @@ const JSONSchema5Plugin = () => ({
3333
...JSONSchemaComponents,
3434
},
3535
fn: {
36-
schemaHasType,
36+
hasSchemaType,
3737
},
3838
})
3939

Diff for: ‎src/core/plugins/oas3/components/request-body.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const RequestBody = ({
129129
return null
130130
}
131131

132-
const isObjectContent = fn.schemaHasType(mediaTypeValue.get("schema"), ["object"])
132+
const isObjectContent = fn.hasSchemaType(mediaTypeValue.get("schema"), "object")
133133

134134
if (
135135
isObjectContent &&

Diff for: ‎src/core/plugins/oas3/fn.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export const makeIsFileUploadIntended = (getSystem) => {
2727
const format = isSchemaImmutable ? schema.get("format") : schema.format
2828

2929
return (
30-
fn.schemaHasType(schema, ["string"]) &&
31-
["binary", "byte"].includes(format)
30+
fn.hasSchemaType(schema, "string") && ["binary", "byte"].includes(format)
3231
)
3332
}
3433

Diff for: ‎src/core/plugins/oas31/after-load.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,26 @@ function afterLoad({ fn, getSystem }) {
4242

4343
// overrides behavior in OpenAPI 3.1.x, recognizes more intentions
4444
const isFileUploadIntended = makeIsFileUploadIntended(getSystem)
45-
const { isFileUploadIntended: isFileUploadIntendedWrap, schemaHasType } =
46-
wrapOAS31Fn(
45+
const { isFileUploadIntended: isFileUploadIntendedWrap } = wrapOAS31Fn(
46+
{
47+
isFileUploadIntended,
48+
},
49+
getSystem()
50+
)
51+
52+
this.fn.isFileUploadIntended = isFileUploadIntendedWrap
53+
this.fn.isFileUploadIntendedOAS31 = isFileUploadIntended
54+
55+
if (fn.jsonSchema202012) {
56+
const { hasSchemaType } = wrapOAS31Fn(
4757
{
48-
isFileUploadIntended,
49-
...(fn.jsonSchema202012 && {
50-
schemaHasType: fn.jsonSchema202012.schemaHasType,
51-
}),
58+
hasSchemaType: fn.jsonSchema202012.hasSchemaType,
5259
},
5360
getSystem()
5461
)
5562

56-
this.fn.isFileUploadIntended = isFileUploadIntendedWrap
57-
this.fn.isFileUploadIntendedOAS31 = isFileUploadIntended
58-
this.fn.schemaHasType = schemaHasType
63+
this.fn.hasSchemaType = hasSchemaType
64+
}
5965
}
6066

6167
export default afterLoad

Diff for: ‎test/unit/core/plugins/json-schema-5/components/json-schema-form.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const getSystemStub = () => ({
1919
fileUploadMediaTypes: [],
2020
}),
2121
fn: {
22-
schemaHasType: () => {},
22+
hasSchemaType: () => {},
2323
isFileUploadIntendedOAS30: () => {},
2424
},
2525
})

0 commit comments

Comments
 (0)