Skip to content

Commit 5d346fd

Browse files
authored
fix(json-schema-2020-12-samples): generate proper samples for XML atttributes (#10459)
1 parent 581d544 commit 5d346fd

File tree

3 files changed

+100
-2
lines changed
  • src/core/plugins/json-schema-2020-12-samples/fn
  • test/unit/core/plugins/json-schema-2020-12-samples

3 files changed

+100
-2
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,31 @@ export const sampleFromSchemaGeneric = (
148148
const propSchema = typeCast(props[propName])
149149
const propSchemaType = getType(propSchema)
150150
const attrName = props[propName].xml.name || propName
151-
_attr[attrName] = typeMap[propSchemaType](propSchema)
151+
152+
if (propSchemaType === "array") {
153+
const arraySample = sampleFromSchemaGeneric(
154+
props[propName],
155+
config,
156+
overrideE,
157+
false
158+
)
159+
_attr[attrName] = arraySample
160+
.map((item) => {
161+
if (isPlainObject(item)) {
162+
return "UnknownTypeObject"
163+
}
164+
if (Array.isArray(item)) {
165+
return "UnknownTypeArray"
166+
}
167+
return item
168+
})
169+
.join(" ")
170+
} else {
171+
_attr[attrName] =
172+
propSchemaType === "object"
173+
? "UnknownTypeObject"
174+
: typeMap[propSchemaType](propSchema)
175+
}
152176
}
153177

154178
return

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const applyArrayConstraints = (array, constraints = {}) => {
4545
return constrainedArray
4646
}
4747

48-
const arrayType = (schema, { sample }) => {
48+
const arrayType = (schema, { sample = [] } = {}) => {
4949
return applyArrayConstraints(sample, schema)
5050
}
5151

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,6 +2954,80 @@ describe("createXMLExample", function () {
29542954

29552955
expect(sut(definition)).toEqual(expected)
29562956
})
2957+
2958+
it("should handle object properties of type `array` as an attribute", () => {
2959+
const definition = {
2960+
type: "object",
2961+
xml: {
2962+
name: "test",
2963+
},
2964+
properties: {
2965+
arrayOfStrings: {
2966+
type: "array",
2967+
items: {
2968+
type: "string",
2969+
},
2970+
xml: {
2971+
attribute: true,
2972+
},
2973+
},
2974+
arrayOfArrays: {
2975+
type: "array",
2976+
items: {
2977+
type: "array",
2978+
},
2979+
minItems: 3,
2980+
xml: {
2981+
attribute: true,
2982+
},
2983+
},
2984+
arrayOfContainsObject: {
2985+
type: "array",
2986+
contains: {
2987+
type: "object",
2988+
},
2989+
minContains: 3,
2990+
xml: {
2991+
attribute: true,
2992+
},
2993+
},
2994+
},
2995+
}
2996+
2997+
const expected = `<?xml version="1.0" encoding="UTF-8"?>
2998+
<test arrayOfStrings="string" arrayOfArrays="UnknownTypeArray UnknownTypeArray UnknownTypeArray" arrayOfContainsObject="UnknownTypeObject UnknownTypeObject UnknownTypeObject">
2999+
</test>`
3000+
3001+
expect(sut(definition)).toEqual(expected)
3002+
})
3003+
3004+
it("should handle object properties of type `object` as an attribute", () => {
3005+
const definition = {
3006+
type: "object",
3007+
xml: {
3008+
name: "test",
3009+
},
3010+
properties: {
3011+
object: {
3012+
type: "object",
3013+
properties: {
3014+
string: {
3015+
type: "string",
3016+
},
3017+
},
3018+
xml: {
3019+
attribute: true,
3020+
},
3021+
},
3022+
},
3023+
}
3024+
3025+
const expected = `<?xml version="1.0" encoding="UTF-8"?>
3026+
<test object="UnknownTypeObject">
3027+
</test>`
3028+
3029+
expect(sut(definition)).toEqual(expected)
3030+
})
29573031
})
29583032

29593033
describe("memoizedSampleFromSchema", () => {

0 commit comments

Comments
 (0)