From f1aab53dd60d131c1cb9b34ad2fc8f7f85c89ab6 Mon Sep 17 00:00:00 2001 From: Helen Kosova Date: Wed, 15 Sep 2021 15:54:26 +0300 Subject: [PATCH] fix(sample-gen): generate the correct number of properties (#7432) This commit fixes correct number of additionalProperties when minProperties is used. --- src/core/plugins/samples/fn.js | 4 ++-- test/unit/core/plugins/samples/fn.js | 31 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index cfcc9375..aec6f28d 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -480,8 +480,8 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und } else { const toGenerateCount = schema.minProperties !== null && schema.minProperties !== undefined && propertyAddedCounter < schema.minProperties ? schema.minProperties - propertyAddedCounter - : 4 - for (let i = 1; i < toGenerateCount; i++) { + : 3 + for (let i = 1; i <= toGenerateCount; i++) { if(hasExceededMaxProperties()) { return res } diff --git a/test/unit/core/plugins/samples/fn.js b/test/unit/core/plugins/samples/fn.js index 47e7722d..7166db8d 100644 --- a/test/unit/core/plugins/samples/fn.js +++ b/test/unit/core/plugins/samples/fn.js @@ -789,6 +789,23 @@ describe("sampleFromSchema", () => { }) it("should handle minProperties in conjunction with additionalProperties", () => { + const definition = { + type: "object", + minProperties: 2, + additionalProperties: { + type: "string" + } + } + + const expected = { + additionalProp1: "string", + additionalProp2: "string" + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("should handle minProperties in conjunction with properties and additionalProperties", () => { const definition = { type: "object", minProperties: 2, @@ -854,6 +871,20 @@ describe("sampleFromSchema", () => { expect(sampleFromSchema(definition)).toEqual(expected) }) + it("should handle maxProperties in conjunction with additionalProperties", () => { + const definition = { + type: "object", + maxProperties: 1, + additionalProperties: true + } + + const expected = { + additionalProp1: {} + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + it("should handle maxProperties in conjunction with anyOf", () => { const definition = { type: "object",