Skip to content

Commit 317586d

Browse files
committedJan 6, 2025
fix crash caused by empty anyOf array
1 parent 25c1d27 commit 317586d

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed
 

‎src/generator.ts

+6
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ function generateRawType(ast: AST, options: Options): string {
174174
case 'ARRAY':
175175
return (() => {
176176
const type = generateType(ast.params, options)
177+
if (!type) {
178+
return 'never'
179+
}
177180
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]'
178181
})()
179182
case 'BOOLEAN':
@@ -289,6 +292,9 @@ function generateRawType(ast: AST, options: Options): string {
289292
function generateSetOperation(ast: TIntersection | TUnion, options: Options): string {
290293
const members = (ast as TUnion).params.map(_ => generateType(_, options))
291294
const separator = ast.type === 'UNION' ? '|' : '&'
295+
if (!members.length) {
296+
return 'never'
297+
}
292298
return members.length === 1 ? members[0] : '(' + members.join(' ' + separator + ' ') + ')'
293299
}
294300

‎test/__snapshots__/test/test.ts.md

+21
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,27 @@ Generated by [AVA](https://avajs.dev).
947947
}␊
948948
`
949949

950+
## emptySet.js
951+
952+
> Expected output to match snapshot for e2e test: emptySet.js
953+
954+
`/* eslint-disable */␊
955+
/**␊
956+
* This file was automatically generated by json-schema-to-typescript.␊
957+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
958+
* and run json-schema-to-typescript to regenerate this file.␊
959+
*/␊
960+
961+
export interface EmptySet {␊
962+
a?: never;␊
963+
b?: never;␊
964+
c?: never;␊
965+
d?: {␊
966+
[k: string]: unknown;␊
967+
};␊
968+
}␊
969+
`
970+
950971
## enum.2.js
951972

952973
> Expected output to match snapshot for e2e test: enum.2.js

‎test/__snapshots__/test/test.ts.snap

42 Bytes
Binary file not shown.

‎test/e2e/emptySet.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const input = {
2+
type: 'object',
3+
properties: {
4+
a: {anyOf: []},
5+
b: {oneOf: []},
6+
c: {allOf: []},
7+
d: {multipleOf: []},
8+
},
9+
additionalProperties: false,
10+
}

0 commit comments

Comments
 (0)
Failed to load comments.