From bdad2fe83db0c5cc28b9ba5e475e18ef529b8b56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Tue, 6 Jun 2023 11:09:10 +0200 Subject: [PATCH] feat(samples): add support for const keyword (#8884) This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577 --- .../samples-extensions/fn.js | 6 +++++- .../samples-extensions/fn.js | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js b/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js index 2af72f7603f..3f206c03435 100644 --- a/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js +++ b/src/core/plugins/json-schema-2020-12/samples-extensions/fn.js @@ -101,6 +101,7 @@ const liftSampleHelper = (oldSchema, target, config = {}) => { "enum", "xml", "type", + "const", ...objectContracts, ...arrayContracts, ...numberContracts, @@ -683,7 +684,10 @@ export const sampleFromSchemaGeneric = ( } let value - if (schema && Array.isArray(schema.enum)) { + if (typeof schema?.const !== "undefined") { + // display const value + value = schema.const + } else if (schema && Array.isArray(schema.enum)) { //display enum first value value = normalizeArray(schema.enum)[0] } else if (schema) { diff --git a/test/unit/core/plugins/json-schema-2020-12/samples-extensions/fn.js b/test/unit/core/plugins/json-schema-2020-12/samples-extensions/fn.js index 1188a0077e1..57953d763e9 100644 --- a/test/unit/core/plugins/json-schema-2020-12/samples-extensions/fn.js +++ b/test/unit/core/plugins/json-schema-2020-12/samples-extensions/fn.js @@ -86,32 +86,33 @@ describe("sampleFromSchema", () => { }) it("should handle type keyword defined as list of types", function () { - const definition = fromJS({ - type: ["object", "string"], - }) + const definition = fromJS({ type: ["object", "string"] }) const expected = {} expect(sampleFromSchema(definition)).toEqual(expected) }) it("should prioritize array when array and object defined as list of types", function () { - const definition = fromJS({ - type: ["object", "array"], - }) + const definition = fromJS({ type: ["object", "array"] }) const expected = [] expect(sampleFromSchema(definition)).toEqual(expected) }) it("should handle primitive types defined as list of types", function () { - const definition = fromJS({ - type: ["string", "number"], - }) + const definition = fromJS({ type: ["string", "number"] }) const expected = "string" expect(sampleFromSchema(definition)).toEqual(expected) }) + it("should return const value", function () { + const definition = fromJS({ const: 3 }) + const expected = 3 + + expect(sampleFromSchema(definition)).toStrictEqual(expected) + }) + it("handles Immutable.js objects for nested schemas", function () { const definition = fromJS({ type: "object",