From 486d825a77807e2d73678efd5a830619f95e0ee7 Mon Sep 17 00:00:00 2001 From: Rohith Pariki Date: Sat, 29 Aug 2026 07:32:22 +0530 Subject: [PATCH] fix: safely alias schema properties and transform root xml to x-xml --- src/index.ts | 25 ++++++++++++++++++++----- test/parser.spec.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 614ce0d1..75c19369 100644 --- a/src/index.ts +++ b/src/index.ts @@ -64,6 +64,15 @@ function ajvToSpectralResult(path: Array, errors: ErrorObject[] } function iterateSchema(schema: any) { + if (!schema || typeof schema !== 'object') { + return; + } + + if (schema.xml !== undefined) { + schema['x-xml'] = schema.xml; + delete schema.xml; + } + if (schema.example !== undefined) { const examples = schema.examples || []; examples.push(schema.example); @@ -87,15 +96,21 @@ function iterateSchema(schema: any) { } function aliasProps(obj: any) { + if (!obj || typeof obj !== 'object') { + return; + } + for (const key in obj) { const prop = obj[key]; - if (prop.xml !== undefined) { - prop['x-xml'] = prop.xml; - delete prop.xml; - } + if (prop && typeof prop === 'object') { + if (prop.xml !== undefined) { + prop['x-xml'] = prop.xml; + delete prop.xml; + } - iterateSchema(obj[key]); + iterateSchema(prop); + } } } diff --git a/test/parser.spec.ts b/test/parser.spec.ts index 2cdd7687..c0f3d0a4 100644 --- a/test/parser.spec.ts +++ b/test/parser.spec.ts @@ -133,6 +133,44 @@ describe('OpenAPISchemaParser', function () { ]); }); + it('should alias xml property to x-xml on root and nested schemas', async function() { + const input: ParseSchemaInput = { + ...inputWithValidOpenApi3, + data: { + type: 'object', + xml: { name: 'rootElement' }, + properties: { + item: { + type: 'string', + xml: { name: 'itemElement', attribute: true } + } + } + } + }; + const result = await parser.parse(input); + expect(result['x-xml']).toEqual({ name: 'rootElement' }); + expect(result.xml).toBeUndefined(); + expect((result.properties as any).item['x-xml']).toEqual({ name: 'itemElement', attribute: true }); + expect((result.properties as any).item.xml).toBeUndefined(); + }); + + it('should handle boolean and primitive schema properties safely without errors', async function() { + const input: ParseSchemaInput = { + ...inputWithValidOpenApi3, + data: { + type: 'object', + properties: { + flag: true as any, + nullableProp: null as any + }, + additionalProperties: false + } + }; + const result = await parser.parse(input); + expect(result).toBeDefined(); + expect(result.additionalProperties).toEqual(false); + }); + async function doParseTest(originalInput: ParseSchemaInput, expectedOutput: string) { const input = { ...originalInput }; const result = await parser.parse(input);