feat: sample gen should incorporate schema validation constraint (#7043)

* feat(sample-gen): handle minProperties, maxProperties

* fix(sample-gen): lift required

* feat(sample-gen): handle minimum, maximum (+ exclusive)

* feat(sample-gen): handle minLength, maxLength
This commit is contained in:
Mahtis Michel
2021-03-10 22:19:45 +01:00
committed by GitHub
parent 902241cf7e
commit 3ead825287
2 changed files with 474 additions and 20 deletions

View File

@@ -768,6 +768,306 @@ describe("sampleFromSchema", () => {
true
]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should ignore minProperties if cannot extend object", () => {
const definition = {
type: "object",
minProperties: 2,
properties: {
foo: {
type: "string"
}
}
}
const expected = {
foo: "string"
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minProperties in conjunction with additionalProperties", () => {
const definition = {
type: "object",
minProperties: 2,
additionalProperties: true,
properties: {
foo: {
type: "string"
}
}
}
const expected = {
foo: "string",
additionalProp1: {}
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minProperties in conjunction with additionalProperties and anyOf", () => {
const definition = {
type: "object",
minProperties: 2,
additionalProperties: true,
anyOf: [
{
type: "object",
properties: {
foo: {
type: "string"
}
}
}
]
}
const expected = {
foo: "string",
additionalProp1: {}
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maxProperties", () => {
const definition = {
type: "object",
maxProperties: 1,
properties: {
foo: {
type: "string"
},
swaggerUi: {
type: "string"
}
}
}
const expected = {
foo: "string"
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maxProperties in conjunction with anyOf", () => {
const definition = {
type: "object",
maxProperties: 1,
anyOf: [
{
type: "object",
properties: {
foo: {
type: "string"
},
swaggerUi: {
type: "string"
}
}
}
]
}
const expected = {
foo: "string"
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle handle maxProperties in conjunction with required", () => {
const definition = {
type: "object",
maxProperties: 1,
required: ["swaggerUi"],
properties: {
foo: {
type: "string"
},
swaggerUi: {
type: "string",
example: "<3"
}
}
}
const expected = {
swaggerUi: "<3",
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle handle maxProperties in conjunction with anyOf required", () => {
const definition = {
type: "object",
maxProperties: 1,
required: ["swaggerUi"],
anyOf: [
{
type: "object",
properties: {
foo: {
type: "string"
},
swaggerUi: {
type: "string",
example: "<3"
}
}
}
],
}
const expected = {
swaggerUi: "<3",
}
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minItems", () => {
const definition = {
type: "array",
minItems: 2,
items: {
type: "string"
}
}
const expected = ["string", "string"]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minItems with example", () => {
const definition = {
type: "array",
minItems: 2,
items: {
type: "string",
example: "some"
},
}
const expected = ["some", "some"]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minItems in conjunction with oneOf", () => {
const definition = {
type: "array",
minItems: 4,
items: {
oneOf: [
{
type: "string"
},
{
type: "number"
}
]
}
}
const expected = ["string", 0, "string", 0]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maxItems in conjunction with multiple oneOf", () => {
const definition = {
type: "array",
maxItems: 1,
items: {
oneOf: [
{
type: "string"
},
{
type: "number"
}
]
}
}
const expected = ["string"]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minimum", () => {
const definition = {
type: "number",
minimum: 5,
}
const expected = 5
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minimum with exclusive", () => {
const definition = {
type: "number",
minimum: 5,
exclusiveMinimum: true,
}
const expected = 6
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maximum", () => {
const definition = {
type: "number",
maximum: -1,
}
const expected = -1
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maximum with exclusive", () => {
const definition = {
type: "number",
maximum: -1,
exclusiveMaximum: true,
}
const expected = -2
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle minLength", () => {
const definition = {
type: "string",
minLength: 7
}
const expected = "strings"
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("should handle maxLength", () => {
const definition = {
type: "string",
maxLength: 3
}
const expected = "str"
expect(sampleFromSchema(definition)).toEqual(expected)
})
})
@@ -1794,6 +2094,33 @@ describe("createXMLExample", function () {
expect(sut(definition, {}, { test: "probe" })).toEqual(expected)
})
})
it("should handle handle maxProperties in conjunction with required", function () {
const definition = {
type: "object",
maxProperties: 1,
required: ["swaggerUi"],
xml: {
name: "probe"
},
properties: {
foo: {
type: "string"
},
swaggerUi: {
type: "string",
example: "cool"
}
}
}
const expected = `<?xml version="1.0" encoding="UTF-8"?>
<probe>
\t<swaggerUi>cool</swaggerUi>
</probe>`
expect(sut(definition)).toEqual(expected)
})
})
describe("memoizedSampleFromSchema", () => {