Skip to content

Commit

Permalink
fix(sample-gen): generate the correct number of properties (#7432)
Browse files Browse the repository at this point in the history
This commit fixes correct number of additionalProperties when minProperties is used.
  • Loading branch information
hkosova authored Sep 15, 2021
1 parent cc700f0 commit f1aab53
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit f1aab53

Please sign in to comment.