Skip to content
Open
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
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ function ajvToSpectralResult(path: Array<string | number>, 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);
Expand All @@ -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);
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading