Skip to content

Commit

Permalink
Merge pull request #11 from DavidBiesack/main
Browse files Browse the repository at this point in the history
Impove code , tests for $comment -> x-comment conversio
  • Loading branch information
DavidBiesack authored Nov 21, 2023
2 parents 4fc259b + 9e25511 commit 2a2e4c3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class Converter {
this.convertConstToEnum();
this.convertNullableTypeArray();
this.removeUnsupportedSchemaKeywords();
this.renameSchema$comment();
return this.openapi30;
}

Expand Down Expand Up @@ -169,23 +170,15 @@ export class Converter {
* Replace all `$comment` with `x-comment`
*/
convertJsonSchemaComments() {
const schemaVisitor: SchemaVisitor = (schema: SchemaObject): SchemaObject => {
for (const key in schema) {
const subSchema = schema[key];
if (subSchema !== null && typeof subSchema === 'object') {
if (key === '$comment') {
const comment = schema['$comment'];
if (comment.length > 0) {
delete schema['$comment'];
schema['x-comment'] = comment;
this.log(`Replaces $comment with x-comment. Comment:\n${comment}`);
}
} else {
schema[key] = walkObject(subSchema, schemaVisitor);
}
}
const schemaVisitor: SchemaVisitor =
(schema: SchemaObject): SchemaObject =>
{
if (schema.hasOwnProperty('$comment')) {
schema['x-comment'] = schema['$comment'];
delete schema['$comment'];
this.log(`schema $comment renamed to x-comment`);
}
return schema;
return this.walkNestedSchemaObjects(schema, schemaVisitor);
};
visitSchemaObjects(this.openapi30, schemaVisitor);
}
Expand Down Expand Up @@ -252,6 +245,21 @@ export class Converter {
visitSchemaObjects(this.openapi30, schemaVisitor);
}

renameSchema$comment() {
const schemaVisitor: SchemaVisitor =
(schema: SchemaObject): SchemaObject =>
{
if (schema.hasOwnProperty('$comment')) {
schema['x-comment'] = schema['$comment'];
delete schema['$comment'];
this.log(`schema $comment renamed to x-comment`);
}
return this.walkNestedSchemaObjects(schema, schemaVisitor);
};
visitSchemaObjects(this.openapi30, schemaVisitor);
}


private json(x) {
return JSON.stringify(x, null, 2);
}
Expand Down
84 changes: 84 additions & 0 deletions test/converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,90 @@ describe('resolver test suite', () => {
done();
});

test('Remove $id and $schema keywords', (done) => {
// const sourceFileName = path.join(__dirname, 'data/root.yaml'); // __dirname is the test dir
const input = {
openapi: '3.1.0',
components: {
schemas: {
a: {
$id: 'http://www.example.com/schemas/a',
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'string',
},
},
},
};
const expected = {
openapi: '3.0.3',
components: {
schemas: {
a: {
type: 'string',
},
},
},
};
const converter = new Converter(input, { verbose: true });
const converted: any = converter.convert();
expect(converted).toEqual(expected);
done();
});

test('Rename $comment to x-comment', (done) => {
const input = {
openapi: '3.1.0',
components: {
schemas: {
a: {
type: 'object',
$comment: 'a comment on schema a',
properties: {
b: {
type: 'object',
$comment: 'A comment on a.b',
properties: {
s: {
type: 'string',
$comment: 'A comment on a.b.s',
},
},
},
},
},
},
},
};
const expected = {
openapi: '3.0.3',
components: {
schemas: {
a: {
type: 'object',
'x-comment': 'a comment on schema a',
properties: {
b: {
type: 'object',

'x-comment': 'A comment on a.b',
properties: {
s: {
type: 'string',
'x-comment': 'A comment on a.b.s',
},
},
},
},
},
},
},
};
const converter = new Converter(input, { verbose: true });
const converted: any = converter.convert();
expect(converted).toEqual(expected);
done();
});

test('Convert nullable type array', (done) => {
// const sourceFileName = path.join(__dirname, 'data/root.yaml'); // __dirname is the test dir
const input = {
Expand Down
9 changes: 9 additions & 0 deletions test/data/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,12 @@ components:
format: date-time
readOnly: true
example: '2021-10-30T19:06:04.250Z'

resourceTitle:
title: Resource Title
description: A Title for a business object
type: string
maxLength: 80
x-comment: >-
this maxLength must match the maxLength of
`title` in the `resourcePatch` schema.

0 comments on commit 2a2e4c3

Please sign in to comment.