fix(sample-gen): generate the correct number of properties (#7432)
This commit fixes correct number of additionalProperties when minProperties is used.
This commit is contained in:
@@ -480,8 +480,8 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
|
|||||||
} else {
|
} else {
|
||||||
const toGenerateCount = schema.minProperties !== null && schema.minProperties !== undefined && propertyAddedCounter < schema.minProperties
|
const toGenerateCount = schema.minProperties !== null && schema.minProperties !== undefined && propertyAddedCounter < schema.minProperties
|
||||||
? schema.minProperties - propertyAddedCounter
|
? schema.minProperties - propertyAddedCounter
|
||||||
: 4
|
: 3
|
||||||
for (let i = 1; i < toGenerateCount; i++) {
|
for (let i = 1; i <= toGenerateCount; i++) {
|
||||||
if(hasExceededMaxProperties()) {
|
if(hasExceededMaxProperties()) {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -789,6 +789,23 @@ describe("sampleFromSchema", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should handle minProperties in conjunction with additionalProperties", () => {
|
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 = {
|
const definition = {
|
||||||
type: "object",
|
type: "object",
|
||||||
minProperties: 2,
|
minProperties: 2,
|
||||||
@@ -854,6 +871,20 @@ describe("sampleFromSchema", () => {
|
|||||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
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", () => {
|
it("should handle maxProperties in conjunction with anyOf", () => {
|
||||||
const definition = {
|
const definition = {
|
||||||
type: "object",
|
type: "object",
|
||||||
|
|||||||
Reference in New Issue
Block a user