From fd9af162afa0d1dce47f968f484912da4cbcdd26 Mon Sep 17 00:00:00 2001 From: Adam Davis Date: Sun, 16 Jul 2023 13:51:26 -0400 Subject: [PATCH 1/3] fix(json-schema-2020-12): Fix OAS 3.1 additionalProperties false --- .../json-schema-2020-12/samples-extensions/fn/main.js | 5 ++++- 1 file changed, 4 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 983d6253434..523f5160597 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,10 @@ export const sampleFromSchemaGeneric = ( return res } - if (isBooleanJSONSchema(additionalProperties)) { + if ( + isBooleanJSONSchema(additionalProperties) && + additionalProperties === true + ) { if (respectXML) { res[displayName].push({ additionalProp: "Anything can be here" }) } else { From c989d71e16f9210a3ad388768714ac82841d0fbc Mon Sep 17 00:00:00 2001 From: Adam Davis Date: Sun, 16 Jul 2023 15:01:47 -0400 Subject: [PATCH 2/3] test: Add unit tests for additionalProperties for samples --- .../samples-extensions/fn.js | 80 +++++++++++++++++++ test/unit/core/plugins/samples/fn/index.js | 80 +++++++++++++++++++ 2 files changed, 160 insertions(+) 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 181173efe0f..936e3f9d0c4 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 11815f95fd8..d4de01d5542 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 = { From b9e293ac23c4f80d7886f5e538b695b8b945e724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Mon, 24 Jul 2023 09:52:29 +0200 Subject: [PATCH 3/3] Update main.js --- .../json-schema-2020-12/samples-extensions/fn/main.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 523f5160597..ee57f223ebd 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,10 +420,7 @@ export const sampleFromSchemaGeneric = ( return res } - if ( - isBooleanJSONSchema(additionalProperties) && - additionalProperties === true - ) { + if (isBooleanJSONSchema(additionalProperties) && additionalProperties) { if (respectXML) { res[displayName].push({ additionalProp: "Anything can be here" }) } else {