From d9375dbddff45d04a22effa40ca638beba1c94ea Mon Sep 17 00:00:00 2001 From: Adam Davis Date: Mon, 24 Jul 2023 04:04:00 -0400 Subject: [PATCH] fix(samples): fix handling of additionalProperties in JSON Schema 2020-12 (#9023) This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #9022 --- .../samples-extensions/fn/main.js | 2 +- .../samples-extensions/fn.js | 80 +++++++++++++++++++ test/unit/core/plugins/samples/fn/index.js | 80 +++++++++++++++++++ 3 files changed, 161 insertions(+), 1 deletion(-) diff --git a/src/core/plugins/json-schema-2020-12/samples-extensions/fn/main.js b/src/core/plugins/json-schema-2020-12/samples-extensions/fn/main.js index 983d6253..ee57f223 100644 --- a/src/core/plugins/json-schema-2020-12/samples-extensions/fn/main.js +++ b/src/core/plugins/json-schema-2020-12/samples-extensions/fn/main.js @@ -420,7 +420,7 @@ export const sampleFromSchemaGeneric = ( return res } - if (isBooleanJSONSchema(additionalProperties)) { + if (isBooleanJSONSchema(additionalProperties) && additionalProperties) { if (respectXML) { res[displayName].push({ additionalProp: "Anything can be here" }) } else { 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 181173ef..936e3f9d 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 @@ -1189,6 +1189,67 @@ describe("sampleFromSchema", () => { expect(sampleFromSchema(definition)).toEqual(expected) }) + + it("should handle additionalProperties", () => { + const definition = { + type: "object", + additionalProperties: { + type: "string", + }, + properties: { + foo: { + type: "string", + }, + }, + } + + const expected = { + foo: "string", + additionalProp1: "string", + additionalProp2: "string", + additionalProp3: "string", + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should handle additionalProperties=true", () => { + const definition = { + type: "object", + additionalProperties: true, + properties: { + foo: { + type: "string", + }, + }, + } + + const expected = { + foo: "string", + additionalProp1: {}, + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should handle additionalProperties=false", () => { + const definition = { + type: "object", + additionalProperties: false, + properties: { + foo: { + type: "string", + }, + }, + } + + const expected = { + foo: "string", + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + it("should ignore minProperties if cannot extend object", () => { const definition = { type: "object", @@ -2618,6 +2679,25 @@ describe("createXMLExample", function () { expect(sut(definition)).toEqual(expected) }) + it("returns object with additional props =false", function () { + let expected = + '\n\n\tstring\n' + let definition = { + type: "object", + properties: { + dog: { + type: "string", + }, + }, + additionalProperties: false, + xml: { + name: "animals", + }, + } + + expect(sut(definition)).toEqual(expected) + }) + it("returns object with 2 properties with no type passed but properties", function () { const expected = '\n\n\tstring\n\t0\n' diff --git a/test/unit/core/plugins/samples/fn/index.js b/test/unit/core/plugins/samples/fn/index.js index 11815f95..d4de01d5 100644 --- a/test/unit/core/plugins/samples/fn/index.js +++ b/test/unit/core/plugins/samples/fn/index.js @@ -914,6 +914,68 @@ describe("sampleFromSchema", () => { expect(sampleFromSchema(definition)).toEqual(expected) }) + + + it("should handle additionalProperties", () => { + const definition = { + type: "object", + additionalProperties: { + type: "string", + }, + properties: { + foo: { + type: "string", + }, + }, + } + + const expected = { + foo: "string", + additionalProp1: "string", + additionalProp2: "string", + additionalProp3: "string", + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should handle additionalProperties=true", () => { + const definition = { + type: "object", + additionalProperties: true, + properties: { + foo: { + type: "string", + }, + }, + } + + const expected = { + foo: "string", + additionalProp1: {}, + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should handle additionalProperties=false", () => { + const definition = { + type: "object", + additionalProperties: false, + properties: { + foo: { + type: "string", + }, + }, + } + + const expected = { + foo: "string", + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + it("should ignore minProperties if cannot extend object", () => { const definition = { type: "object", @@ -2149,6 +2211,24 @@ describe("createXMLExample", function () { expect(sut(definition)).toEqual(expected) }) + it("returns object with additional props =false", function () { + let expected = "\n\n\tstring\n" + let definition = { + type: "object", + properties: { + dog: { + type: "string" + } + }, + additionalProperties: false, + xml: { + name: "animals" + } + } + + expect(sut(definition)).toEqual(expected) + }) + it("returns object with 2 properties with no type passed but properties", function () { let expected = "\n\n\tstring\n\t0\n" let definition = {