Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(json-schema): support x-additionalPropertiesName #10006

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/plugins/json-schema-2020-12-samples/fn/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ export const sampleFromSchemaGeneric = (
) {
res[displayName].push(additionalPropSample)
} else {
const keyName =
additionalProps?.["x-additionalPropertiesName"] || "additionalProp"
const toGenerateCount =
Number.isInteger(schema.minProperties) &&
schema.minProperties > 0 &&
Expand All @@ -470,10 +472,10 @@ export const sampleFromSchemaGeneric = (
}
if (respectXML) {
const temp = {}
temp["additionalProp" + i] = additionalPropSample["notagname"]
temp[keyName + i] = additionalPropSample["notagname"]
res[displayName].push(temp)
} else {
res["additionalProp" + i] = additionalPropSample
res[keyName + i] = additionalPropSample
}
propertyAddedCounter++
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
{
res[displayName].push(additionalPropSample)
} else {
const keyName = additionalProps["x-additionalPropertiesName"] || "additionalProp"
const toGenerateCount = schema.minProperties !== null && schema.minProperties !== undefined && propertyAddedCounter < schema.minProperties
? schema.minProperties - propertyAddedCounter
: 3
Expand All @@ -515,10 +516,10 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
}
if(respectXML) {
const temp = {}
temp["additionalProp" + i] = additionalPropSample["notagname"]
temp[keyName + i] = additionalPropSample["notagname"]
res[displayName].push(temp)
} else {
res["additionalProp" + i] = additionalPropSample
res[keyName + i] = additionalPropSample
}
propertyAddedCounter++
}
Expand Down
46 changes: 46 additions & 0 deletions test/unit/core/plugins/json-schema-2020-12-samples/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,30 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})

it("should handle additionalProperties with x-additionalPropertiesName", () => {
const definition = {
type: "object",
additionalProperties: {
type: "string",
"x-additionalPropertiesName": "bar",
},
properties: {
foo: {
type: "string",
},
},
}

const expected = {
foo: "string",
bar1: "string",
bar2: "string",
bar3: "string",
}

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("should handle additionalProperties=true", () => {
const definition = {
type: "object",
Expand Down Expand Up @@ -2755,6 +2779,28 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props with x-additionalPropertiesName", function () {
const expected =
'<?xml version="1.0" encoding="UTF-8"?>\n<animals>\n\t<dog>string</dog>\n\t<animal1>string</animal1>\n\t<animal2>string</animal2>\n\t<animal3>string</animal3>\n</animals>'
const definition = {
type: "object",
properties: {
dog: {
type: "string",
},
},
additionalProperties: {
type: "string",
"x-additionalPropertiesName": "animal",
},
xml: {
name: "animals",
},
}

expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props =true", function () {
const expected =
'<?xml version="1.0" encoding="UTF-8"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp>Anything can be here</additionalProp>\n</animals>'
Expand Down
45 changes: 45 additions & 0 deletions test/unit/core/plugins/json-schema-5-samples/fn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,30 @@ describe("sampleFromSchema", () => {
expect(sampleFromSchema(definition)).toEqual(expected)
})

it("should handle additionalProperties with x-additionalPropertiesName", () => {
const definition = {
type: "object",
additionalProperties: {
type: "string",
"x-additionalPropertiesName": "bar",
},
properties: {
foo: {
type: "string",
},
},
}

const expected = {
foo: "string",
bar1: "string",
bar2: "string",
bar3: "string",
}

expect(sampleFromSchema(definition)).toEqual(expected)
})

it("should handle additionalProperties=true", () => {
const definition = {
type: "object",
Expand Down Expand Up @@ -2199,6 +2223,27 @@ describe("createXMLExample", function () {
expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props with x-additionalPropertiesName", function () {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n\t<animal1>string</animal1>\n\t<animal2>string</animal2>\n\t<animal3>string</animal3>\n</animals>"
let definition = {
type: "object",
properties: {
dog: {
type: "string"
}
},
additionalProperties: {
type: "string",
"x-additionalPropertiesName": "animal"
},
xml: {
name: "animals"
}
}

expect(sut(definition)).toEqual(expected)
})

it("returns object with additional props =true", function () {
let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp>Anything can be here</additionalProp>\n</animals>"
let definition = {
Expand Down