Skip to content

Commit d375f50

Browse files
authored
fix(json-schema-2020-12-samples): fix examples for nullable primitive types defined as list of types (#10390)
1 parent ac4b549 commit d375f50

File tree

2 files changed

+18
-1
lines changed
  • src/core/plugins/json-schema-2020-12-samples/fn/core
  • test/unit/core/plugins/json-schema-2020-12-samples

2 files changed

+18
-1
lines changed

src/core/plugins/json-schema-2020-12-samples/fn/core/type.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ export const foldType = (type) => {
6767
} else if (type.includes("object")) {
6868
return "object"
6969
} else {
70-
const pickedType = randomPick(type)
70+
const notNullTypes = type.filter((t) => t !== "null")
71+
const pickedType = randomPick(
72+
notNullTypes.length > 0 ? notNullTypes : type
73+
)
7174
if (ALL_TYPES.includes(pickedType)) {
7275
return pickedType
7376
}

test/unit/core/plugins/json-schema-2020-12-samples/fn.js

+14
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,20 @@ describe("sampleFromSchema", () => {
373373
expect(sampleFromSchema(definition)).toEqual(expected)
374374
})
375375

376+
it("should handle nullable primitive types defined as list of types", function () {
377+
const sample = (schema) => sampleFromSchema(fromJS(schema))
378+
379+
expect(sample({ type: ["string", "null"] })).toStrictEqual("string")
380+
expect(sample({ type: ["null", "string"] })).toStrictEqual("string")
381+
expect(sample({ type: ["number", "null"] })).toStrictEqual(0)
382+
expect(sample({ type: ["null", "number"] })).toStrictEqual(0)
383+
expect(sample({ type: ["integer", "null"] })).toStrictEqual(0)
384+
expect(sample({ type: ["null", "integer"] })).toStrictEqual(0)
385+
expect(sample({ type: ["boolean", "null"] })).toStrictEqual(true)
386+
expect(sample({ type: ["null", "boolean"] })).toStrictEqual(true)
387+
expect(sample({ type: ["null"] })).toStrictEqual(null)
388+
})
389+
376390
it("should return const value", function () {
377391
const definition = fromJS({ const: 3 })
378392
const expected = 3

0 commit comments

Comments
 (0)