Skip to content

Commit 2887a52

Browse files
authored
Fix crash when a field has 'deprecated', but not 'description' (#549)
* Add failing test case * Fix crash when deprecated is defined without description * Add test case for #540 too
1 parent 699eda8 commit 2887a52

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

src/generator.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,14 @@ function generateInterface(ast: TInterface, options: Options): string {
316316
)
317317
}
318318

319-
function generateComment(comment: string, deprecated?: boolean): string {
319+
function generateComment(comment?: string, deprecated?: boolean): string {
320320
const commentLines = ['/**']
321321
if (deprecated) {
322322
commentLines.push(' * @deprecated')
323323
}
324-
commentLines.push(...comment.split('\n').map(_ => ' * ' + _))
324+
if (typeof comment !== 'undefined') {
325+
commentLines.push(...comment.split('\n').map(_ => ' * ' + _))
326+
}
325327
commentLines.push(' */')
326328
return commentLines.join('\n')
327329
}

test/__snapshots__/test/test.ts.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,6 @@ Generated by [AVA](https://avajs.dev).
827827
export interface ExampleSchema {␊
828828
/**␊
829829
* @deprecated␊
830-
* nested comment␊
831830
*/␊
832831
firstName: string;␊
833832
/**␊
@@ -839,6 +838,7 @@ Generated by [AVA](https://avajs.dev).
839838
* nested comment␊
840839
*/␊
841840
lastName?: string;␊
841+
description?: string;␊
842842
}␊
843843
`
844844

test/__snapshots__/test/test.ts.snap

1.15 KB
Binary file not shown.

test/e2e/deprecated.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ export const input = {
55
deprecated: true,
66
description: 'comment',
77
properties: {
8+
// https://github.com/bcherny/json-schema-to-typescript/issues/548
89
firstName: {
910
type: 'string',
1011
deprecated: true,
11-
description: 'nested comment',
1212
},
1313
middleName: {
1414
type: 'string',
@@ -20,6 +20,10 @@ export const input = {
2020
deprecated: false,
2121
description: 'nested comment',
2222
},
23+
// https://github.com/bcherny/json-schema-to-typescript/issues/540
24+
description: {
25+
type: 'string',
26+
},
2327
},
2428
additionalProperties: false,
2529
required: ['firstName'],

0 commit comments

Comments
 (0)