diff --git a/rollup.config.js b/rollup.config.js index ff914057..59e8d39e 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -34,6 +34,8 @@ const handlebarsPlugin = () => ({ union: true, intersection: true, enumerator: true, + escapeComment: true, + escapeDescription: true, }, }); return `export default ${templateSpec};`; diff --git a/src/openApi/v2/parser/escapeDescription.spec.ts b/src/openApi/v2/parser/escapeDescription.spec.ts deleted file mode 100644 index 5823eec0..00000000 --- a/src/openApi/v2/parser/escapeDescription.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { escapeDescription } from './escapeDescription'; - -describe('escapeDescription', () => { - it('should escape', () => { - expect(escapeDescription('foo `test` bar')).toEqual('foo \\`test\\` bar'); - }); - - it('should not escape', () => { - expect(escapeDescription('')).toEqual(''); - expect(escapeDescription('fooBar')).toEqual('fooBar'); - expect(escapeDescription('foo \\`test\\` bar')).toEqual('foo \\`test\\` bar'); - expect(escapeDescription('foo */bar/*')).toEqual('foo *_/bar/*'); - }); -}); diff --git a/src/openApi/v2/parser/escapeDescription.ts b/src/openApi/v2/parser/escapeDescription.ts deleted file mode 100644 index fc0b05a1..00000000 --- a/src/openApi/v2/parser/escapeDescription.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function escapeDescription(value: string): string { - return value.replace(/([^\\])`/g, '$1\\`').replace(/(\*\/)/g, '*_/'); -} diff --git a/src/openApi/v2/parser/getComment.spec.ts b/src/openApi/v2/parser/getComment.spec.ts deleted file mode 100644 index c3bde383..00000000 --- a/src/openApi/v2/parser/getComment.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { EOL } from 'os'; - -import { getComment } from './getComment'; - -describe('getComment', () => { - it('should parse comments', () => { - const multiline = - 'Testing multiline comments.' + - EOL + - ' * This must go to the next line.' + - EOL + - ' * ' + - EOL + - ' * This will contain a break.'; - expect(getComment('')).toEqual(null); - expect(getComment('Hello')).toEqual('Hello'); - expect(getComment('Hello World!')).toEqual('Hello World!'); - expect(getComment('Testing */escape/*')).toEqual('Testing *_/escape/*'); - expect( - getComment('Testing multiline comments.\nThis must go to the next line.\n\nThis will contain a break.') - ).toEqual(multiline); - expect( - getComment( - 'Testing multiline comments.\r\nThis must go to the next line.\r\n\r\nThis will contain a break.' - ) - ).toEqual(multiline); - }); -}); diff --git a/src/openApi/v2/parser/getComment.ts b/src/openApi/v2/parser/getComment.ts deleted file mode 100644 index cf126b9c..00000000 --- a/src/openApi/v2/parser/getComment.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { EOL } from 'os'; - -/** - * Cleanup comment and prefix multiline comments with "*", - * so they look a bit nicer when used in the generated code. - * @param comment - */ -export function getComment(comment?: string): string | null { - if (comment) { - return comment.replace(/(\*\/)/g, '*_/').replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`); - } - return null; -} diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index dbf75107..d5683f07 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -3,7 +3,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { extendEnum } from './extendEnum'; -import { getComment } from './getComment'; import { getEnum } from './getEnum'; import { getEnumFromDescription } from './getEnumFromDescription'; import { getModelComposition } from './getModelComposition'; @@ -23,7 +22,7 @@ export function getModel( base: 'any', template: null, link: null, - description: getComment(definition.description), + description: definition.description || null, isDefinition, isReadOnly: definition.readOnly === true, isNullable: definition['x-nullable'] === true, diff --git a/src/openApi/v2/parser/getModelProperties.ts b/src/openApi/v2/parser/getModelProperties.ts index e1dcf27e..fcb07fc9 100644 --- a/src/openApi/v2/parser/getModelProperties.ts +++ b/src/openApi/v2/parser/getModelProperties.ts @@ -3,7 +3,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { escapeName } from './escapeName'; -import { getComment } from './getComment'; import type { getModel } from './getModel'; import { getType } from './getType'; @@ -25,7 +24,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema, base: model.base, template: model.template, link: null, - description: getComment(property.description), + description: property.description || null, isDefinition: false, isReadOnly: property.readOnly === true, isRequired: propertyRequired, @@ -58,7 +57,7 @@ export function getModelProperties(openApi: OpenApi, definition: OpenApiSchema, base: model.base, template: model.template, link: model.link, - description: getComment(property.description), + description: property.description || null, isDefinition: false, isReadOnly: property.readOnly === true, isRequired: propertyRequired, diff --git a/src/openApi/v2/parser/getOperation.ts b/src/openApi/v2/parser/getOperation.ts index f56a0d8c..f70e7f21 100644 --- a/src/openApi/v2/parser/getOperation.ts +++ b/src/openApi/v2/parser/getOperation.ts @@ -2,7 +2,6 @@ import type { Operation } from '../../../client/interfaces/Operation'; import type { OperationParameters } from '../../../client/interfaces/OperationParameters'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiOperation } from '../interfaces/OpenApiOperation'; -import { getComment } from './getComment'; import { getOperationErrors } from './getOperationErrors'; import { getOperationName } from './getOperationName'; import { getOperationParameters } from './getOperationParameters'; @@ -30,8 +29,8 @@ export function getOperation( const operation: Operation = { service: serviceName, name: operationName, - summary: getComment(op.summary), - description: getComment(op.description), + summary: op.summary || null, + description: op.description || null, deprecated: op.deprecated === true, method: method.toUpperCase(), path: operationPath, diff --git a/src/openApi/v2/parser/getOperationErrors.ts b/src/openApi/v2/parser/getOperationErrors.ts index 31a742c8..3e0ed0b9 100644 --- a/src/openApi/v2/parser/getOperationErrors.ts +++ b/src/openApi/v2/parser/getOperationErrors.ts @@ -1,7 +1,10 @@ import type { OperationError } from '../../../client/interfaces/OperationError'; import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -import { escapeDescription } from './escapeDescription'; +/** + * + * @param operationResponses + */ export function getOperationErrors(operationResponses: OperationResponse[]): OperationError[] { return operationResponses .filter(operationResponse => { @@ -9,6 +12,6 @@ export function getOperationErrors(operationResponses: OperationResponse[]): Ope }) .map(response => ({ code: response.code, - description: escapeDescription(response.description!), + description: response.description!, })); } diff --git a/src/openApi/v2/parser/getOperationParameter.ts b/src/openApi/v2/parser/getOperationParameter.ts index 08394069..7bfb136d 100644 --- a/src/openApi/v2/parser/getOperationParameter.ts +++ b/src/openApi/v2/parser/getOperationParameter.ts @@ -4,7 +4,6 @@ import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { extendEnum } from './extendEnum'; -import { getComment } from './getComment'; import { getEnum } from './getEnum'; import { getEnumFromDescription } from './getEnumFromDescription'; import { getModel } from './getModel'; @@ -23,7 +22,7 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame base: 'any', template: null, link: null, - description: getComment(parameter.description), + description: parameter.description || null, isDefinition: false, isReadOnly: false, isRequired: parameter.required === true, diff --git a/src/openApi/v2/parser/getOperationResponse.ts b/src/openApi/v2/parser/getOperationResponse.ts index 1008fca6..c7611915 100644 --- a/src/openApi/v2/parser/getOperationResponse.ts +++ b/src/openApi/v2/parser/getOperationResponse.ts @@ -3,7 +3,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiResponse } from '../interfaces/OpenApiResponse'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; -import { getComment } from './getComment'; import { getModel } from './getModel'; import { getRef } from './getRef'; import { getType } from './getType'; @@ -17,7 +16,7 @@ export function getOperationResponse( in: 'response', name: '', code: responseCode, - description: getComment(response.description)!, + description: response.description || null, export: 'generic', type: 'any', base: 'any', @@ -35,8 +34,8 @@ export function getOperationResponse( // If this response has a schema, then we need to check two things: // if this is a reference then the parameter is just the 'name' of - // this reference type. Otherwise it might be a complex schema and - // then we need to parse the schema! + // this reference type. Otherwise, it might be a complex schema, + // and then we need to parse the schema! let schema = response.schema; if (schema) { if (schema.$ref?.startsWith('#/responses/')) { diff --git a/src/openApi/v3/parser/escapeDescription.spec.ts b/src/openApi/v3/parser/escapeDescription.spec.ts deleted file mode 100644 index 5823eec0..00000000 --- a/src/openApi/v3/parser/escapeDescription.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { escapeDescription } from './escapeDescription'; - -describe('escapeDescription', () => { - it('should escape', () => { - expect(escapeDescription('foo `test` bar')).toEqual('foo \\`test\\` bar'); - }); - - it('should not escape', () => { - expect(escapeDescription('')).toEqual(''); - expect(escapeDescription('fooBar')).toEqual('fooBar'); - expect(escapeDescription('foo \\`test\\` bar')).toEqual('foo \\`test\\` bar'); - expect(escapeDescription('foo */bar/*')).toEqual('foo *_/bar/*'); - }); -}); diff --git a/src/openApi/v3/parser/escapeDescription.ts b/src/openApi/v3/parser/escapeDescription.ts deleted file mode 100644 index fc0b05a1..00000000 --- a/src/openApi/v3/parser/escapeDescription.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function escapeDescription(value: string): string { - return value.replace(/([^\\])`/g, '$1\\`').replace(/(\*\/)/g, '*_/'); -} diff --git a/src/openApi/v3/parser/getComment.spec.ts b/src/openApi/v3/parser/getComment.spec.ts deleted file mode 100644 index c3bde383..00000000 --- a/src/openApi/v3/parser/getComment.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { EOL } from 'os'; - -import { getComment } from './getComment'; - -describe('getComment', () => { - it('should parse comments', () => { - const multiline = - 'Testing multiline comments.' + - EOL + - ' * This must go to the next line.' + - EOL + - ' * ' + - EOL + - ' * This will contain a break.'; - expect(getComment('')).toEqual(null); - expect(getComment('Hello')).toEqual('Hello'); - expect(getComment('Hello World!')).toEqual('Hello World!'); - expect(getComment('Testing */escape/*')).toEqual('Testing *_/escape/*'); - expect( - getComment('Testing multiline comments.\nThis must go to the next line.\n\nThis will contain a break.') - ).toEqual(multiline); - expect( - getComment( - 'Testing multiline comments.\r\nThis must go to the next line.\r\n\r\nThis will contain a break.' - ) - ).toEqual(multiline); - }); -}); diff --git a/src/openApi/v3/parser/getComment.ts b/src/openApi/v3/parser/getComment.ts deleted file mode 100644 index cf126b9c..00000000 --- a/src/openApi/v3/parser/getComment.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { EOL } from 'os'; - -/** - * Cleanup comment and prefix multiline comments with "*", - * so they look a bit nicer when used in the generated code. - * @param comment - */ -export function getComment(comment?: string): string | null { - if (comment) { - return comment.replace(/(\*\/)/g, '*_/').replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`); - } - return null; -} diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index 99507ad8..a44ebb0d 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -3,7 +3,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { extendEnum } from './extendEnum'; -import { getComment } from './getComment'; import { getEnum } from './getEnum'; import { getEnumFromDescription } from './getEnumFromDescription'; import { getModelComposition } from './getModelComposition'; @@ -24,7 +23,7 @@ export function getModel( base: 'any', template: null, link: null, - description: getComment(definition.description), + description: definition.description || null, isDefinition, isReadOnly: definition.readOnly === true, isNullable: definition.nullable === true, diff --git a/src/openApi/v3/parser/getModelProperties.ts b/src/openApi/v3/parser/getModelProperties.ts index ccafe821..9c9833da 100644 --- a/src/openApi/v3/parser/getModelProperties.ts +++ b/src/openApi/v3/parser/getModelProperties.ts @@ -4,7 +4,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; import { escapeName } from './escapeName'; -import { getComment } from './getComment'; import type { getModel } from './getModel'; import { getType } from './getType'; @@ -37,7 +36,7 @@ export function getModelProperties( | 'properties' > = { name: escapeName(propertyName), - description: getComment(property.description), + description: property.description || null, isDefinition: false, isReadOnly: property.readOnly === true, isRequired: propertyRequired, diff --git a/src/openApi/v3/parser/getOperation.ts b/src/openApi/v3/parser/getOperation.ts index 67873e11..de452712 100644 --- a/src/openApi/v3/parser/getOperation.ts +++ b/src/openApi/v3/parser/getOperation.ts @@ -3,7 +3,6 @@ import type { OperationParameters } from '../../../client/interfaces/OperationPa import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiOperation } from '../interfaces/OpenApiOperation'; import type { OpenApiRequestBody } from '../interfaces/OpenApiRequestBody'; -import { getComment } from './getComment'; import { getOperationErrors } from './getOperationErrors'; import { getOperationName } from './getOperationName'; import { getOperationParameters } from './getOperationParameters'; @@ -33,8 +32,8 @@ export function getOperation( const operation: Operation = { service: serviceName, name: operationName, - summary: getComment(op.summary), - description: getComment(op.description), + summary: op.summary || null, + description: op.description || null, deprecated: op.deprecated === true, method: method.toUpperCase(), path: operationPath, diff --git a/src/openApi/v3/parser/getOperationErrors.ts b/src/openApi/v3/parser/getOperationErrors.ts index 31a742c8..9703f91b 100644 --- a/src/openApi/v3/parser/getOperationErrors.ts +++ b/src/openApi/v3/parser/getOperationErrors.ts @@ -1,6 +1,5 @@ import type { OperationError } from '../../../client/interfaces/OperationError'; import type { OperationResponse } from '../../../client/interfaces/OperationResponse'; -import { escapeDescription } from './escapeDescription'; export function getOperationErrors(operationResponses: OperationResponse[]): OperationError[] { return operationResponses @@ -9,6 +8,6 @@ export function getOperationErrors(operationResponses: OperationResponse[]): Ope }) .map(response => ({ code: response.code, - description: escapeDescription(response.description!), + description: response.description!, })); } diff --git a/src/openApi/v3/parser/getOperationParameter.ts b/src/openApi/v3/parser/getOperationParameter.ts index c8cacc2d..89ef5f35 100644 --- a/src/openApi/v3/parser/getOperationParameter.ts +++ b/src/openApi/v3/parser/getOperationParameter.ts @@ -3,7 +3,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiParameter } from '../interfaces/OpenApiParameter'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; -import { getComment } from './getComment'; import { getModel } from './getModel'; import { getModelDefault } from './getModelDefault'; import { getOperationParameterName } from './getOperationParameterName'; @@ -20,7 +19,7 @@ export function getOperationParameter(openApi: OpenApi, parameter: OpenApiParame base: 'any', template: null, link: null, - description: getComment(parameter.description), + description: parameter.description || null, isDefinition: false, isReadOnly: false, isRequired: parameter.required === true, diff --git a/src/openApi/v3/parser/getOperationRequestBody.ts b/src/openApi/v3/parser/getOperationRequestBody.ts index 6f608a3a..be0b8eeb 100644 --- a/src/openApi/v3/parser/getOperationRequestBody.ts +++ b/src/openApi/v3/parser/getOperationRequestBody.ts @@ -2,7 +2,6 @@ import type { OperationParameter } from '../../../client/interfaces/OperationPar import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiRequestBody } from '../interfaces/OpenApiRequestBody'; -import { getComment } from './getComment'; import { getContent } from './getContent'; import { getModel } from './getModel'; import { getType } from './getType'; @@ -17,7 +16,7 @@ export function getOperationRequestBody(openApi: OpenApi, body: OpenApiRequestBo base: 'any', template: null, link: null, - description: getComment(body.description), + description: body.description || null, default: undefined, isDefinition: false, isReadOnly: false, diff --git a/src/openApi/v3/parser/getOperationResponse.ts b/src/openApi/v3/parser/getOperationResponse.ts index ccfd7cf4..8b91980e 100644 --- a/src/openApi/v3/parser/getOperationResponse.ts +++ b/src/openApi/v3/parser/getOperationResponse.ts @@ -3,7 +3,6 @@ import { getPattern } from '../../../utils/getPattern'; import type { OpenApi } from '../interfaces/OpenApi'; import type { OpenApiResponse } from '../interfaces/OpenApiResponse'; import type { OpenApiSchema } from '../interfaces/OpenApiSchema'; -import { getComment } from './getComment'; import { getContent } from './getContent'; import { getModel } from './getModel'; import { getRef } from './getRef'; @@ -18,7 +17,7 @@ export function getOperationResponse( in: 'response', name: '', code: responseCode, - description: getComment(response.description)!, + description: response.description || null, export: 'generic', type: 'any', base: 'any', diff --git a/src/templates/exportService.hbs b/src/templates/exportService.hbs index 1e13e19a..10a389f3 100644 --- a/src/templates/exportService.hbs +++ b/src/templates/exportService.hbs @@ -19,20 +19,20 @@ export class {{{name}}}{{{@root.postfix}}} { * @deprecated {{/if}} {{#if summary}} - * {{{summary}}} + * {{{escapeComment summary}}} {{/if}} {{#if description}} - * {{{description}}} + * {{{escapeComment description}}} {{/if}} {{#unless @root.useOptions}} {{#if parameters}} {{#each parameters}} - * @param {{{name}}} {{{description}}} + * @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}} {{/each}} {{/if}} {{/unless}} {{#each results}} - * @returns {{{type}}} {{{description}}} + * @returns {{{type}}} {{#if description}}{{{escapeComment description}}}{{/if}} {{/each}} * @throws ApiError */ diff --git a/src/templates/partials/exportComposition.hbs b/src/templates/partials/exportComposition.hbs index b8b3f4a2..d1ea334c 100644 --- a/src/templates/partials/exportComposition.hbs +++ b/src/templates/partials/exportComposition.hbs @@ -1,6 +1,6 @@ {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} export type {{{name}}} = {{>type parent=name}}; @@ -12,7 +12,7 @@ export namespace {{{name}}} { {{#each enums}} {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} export enum {{{name}}} { diff --git a/src/templates/partials/exportEnum.hbs b/src/templates/partials/exportEnum.hbs index 3f81c093..34cec514 100644 --- a/src/templates/partials/exportEnum.hbs +++ b/src/templates/partials/exportEnum.hbs @@ -1,13 +1,13 @@ {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} export enum {{{name}}} { {{#each enum}} {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} {{#containsSpaces name}} diff --git a/src/templates/partials/exportInterface.hbs b/src/templates/partials/exportInterface.hbs index d63fc87d..2a023043 100644 --- a/src/templates/partials/exportInterface.hbs +++ b/src/templates/partials/exportInterface.hbs @@ -1,13 +1,13 @@ {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} export type {{{name}}} = { {{#each properties}} {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} {{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../name}}; @@ -21,7 +21,7 @@ export namespace {{{name}}} { {{#each enums}} {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} export enum {{{name}}} { diff --git a/src/templates/partials/exportType.hbs b/src/templates/partials/exportType.hbs index d4d5b83b..3fce9bef 100644 --- a/src/templates/partials/exportType.hbs +++ b/src/templates/partials/exportType.hbs @@ -1,6 +1,6 @@ {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} export type {{{name}}} = {{>type}}; diff --git a/src/templates/partials/parameters.hbs b/src/templates/partials/parameters.hbs index a911f3a4..afbb74f4 100644 --- a/src/templates/partials/parameters.hbs +++ b/src/templates/partials/parameters.hbs @@ -7,7 +7,7 @@ }: { {{#each parameters}} {{#if description}} -/** {{{description}}} **/ +/** {{{escapeComment description}}} **/ {{/if}} {{{name}}}{{>isRequired}}: {{>type}}, {{/each}} diff --git a/src/templates/partials/schemaComposition.hbs b/src/templates/partials/schemaComposition.hbs index 50b507c0..f916e73d 100644 --- a/src/templates/partials/schemaComposition.hbs +++ b/src/templates/partials/schemaComposition.hbs @@ -1,7 +1,7 @@ { type: '{{export}}', {{#if description}} - description: `{{{description}}}`, + description: `{{{escapeDescription description}}}`, {{/if}} contains: [{{#each properties}}{{>schema}}{{#unless @last}}, {{/unless}}{{/each}}], {{#if isReadOnly}} diff --git a/src/templates/partials/schemaGeneric.hbs b/src/templates/partials/schemaGeneric.hbs index 052dee04..31ad8e46 100644 --- a/src/templates/partials/schemaGeneric.hbs +++ b/src/templates/partials/schemaGeneric.hbs @@ -3,7 +3,7 @@ type: '{{{type}}}', {{/if}} {{#if description}} - description: `{{{description}}}`, + description: `{{{escapeDescription description}}}`, {{/if}} {{#if isReadOnly}} isReadOnly: {{{isReadOnly}}}, diff --git a/src/templates/partials/schemaInterface.hbs b/src/templates/partials/schemaInterface.hbs index 29987157..94770606 100644 --- a/src/templates/partials/schemaInterface.hbs +++ b/src/templates/partials/schemaInterface.hbs @@ -1,6 +1,6 @@ { {{#if description}} - description: `{{{description}}}`, + description: `{{{escapeDescription description}}}`, {{/if}} properties: { {{#if properties}} diff --git a/src/templates/partials/typeInterface.hbs b/src/templates/partials/typeInterface.hbs index 1adbc5b2..649e916c 100644 --- a/src/templates/partials/typeInterface.hbs +++ b/src/templates/partials/typeInterface.hbs @@ -3,7 +3,7 @@ {{#each properties}} {{#if description}} /** - * {{{description}}} + * {{{escapeComment description}}} */ {{/if}} {{#if ../parent}} diff --git a/src/utils/registerHandlebarHelpers.spec.ts b/src/utils/registerHandlebarHelpers.spec.ts index d261f38e..adc4200b 100644 --- a/src/utils/registerHandlebarHelpers.spec.ts +++ b/src/utils/registerHandlebarHelpers.spec.ts @@ -17,5 +17,7 @@ describe('registerHandlebarHelpers', () => { expect(helpers).toContain('union'); expect(helpers).toContain('intersection'); expect(helpers).toContain('enumerator'); + expect(helpers).toContain('escapeComment'); + expect(helpers).toContain('escapeDescription'); }); }); diff --git a/src/utils/registerHandlebarHelpers.ts b/src/utils/registerHandlebarHelpers.ts index 7f84f8d3..a1ac4454 100644 --- a/src/utils/registerHandlebarHelpers.ts +++ b/src/utils/registerHandlebarHelpers.ts @@ -1,4 +1,5 @@ import Handlebars from 'handlebars/runtime'; +import { EOL } from 'os'; import { Enum } from '../client/interfaces/Enum'; import { Model } from '../client/interfaces/Model'; @@ -79,4 +80,15 @@ export function registerHandlebarHelpers(root: { ); } ); + + Handlebars.registerHelper('escapeComment', function (value: string): string { + return value + .replace(/\*\//g, '*') + .replace(/\/\*/g, '*') + .replace(/\r?\n(.*)/g, (_, w) => `${EOL} * ${w.trim()}`); + }); + + Handlebars.registerHelper('escapeDescription', function (value: string): string { + return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\${/g, '\\${'); + }); } diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap index 0621d72d..2804feb0 100644 --- a/test/__snapshots__/index.spec.ts.snap +++ b/test/__snapshots__/index.spec.ts.snap @@ -524,6 +524,12 @@ export type { ArrayWithNumbers } from './models/ArrayWithNumbers'; export type { ArrayWithProperties } from './models/ArrayWithProperties'; export type { ArrayWithReferences } from './models/ArrayWithReferences'; export type { ArrayWithStrings } from './models/ArrayWithStrings'; +export type { CommentWithBackticks } from './models/CommentWithBackticks'; +export type { CommentWithBreaks } from './models/CommentWithBreaks'; +export type { CommentWithExpressionPlaceholders } from './models/CommentWithExpressionPlaceholders'; +export type { CommentWithQuotes } from './models/CommentWithQuotes'; +export type { CommentWithReservedCharacters } from './models/CommentWithReservedCharacters'; +export type { CommentWithSlashes } from './models/CommentWithSlashes'; export type { Date } from './models/Date'; export type { DictionaryWithArray } from './models/DictionaryWithArray'; export type { DictionaryWithDictionary } from './models/DictionaryWithDictionary'; @@ -553,7 +559,6 @@ export type { ModelWithPattern } from './models/ModelWithPattern'; export type { ModelWithProperties } from './models/ModelWithProperties'; export type { ModelWithReference } from './models/ModelWithReference'; export type { ModelWithString } from './models/ModelWithString'; -export type { MultilineComment } from './models/MultilineComment'; export type { SimpleBoolean } from './models/SimpleBoolean'; export type { SimpleFile } from './models/SimpleFile'; export type { SimpleInteger } from './models/SimpleInteger'; @@ -567,6 +572,12 @@ export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers'; export { $ArrayWithProperties } from './schemas/$ArrayWithProperties'; export { $ArrayWithReferences } from './schemas/$ArrayWithReferences'; export { $ArrayWithStrings } from './schemas/$ArrayWithStrings'; +export { $CommentWithBackticks } from './schemas/$CommentWithBackticks'; +export { $CommentWithBreaks } from './schemas/$CommentWithBreaks'; +export { $CommentWithExpressionPlaceholders } from './schemas/$CommentWithExpressionPlaceholders'; +export { $CommentWithQuotes } from './schemas/$CommentWithQuotes'; +export { $CommentWithReservedCharacters } from './schemas/$CommentWithReservedCharacters'; +export { $CommentWithSlashes } from './schemas/$CommentWithSlashes'; export { $Date } from './schemas/$Date'; export { $DictionaryWithArray } from './schemas/$DictionaryWithArray'; export { $DictionaryWithDictionary } from './schemas/$DictionaryWithDictionary'; @@ -596,7 +607,6 @@ export { $ModelWithPattern } from './schemas/$ModelWithPattern'; export { $ModelWithProperties } from './schemas/$ModelWithProperties'; export { $ModelWithReference } from './schemas/$ModelWithReference'; export { $ModelWithString } from './schemas/$ModelWithString'; -export { $MultilineComment } from './schemas/$MultilineComment'; export { $SimpleBoolean } from './schemas/$SimpleBoolean'; export { $SimpleFile } from './schemas/$SimpleFile'; export { $SimpleInteger } from './schemas/$SimpleInteger'; @@ -694,6 +704,75 @@ exports[`v2 should generate: ./test/generated/v2/models/ArrayWithStrings.ts 1`] export type ArrayWithStrings = Array;" `; +exports[`v2 should generate: ./test/generated/v2/models/CommentWithBackticks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing backticks in string: \`backticks\` and \`\`\`multiple backticks\`\`\` should work + */ +export type CommentWithBackticks = number;" +`; + +exports[`v2 should generate: ./test/generated/v2/models/CommentWithBreaks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing multiline comments in string: First line + * Second line + * + * Fourth line + */ +export type CommentWithBreaks = number;" +`; + +exports[`v2 should generate: ./test/generated/v2/models/CommentWithExpressionPlaceholders.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing expression placeholders in string: \${expression} should work + */ +export type CommentWithExpressionPlaceholders = number;" +`; + +exports[`v2 should generate: ./test/generated/v2/models/CommentWithQuotes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing quotes in string: 'single quote''' and \\"double quotes\\"\\"\\" should work + */ +export type CommentWithQuotes = number;" +`; + +exports[`v2 should generate: ./test/generated/v2/models/CommentWithReservedCharacters.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing reserved characters in string: * inline * and ** inline ** should work + */ +export type CommentWithReservedCharacters = number;" +`; + +exports[`v2 should generate: ./test/generated/v2/models/CommentWithSlashes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing slashes in string: \\\\backwards\\\\\\\\\\\\ and /forwards/// should work + */ +export type CommentWithSlashes = number;" +`; + exports[`v2 should generate: ./test/generated/v2/models/Date.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1225,20 +1304,6 @@ export type ModelWithString = { " `; -exports[`v2 should generate: ./test/generated/v2/models/MultilineComment.ts 1`] = ` -"/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ - -/** - * Testing multiline comments. - * This must go to the next line. - * - * This will contain a break. - */ -export type MultilineComment = number;" -`; - exports[`v2 should generate: ./test/generated/v2/models/SimpleBoolean.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1389,6 +1454,69 @@ export const $ArrayWithStrings = { } as const;" `; +exports[`v2 should generate: ./test/generated/v2/schemas/$CommentWithBackticks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithBackticks = { + type: 'number', + description: \`Testing backticks in string: \\\\\`backticks\\\\\` and \\\\\`\\\\\`\\\\\`multiple backticks\\\\\`\\\\\`\\\\\` should work\`, +} as const;" +`; + +exports[`v2 should generate: ./test/generated/v2/schemas/$CommentWithBreaks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithBreaks = { + type: 'number', + description: \`Testing multiline comments in string: First line + Second line + + Fourth line\`, +} as const;" +`; + +exports[`v2 should generate: ./test/generated/v2/schemas/$CommentWithExpressionPlaceholders.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithExpressionPlaceholders = { + type: 'number', + description: \`Testing expression placeholders in string: \\\\\${expression} should work\`, +} as const;" +`; + +exports[`v2 should generate: ./test/generated/v2/schemas/$CommentWithQuotes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithQuotes = { + type: 'number', + description: \`Testing quotes in string: 'single quote''' and \\"double quotes\\"\\"\\" should work\`, +} as const;" +`; + +exports[`v2 should generate: ./test/generated/v2/schemas/$CommentWithReservedCharacters.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithReservedCharacters = { + type: 'number', + description: \`Testing reserved characters in string: /* inline */ and /** inline **/ should work\`, +} as const;" +`; + +exports[`v2 should generate: ./test/generated/v2/schemas/$CommentWithSlashes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithSlashes = { + type: 'number', + description: \`Testing slashes in string: \\\\\\\\backwards\\\\\\\\\\\\\\\\\\\\\\\\ and /forwards/// should work\`, +} as const;" +`; + exports[`v2 should generate: ./test/generated/v2/schemas/$Date.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -1936,19 +2064,6 @@ export const $ModelWithString = { } as const;" `; -exports[`v2 should generate: ./test/generated/v2/schemas/$MultilineComment.ts 1`] = ` -"/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ -export const $MultilineComment = { - type: 'number', - description: \`Testing multiline comments. - * This must go to the next line. - * - * This will contain a break.\`, -} as const;" -`; - exports[`v2 should generate: ./test/generated/v2/schemas/$SimpleBoolean.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -3251,6 +3366,12 @@ export type { ArrayWithNumbers } from './models/ArrayWithNumbers'; export type { ArrayWithProperties } from './models/ArrayWithProperties'; export type { ArrayWithReferences } from './models/ArrayWithReferences'; export type { ArrayWithStrings } from './models/ArrayWithStrings'; +export type { CommentWithBackticks } from './models/CommentWithBackticks'; +export type { CommentWithBreaks } from './models/CommentWithBreaks'; +export type { CommentWithExpressionPlaceholders } from './models/CommentWithExpressionPlaceholders'; +export type { CommentWithQuotes } from './models/CommentWithQuotes'; +export type { CommentWithReservedCharacters } from './models/CommentWithReservedCharacters'; +export type { CommentWithSlashes } from './models/CommentWithSlashes'; export type { CompositionBaseModel } from './models/CompositionBaseModel'; export type { CompositionExtendedModel } from './models/CompositionExtendedModel'; export type { CompositionWithAllOfAndNullable } from './models/CompositionWithAllOfAndNullable'; @@ -3292,7 +3413,6 @@ export type { ModelWithPattern } from './models/ModelWithPattern'; export type { ModelWithProperties } from './models/ModelWithProperties'; export type { ModelWithReference } from './models/ModelWithReference'; export type { ModelWithString } from './models/ModelWithString'; -export type { MultilineComment } from './models/MultilineComment'; export type { SimpleBoolean } from './models/SimpleBoolean'; export type { SimpleFile } from './models/SimpleFile'; export type { SimpleInteger } from './models/SimpleInteger'; @@ -3306,6 +3426,12 @@ export { $ArrayWithNumbers } from './schemas/$ArrayWithNumbers'; export { $ArrayWithProperties } from './schemas/$ArrayWithProperties'; export { $ArrayWithReferences } from './schemas/$ArrayWithReferences'; export { $ArrayWithStrings } from './schemas/$ArrayWithStrings'; +export { $CommentWithBackticks } from './schemas/$CommentWithBackticks'; +export { $CommentWithBreaks } from './schemas/$CommentWithBreaks'; +export { $CommentWithExpressionPlaceholders } from './schemas/$CommentWithExpressionPlaceholders'; +export { $CommentWithQuotes } from './schemas/$CommentWithQuotes'; +export { $CommentWithReservedCharacters } from './schemas/$CommentWithReservedCharacters'; +export { $CommentWithSlashes } from './schemas/$CommentWithSlashes'; export { $CompositionBaseModel } from './schemas/$CompositionBaseModel'; export { $CompositionExtendedModel } from './schemas/$CompositionExtendedModel'; export { $CompositionWithAllOfAndNullable } from './schemas/$CompositionWithAllOfAndNullable'; @@ -3347,7 +3473,6 @@ export { $ModelWithPattern } from './schemas/$ModelWithPattern'; export { $ModelWithProperties } from './schemas/$ModelWithProperties'; export { $ModelWithReference } from './schemas/$ModelWithReference'; export { $ModelWithString } from './schemas/$ModelWithString'; -export { $MultilineComment } from './schemas/$MultilineComment'; export { $SimpleBoolean } from './schemas/$SimpleBoolean'; export { $SimpleFile } from './schemas/$SimpleFile'; export { $SimpleInteger } from './schemas/$SimpleInteger'; @@ -3449,6 +3574,75 @@ exports[`v3 should generate: ./test/generated/v3/models/ArrayWithStrings.ts 1`] export type ArrayWithStrings = Array;" `; +exports[`v3 should generate: ./test/generated/v3/models/CommentWithBackticks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing backticks in string: \`backticks\` and \`\`\`multiple backticks\`\`\` should work + */ +export type CommentWithBackticks = number;" +`; + +exports[`v3 should generate: ./test/generated/v3/models/CommentWithBreaks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing multiline comments in string: First line + * Second line + * + * Fourth line + */ +export type CommentWithBreaks = number;" +`; + +exports[`v3 should generate: ./test/generated/v3/models/CommentWithExpressionPlaceholders.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing expression placeholders in string: \${expression} should work + */ +export type CommentWithExpressionPlaceholders = number;" +`; + +exports[`v3 should generate: ./test/generated/v3/models/CommentWithQuotes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing quotes in string: 'single quote''' and \\"double quotes\\"\\"\\" should work + */ +export type CommentWithQuotes = number;" +`; + +exports[`v3 should generate: ./test/generated/v3/models/CommentWithReservedCharacters.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing reserved characters in string: * inline * and ** inline ** should work + */ +export type CommentWithReservedCharacters = number;" +`; + +exports[`v3 should generate: ./test/generated/v3/models/CommentWithSlashes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ + +/** + * Testing slashes in string: \\\\backwards\\\\\\\\\\\\ and /forwards/// should work + */ +export type CommentWithSlashes = number;" +`; + exports[`v3 should generate: ./test/generated/v3/models/CompositionBaseModel.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -4201,20 +4395,6 @@ export type ModelWithString = { " `; -exports[`v3 should generate: ./test/generated/v3/models/MultilineComment.ts 1`] = ` -"/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ - -/** - * Testing multiline comments. - * This must go to the next line. - * - * This will contain a break. - */ -export type MultilineComment = number;" -`; - exports[`v3 should generate: ./test/generated/v3/models/SimpleBoolean.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -4365,6 +4545,69 @@ export const $ArrayWithStrings = { } as const;" `; +exports[`v3 should generate: ./test/generated/v3/schemas/$CommentWithBackticks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithBackticks = { + type: 'number', + description: \`Testing backticks in string: \\\\\`backticks\\\\\` and \\\\\`\\\\\`\\\\\`multiple backticks\\\\\`\\\\\`\\\\\` should work\`, +} as const;" +`; + +exports[`v3 should generate: ./test/generated/v3/schemas/$CommentWithBreaks.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithBreaks = { + type: 'number', + description: \`Testing multiline comments in string: First line + Second line + + Fourth line\`, +} as const;" +`; + +exports[`v3 should generate: ./test/generated/v3/schemas/$CommentWithExpressionPlaceholders.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithExpressionPlaceholders = { + type: 'number', + description: \`Testing expression placeholders in string: \\\\\${expression} should work\`, +} as const;" +`; + +exports[`v3 should generate: ./test/generated/v3/schemas/$CommentWithQuotes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithQuotes = { + type: 'number', + description: \`Testing quotes in string: 'single quote''' and \\"double quotes\\"\\"\\" should work\`, +} as const;" +`; + +exports[`v3 should generate: ./test/generated/v3/schemas/$CommentWithReservedCharacters.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithReservedCharacters = { + type: 'number', + description: \`Testing reserved characters in string: /* inline */ and /** inline **/ should work\`, +} as const;" +`; + +exports[`v3 should generate: ./test/generated/v3/schemas/$CommentWithSlashes.ts 1`] = ` +"/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $CommentWithSlashes = { + type: 'number', + description: \`Testing slashes in string: \\\\\\\\backwards\\\\\\\\\\\\\\\\\\\\\\\\ and /forwards/// should work\`, +} as const;" +`; + exports[`v3 should generate: ./test/generated/v3/schemas/$CompositionBaseModel.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ @@ -5239,19 +5482,6 @@ export const $ModelWithString = { } as const;" `; -exports[`v3 should generate: ./test/generated/v3/schemas/$MultilineComment.ts 1`] = ` -"/* istanbul ignore file */ -/* tslint:disable */ -/* eslint-disable */ -export const $MultilineComment = { - type: 'number', - description: \`Testing multiline comments. - * This must go to the next line. - * - * This will contain a break.\`, -} as const;" -`; - exports[`v3 should generate: ./test/generated/v3/schemas/$SimpleBoolean.ts 1`] = ` "/* istanbul ignore file */ /* tslint:disable */ diff --git a/test/spec/v2.json b/test/spec/v2.json index 6cf4a73f..bf13a84e 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -856,8 +856,28 @@ } }, "definitions": { - "MultilineComment": { - "description": "Testing multiline comments.\nThis must go to the next line.\n\nThis will contain a break.", + "CommentWithBreaks": { + "description": "Testing multiline comments in string: First line\nSecond line\n\nFourth line", + "type": "integer" + }, + "CommentWithBackticks": { + "description": "Testing backticks in string: `backticks` and ```multiple backticks``` should work", + "type": "integer" + }, + "CommentWithSlashes": { + "description": "Testing slashes in string: \\backwards\\\\\\ and /forwards/// should work", + "type": "integer" + }, + "CommentWithExpressionPlaceholders": { + "description": "Testing expression placeholders in string: ${expression} should work", + "type": "integer" + }, + "CommentWithQuotes": { + "description": "Testing quotes in string: 'single quote''' and \"double quotes\"\"\" should work", + "type": "integer" + }, + "CommentWithReservedCharacters": { + "description": "Testing reserved characters in string: /* inline */ and /** inline **/ should work", "type": "integer" }, "SimpleInteger": { diff --git a/test/spec/v3.json b/test/spec/v3.json index b574b91c..2d816d70 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -1429,8 +1429,28 @@ } }, "schemas": { - "MultilineComment": { - "description": "Testing multiline comments.\nThis must go to the next line.\n\nThis will contain a break.", + "CommentWithBreaks": { + "description": "Testing multiline comments in string: First line\nSecond line\n\nFourth line", + "type": "integer" + }, + "CommentWithBackticks": { + "description": "Testing backticks in string: `backticks` and ```multiple backticks``` should work", + "type": "integer" + }, + "CommentWithSlashes": { + "description": "Testing slashes in string: \\backwards\\\\\\ and /forwards/// should work", + "type": "integer" + }, + "CommentWithExpressionPlaceholders": { + "description": "Testing expression placeholders in string: ${expression} should work", + "type": "integer" + }, + "CommentWithQuotes": { + "description": "Testing quotes in string: 'single quote''' and \"double quotes\"\"\" should work", + "type": "integer" + }, + "CommentWithReservedCharacters": { + "description": "Testing reserved characters in string: /* inline */ and /** inline **/ should work", "type": "integer" }, "SimpleInteger": { @@ -1873,7 +1893,7 @@ }, "radius": { "type": "number" - } + } } }, "ModelSquare": {