Skip to content

Commit

Permalink
fix(sample-gen): should render additionalProperties in example (#6821)
Browse files Browse the repository at this point in the history
* fix(sample-gen): should render additionalProperties in example

* test(sample-gen): should return additional property from example
for object and array schemas
  • Loading branch information
mathis-m committed Jan 13, 2021
1 parent 99f93a5 commit 35cb925
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
let addPropertyToResult
if(respectXML) {
addPropertyToResult = (propName, overrideE = undefined) => {

if(schema) {
if(schema && props[propName]) {
// case it is an xml attribute
props[propName].xml = props[propName].xml || {}

Expand All @@ -168,6 +167,13 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
return
}
props[propName].xml.name = props[propName].xml.name || propName
} else if(!props[propName] && additionalProperties !== false) {
// case only additionalProperty that is not defined in schema
props[propName] = {
xml: {
name: propName
}
}
}

let t = sampleFromSchemaGeneric(schema && props[propName] || undefined, config, overrideE, respectXML)
Expand Down
41 changes: 41 additions & 0 deletions test/unit/core/plugins/samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,28 @@ describe("createXMLExample", function () {

expect(sut(definition)).toEqual(expected)
})
it("should return additionalProperty example", () => {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<notalien>test</notalien>\n</aliens>"
let definition = {
type: "array",
items: {
type: "object",
properties: {
alien: {
type: "string"
},
dog: {
type: "integer"
}
}
},
xml: {
name: "aliens"
}
}

expect(sut(definition, {}, [{ notalien: "test" }])).toEqual(expected)
})
})

describe("object", function () {
Expand Down Expand Up @@ -1521,6 +1542,26 @@ describe("createXMLExample", function () {

expect(sut(definition, {}, overrideExample)).toEqual(expected)
})

it("should return additionalProperty example", () => {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<aliens>\n\t<alien>test</alien>\n\t<dog>1</dog>\n</aliens>"
let definition = {
type: "object",
properties: {
alien: {
type: "string"
},
dog: {
type: "integer"
}
},
xml: {
name: "aliens"
}
}

expect(sut(definition, {}, { alien: "test", dog: 1 })).toEqual(expected)
})
})
})

Expand Down

0 comments on commit 35cb925

Please sign in to comment.