Skip to content

Commit 4bada92

Browse files
authored
feat(json-schema): support x-additionalPropertiesName (#10006)
1 parent 2ab53f4 commit 4bada92

File tree

4 files changed

+98
-4
lines changed
  • src/core/plugins
  • test/unit/core/plugins

4 files changed

+98
-4
lines changed

src/core/plugins/json-schema-2020-12-samples/fn/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ export const sampleFromSchemaGeneric = (
482482
) {
483483
res[displayName].push(additionalPropSample)
484484
} else {
485+
const keyName =
486+
additionalProps?.["x-additionalPropertiesName"] || "additionalProp"
485487
const toGenerateCount =
486488
Number.isInteger(schema.minProperties) &&
487489
schema.minProperties > 0 &&
@@ -494,10 +496,10 @@ export const sampleFromSchemaGeneric = (
494496
}
495497
if (respectXML) {
496498
const temp = {}
497-
temp["additionalProp" + i] = additionalPropSample["notagname"]
499+
temp[keyName + i] = additionalPropSample["notagname"]
498500
res[displayName].push(temp)
499501
} else {
500-
res["additionalProp" + i] = additionalPropSample
502+
res[keyName + i] = additionalPropSample
501503
}
502504
propertyAddedCounter++
503505
}

src/core/plugins/json-schema-5-samples/fn/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
518518
{
519519
res[displayName].push(additionalPropSample)
520520
} else {
521+
const keyName = additionalProps["x-additionalPropertiesName"] || "additionalProp"
521522
const toGenerateCount = schema.minProperties !== null && schema.minProperties !== undefined && propertyAddedCounter < schema.minProperties
522523
? schema.minProperties - propertyAddedCounter
523524
: 3
@@ -527,10 +528,10 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
527528
}
528529
if(respectXML) {
529530
const temp = {}
530-
temp["additionalProp" + i] = additionalPropSample["notagname"]
531+
temp[keyName + i] = additionalPropSample["notagname"]
531532
res[displayName].push(temp)
532533
} else {
533-
res["additionalProp" + i] = additionalPropSample
534+
res[keyName + i] = additionalPropSample
534535
}
535536
propertyAddedCounter++
536537
}

test/unit/core/plugins/json-schema-2020-12-samples/fn.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,30 @@ describe("sampleFromSchema", () => {
12631263
expect(sampleFromSchema(definition)).toEqual(expected)
12641264
})
12651265

1266+
it("should handle additionalProperties with x-additionalPropertiesName", () => {
1267+
const definition = {
1268+
type: "object",
1269+
additionalProperties: {
1270+
type: "string",
1271+
"x-additionalPropertiesName": "bar",
1272+
},
1273+
properties: {
1274+
foo: {
1275+
type: "string",
1276+
},
1277+
},
1278+
}
1279+
1280+
const expected = {
1281+
foo: "string",
1282+
bar1: "string",
1283+
bar2: "string",
1284+
bar3: "string",
1285+
}
1286+
1287+
expect(sampleFromSchema(definition)).toEqual(expected)
1288+
})
1289+
12661290
it("should handle additionalProperties=true", () => {
12671291
const definition = {
12681292
type: "object",
@@ -2765,6 +2789,28 @@ describe("createXMLExample", function () {
27652789
expect(sut(definition)).toEqual(expected)
27662790
})
27672791

2792+
it("returns object with additional props with x-additionalPropertiesName", function () {
2793+
const expected =
2794+
'<?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>'
2795+
const definition = {
2796+
type: "object",
2797+
properties: {
2798+
dog: {
2799+
type: "string",
2800+
},
2801+
},
2802+
additionalProperties: {
2803+
type: "string",
2804+
"x-additionalPropertiesName": "animal",
2805+
},
2806+
xml: {
2807+
name: "animals",
2808+
},
2809+
}
2810+
2811+
expect(sut(definition)).toEqual(expected)
2812+
})
2813+
27682814
it("returns object with additional props =true", function () {
27692815
const expected =
27702816
'<?xml version="1.0" encoding="UTF-8"?>\n<animals>\n\t<dog>string</dog>\n\t<additionalProp>Anything can be here</additionalProp>\n</animals>'

test/unit/core/plugins/json-schema-5-samples/fn/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,30 @@ describe("sampleFromSchema", () => {
969969
expect(sampleFromSchema(definition)).toEqual(expected)
970970
})
971971

972+
it("should handle additionalProperties with x-additionalPropertiesName", () => {
973+
const definition = {
974+
type: "object",
975+
additionalProperties: {
976+
type: "string",
977+
"x-additionalPropertiesName": "bar",
978+
},
979+
properties: {
980+
foo: {
981+
type: "string",
982+
},
983+
},
984+
}
985+
986+
const expected = {
987+
foo: "string",
988+
bar1: "string",
989+
bar2: "string",
990+
bar3: "string",
991+
}
992+
993+
expect(sampleFromSchema(definition)).toEqual(expected)
994+
})
995+
972996
it("should handle additionalProperties=true", () => {
973997
const definition = {
974998
type: "object",
@@ -2223,6 +2247,27 @@ describe("createXMLExample", function () {
22232247
expect(sut(definition)).toEqual(expected)
22242248
})
22252249

2250+
it("returns object with additional props with x-additionalPropertiesName", function () {
2251+
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>"
2252+
let definition = {
2253+
type: "object",
2254+
properties: {
2255+
dog: {
2256+
type: "string"
2257+
}
2258+
},
2259+
additionalProperties: {
2260+
type: "string",
2261+
"x-additionalPropertiesName": "animal"
2262+
},
2263+
xml: {
2264+
name: "animals"
2265+
}
2266+
}
2267+
2268+
expect(sut(definition)).toEqual(expected)
2269+
})
2270+
22262271
it("returns object with additional props =true", function () {
22272272
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>"
22282273
let definition = {

0 commit comments

Comments
 (0)