Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impove code , tests for $comment -> x-comment conversio #11

Merged
merged 5 commits into from
Nov 21, 2023
Merged
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
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.