Skip to content
Merged
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
26 changes: 25 additions & 1 deletion src/core/plugins/json-schema-2020-12-samples/fn/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,31 @@ export const sampleFromSchemaGeneric = (
const propSchema = typeCast(props[propName])
const propSchemaType = getType(propSchema)
const attrName = props[propName].xml.name || propName
_attr[attrName] = typeMap[propSchemaType](propSchema)

if (propSchemaType === "array") {
const arraySample = sampleFromSchemaGeneric(
props[propName],
config,
overrideE,
false
)
_attr[attrName] = arraySample
.map((item) => {
if (isPlainObject(item)) {
return "UnknownTypeObject"
}
if (Array.isArray(item)) {
return "UnknownTypeArray"
}
return item
})
.join(" ")
} else {
_attr[attrName] =
propSchemaType === "object"
? "UnknownTypeObject"
: typeMap[propSchemaType](propSchema)
}
}

return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const applyArrayConstraints = (array, constraints = {}) => {
return constrainedArray
}

const arrayType = (schema, { sample }) => {
const arrayType = (schema, { sample = [] } = {}) => {
return applyArrayConstraints(sample, schema)
}

Expand Down
74 changes: 74 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 @@ -2954,6 +2954,80 @@ describe("createXMLExample", function () {

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

it("should handle object properties of type `array` as an attribute", () => {
const definition = {
type: "object",
xml: {
name: "test",
},
properties: {
arrayOfStrings: {
type: "array",
items: {
type: "string",
},
xml: {
attribute: true,
},
},
arrayOfArrays: {
type: "array",
items: {
type: "array",
},
minItems: 3,
xml: {
attribute: true,
},
},
arrayOfContainsObject: {
type: "array",
contains: {
type: "object",
},
minContains: 3,
xml: {
attribute: true,
},
},
},
}

const expected = `<?xml version="1.0" encoding="UTF-8"?>
<test arrayOfStrings="string" arrayOfArrays="UnknownTypeArray UnknownTypeArray UnknownTypeArray" arrayOfContainsObject="UnknownTypeObject UnknownTypeObject UnknownTypeObject">
</test>`

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

it("should handle object properties of type `object` as an attribute", () => {
const definition = {
type: "object",
xml: {
name: "test",
},
properties: {
object: {
type: "object",
properties: {
string: {
type: "string",
},
},
xml: {
attribute: true,
},
},
},
}

const expected = `<?xml version="1.0" encoding="UTF-8"?>
<test object="UnknownTypeObject">
</test>`

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

describe("memoizedSampleFromSchema", () => {
Expand Down