diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/main.js b/src/core/plugins/json-schema-2020-12-samples/fn/main.js index ced45993920..a0ef02c87a0 100644 --- a/src/core/plugins/json-schema-2020-12-samples/fn/main.js +++ b/src/core/plugins/json-schema-2020-12-samples/fn/main.js @@ -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 diff --git a/src/core/plugins/json-schema-2020-12-samples/fn/types/array.js b/src/core/plugins/json-schema-2020-12-samples/fn/types/array.js index babaf619d55..4c39e4f7c80 100644 --- a/src/core/plugins/json-schema-2020-12-samples/fn/types/array.js +++ b/src/core/plugins/json-schema-2020-12-samples/fn/types/array.js @@ -45,7 +45,7 @@ export const applyArrayConstraints = (array, constraints = {}) => { return constrainedArray } -const arrayType = (schema, { sample }) => { +const arrayType = (schema, { sample = [] } = {}) => { return applyArrayConstraints(sample, schema) } diff --git a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js index 1c5a749d1be..8eb34470742 100644 --- a/test/unit/core/plugins/json-schema-2020-12-samples/fn.js +++ b/test/unit/core/plugins/json-schema-2020-12-samples/fn.js @@ -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 = ` + +` + + 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 = ` + +` + + expect(sut(definition)).toEqual(expected) + }) }) describe("memoizedSampleFromSchema", () => {