From 0521cba8bb6b805e39a3f2065947452feea65abc Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Fri, 29 Nov 2024 17:34:46 +0100 Subject: [PATCH] fix: undefined error for xml when example is defined --- src/utils.js | 2 +- test/integration.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 988f621..0cf3c1f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -115,7 +115,7 @@ export function applyXMLAttributes(result, schema = {}, context = {}) { } if (schema.example !== undefined && !wrapped) { - propertyName = schema.items.xml?.name || propertyName; + propertyName = schema.items?.xml?.name || propertyName; } } if (schema.oneOf || schema.anyOf || schema.allOf || schema.$ref) { diff --git a/test/integration.spec.js b/test/integration.spec.js index 65c86c4..a038a06 100644 --- a/test/integration.spec.js +++ b/test/integration.spec.js @@ -881,6 +881,30 @@ describe('Integration', () => { const options = { format: 'xml', }; + + it('should build XML for an array with nested array', () => { + const schema = { + type: 'object', + properties: { + name: { + type: 'array', + items: { + type: 'string', + example: ['John', 'Smith'], + } + } + } + }; + const result = sample(schema, options, {}); + + expect(result.trim()).toMatchInlineSnapshot(` +" + <0>John + <1>Smith +" +`); + }); + it('should build XML for an array with wrapped elements without examples', () => { const schema = { type: 'object',