Skip to content

Commit 2a2e4c3

Browse files
authored
Merge pull request #11 from DavidBiesack/main
Impove code , tests for $comment -> x-comment conversio
2 parents 4fc259b + 9e25511 commit 2a2e4c3

File tree

3 files changed

+117
-16
lines changed

3 files changed

+117
-16
lines changed

src/converter.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export class Converter {
121121
this.convertConstToEnum();
122122
this.convertNullableTypeArray();
123123
this.removeUnsupportedSchemaKeywords();
124+
this.renameSchema$comment();
124125
return this.openapi30;
125126
}
126127

@@ -169,23 +170,15 @@ export class Converter {
169170
* Replace all `$comment` with `x-comment`
170171
*/
171172
convertJsonSchemaComments() {
172-
const schemaVisitor: SchemaVisitor = (schema: SchemaObject): SchemaObject => {
173-
for (const key in schema) {
174-
const subSchema = schema[key];
175-
if (subSchema !== null && typeof subSchema === 'object') {
176-
if (key === '$comment') {
177-
const comment = schema['$comment'];
178-
if (comment.length > 0) {
179-
delete schema['$comment'];
180-
schema['x-comment'] = comment;
181-
this.log(`Replaces $comment with x-comment. Comment:\n${comment}`);
182-
}
183-
} else {
184-
schema[key] = walkObject(subSchema, schemaVisitor);
185-
}
186-
}
173+
const schemaVisitor: SchemaVisitor =
174+
(schema: SchemaObject): SchemaObject =>
175+
{
176+
if (schema.hasOwnProperty('$comment')) {
177+
schema['x-comment'] = schema['$comment'];
178+
delete schema['$comment'];
179+
this.log(`schema $comment renamed to x-comment`);
187180
}
188-
return schema;
181+
return this.walkNestedSchemaObjects(schema, schemaVisitor);
189182
};
190183
visitSchemaObjects(this.openapi30, schemaVisitor);
191184
}
@@ -252,6 +245,21 @@ export class Converter {
252245
visitSchemaObjects(this.openapi30, schemaVisitor);
253246
}
254247

248+
renameSchema$comment() {
249+
const schemaVisitor: SchemaVisitor =
250+
(schema: SchemaObject): SchemaObject =>
251+
{
252+
if (schema.hasOwnProperty('$comment')) {
253+
schema['x-comment'] = schema['$comment'];
254+
delete schema['$comment'];
255+
this.log(`schema $comment renamed to x-comment`);
256+
}
257+
return this.walkNestedSchemaObjects(schema, schemaVisitor);
258+
};
259+
visitSchemaObjects(this.openapi30, schemaVisitor);
260+
}
261+
262+
255263
private json(x) {
256264
return JSON.stringify(x, null, 2);
257265
}

test/converter.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,90 @@ describe('resolver test suite', () => {
355355
done();
356356
});
357357

358+
test('Remove $id and $schema keywords', (done) => {
359+
// const sourceFileName = path.join(__dirname, 'data/root.yaml'); // __dirname is the test dir
360+
const input = {
361+
openapi: '3.1.0',
362+
components: {
363+
schemas: {
364+
a: {
365+
$id: 'http://www.example.com/schemas/a',
366+
$schema: 'https://json-schema.org/draft/2020-12/schema',
367+
type: 'string',
368+
},
369+
},
370+
},
371+
};
372+
const expected = {
373+
openapi: '3.0.3',
374+
components: {
375+
schemas: {
376+
a: {
377+
type: 'string',
378+
},
379+
},
380+
},
381+
};
382+
const converter = new Converter(input, { verbose: true });
383+
const converted: any = converter.convert();
384+
expect(converted).toEqual(expected);
385+
done();
386+
});
387+
388+
test('Rename $comment to x-comment', (done) => {
389+
const input = {
390+
openapi: '3.1.0',
391+
components: {
392+
schemas: {
393+
a: {
394+
type: 'object',
395+
$comment: 'a comment on schema a',
396+
properties: {
397+
b: {
398+
type: 'object',
399+
$comment: 'A comment on a.b',
400+
properties: {
401+
s: {
402+
type: 'string',
403+
$comment: 'A comment on a.b.s',
404+
},
405+
},
406+
},
407+
},
408+
},
409+
},
410+
},
411+
};
412+
const expected = {
413+
openapi: '3.0.3',
414+
components: {
415+
schemas: {
416+
a: {
417+
type: 'object',
418+
'x-comment': 'a comment on schema a',
419+
properties: {
420+
b: {
421+
type: 'object',
422+
423+
'x-comment': 'A comment on a.b',
424+
properties: {
425+
s: {
426+
type: 'string',
427+
'x-comment': 'A comment on a.b.s',
428+
},
429+
},
430+
},
431+
},
432+
},
433+
},
434+
},
435+
};
436+
const converter = new Converter(input, { verbose: true });
437+
const converted: any = converter.convert();
438+
expect(converted).toEqual(expected);
439+
done();
440+
});
441+
358442
test('Convert nullable type array', (done) => {
359443
// const sourceFileName = path.join(__dirname, 'data/root.yaml'); // __dirname is the test dir
360444
const input = {

test/data/openapi.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,12 @@ components:
427427
format: date-time
428428
readOnly: true
429429
example: '2021-10-30T19:06:04.250Z'
430+
431+
resourceTitle:
432+
title: Resource Title
433+
description: A Title for a business object
434+
type: string
435+
maxLength: 80
436+
x-comment: >-
437+
this maxLength must match the maxLength of
438+
`title` in the `resourcePatch` schema.

0 commit comments

Comments
 (0)