diff --git a/src/converter.ts b/src/converter.ts index b008d99..94597f1 100644 --- a/src/converter.ts +++ b/src/converter.ts @@ -121,6 +121,7 @@ export class Converter { this.convertConstToEnum(); this.convertNullableTypeArray(); this.removeUnsupportedSchemaKeywords(); + this.renameSchema$comment(); return this.openapi30; } @@ -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); } @@ -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); } diff --git a/test/converter.spec.ts b/test/converter.spec.ts index 6789df3..0946d08 100644 --- a/test/converter.spec.ts +++ b/test/converter.spec.ts @@ -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 = { diff --git a/test/data/openapi.yaml b/test/data/openapi.yaml index 44771fb..913a772 100644 --- a/test/data/openapi.yaml +++ b/test/data/openapi.yaml @@ -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.