diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js b/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js index 7f71364a..84315b3d 100644 --- a/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js +++ b/src/core/plugins/json-schema-2020-12-samples/fn/core/type.js @@ -67,7 +67,10 @@ export const foldType = (type) => { } else if (type.includes("object")) { return "object" } else { - const pickedType = randomPick(type) + const notNullTypes = type.filter((t) => t !== "null") + const pickedType = randomPick( + notNullTypes.length > 0 ? notNullTypes : type + ) if (ALL_TYPES.includes(pickedType)) { return pickedType } diff --git a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js index a5ff7fc1..e2a3e88d 100644 --- a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js +++ b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js @@ -373,6 +373,20 @@ describe("sampleFromSchema", () => { expect(sampleFromSchema(definition)).toEqual(expected) }) + it("should handle nullable primitive types defined as list of types", function () { + const sample = (schema) => sampleFromSchema(fromJS(schema)) + + expect(sample({ type: ["string", "null"] })).toStrictEqual("string") + expect(sample({ type: ["null", "string"] })).toStrictEqual("string") + expect(sample({ type: ["number", "null"] })).toStrictEqual(0) + expect(sample({ type: ["null", "number"] })).toStrictEqual(0) + expect(sample({ type: ["integer", "null"] })).toStrictEqual(0) + expect(sample({ type: ["null", "integer"] })).toStrictEqual(0) + expect(sample({ type: ["boolean", "null"] })).toStrictEqual(true) + expect(sample({ type: ["null", "boolean"] })).toStrictEqual(true) + expect(sample({ type: ["null"] })).toStrictEqual(null) + }) + it("should return const value", function () { const definition = fromJS({ const: 3 }) const expected = 3