Skip to content

Commit

Permalink
fix(samples): fix handling of additionalProperties in JSON Schema 202…
Browse files Browse the repository at this point in the history
…0-12 (#9023)

This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0.

Refs #9022
  • Loading branch information
adavis444 authored Jul 24, 2023
1 parent 36583bc commit d9375db
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export const sampleFromSchemaGeneric = (
return res
}

if (isBooleanJSONSchema(additionalProperties)) {
if (isBooleanJSONSchema(additionalProperties) && additionalProperties) {
if (respectXML) {
res[displayName].push({ additionalProp: "Anything can be here" })
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -2618,6 +2679,25 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props =false", function () {
let expected =
'<?xml version="1.0" encoding="UTF-8"?>\n<animals>\n\t<dog>string</dog>\n</animals>'
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 =
'<?xml version="1.0" encoding="UTF-8"?>\n<aliens>\n\t<alien>string</alien>\n\t<dog>0</dog>\n</aliens>'
Expand Down
80 changes: 80 additions & 0 deletions test/unit/core/plugins/samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -2149,6 +2211,24 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props =false", function () {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n</animals>"
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<alien>string</alien>\n\t<dog>0</dog>\n</aliens>"
let definition = {
Expand Down

0 comments on commit d9375db

Please sign in to comment.