Skip to content

Commit

Permalink
Fix crash when a field has 'deprecated', but not 'description' (bcher…
Browse files Browse the repository at this point in the history
…ny#549)

* Add failing test case

* Fix crash when deprecated is defined without description

* Add test case for bcherny#540 too
  • Loading branch information
danmichaelo authored Aug 29, 2023
1 parent 699eda8 commit 2887a52
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,14 @@ function generateInterface(ast: TInterface, options: Options): string {
)
}

function generateComment(comment: string, deprecated?: boolean): string {
function generateComment(comment?: string, deprecated?: boolean): string {
const commentLines = ['/**']
if (deprecated) {
commentLines.push(' * @deprecated')
}
commentLines.push(...comment.split('\n').map(_ => ' * ' + _))
if (typeof comment !== 'undefined') {
commentLines.push(...comment.split('\n').map(_ => ' * ' + _))
}
commentLines.push(' */')
return commentLines.join('\n')
}
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,6 @@ Generated by [AVA](https://avajs.dev).
export interface ExampleSchema {␊
/**␊
* @deprecated␊
* nested comment␊
*/␊
firstName: string;␊
/**␊
Expand All @@ -839,6 +838,7 @@ Generated by [AVA](https://avajs.dev).
* nested comment␊
*/␊
lastName?: string;␊
description?: string;␊
}␊
`

Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
6 changes: 5 additions & 1 deletion test/e2e/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export const input = {
deprecated: true,
description: 'comment',
properties: {
// https://github.com/bcherny/json-schema-to-typescript/issues/548
firstName: {
type: 'string',
deprecated: true,
description: 'nested comment',
},
middleName: {
type: 'string',
Expand All @@ -20,6 +20,10 @@ export const input = {
deprecated: false,
description: 'nested comment',
},
// https://github.com/bcherny/json-schema-to-typescript/issues/540
description: {
type: 'string',
},
},
additionalProperties: false,
required: ['firstName'],
Expand Down

0 comments on commit 2887a52

Please sign in to comment.