Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invalid code generated due to empty anyOf array #634

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ function generateRawType(ast: AST, options: Options): string {
case 'ARRAY':
return (() => {
const type = generateType(ast.params, options)
if (!type) {
return 'never'
}
return type.endsWith('"') ? '(' + type + ')[]' : type + '[]'
})()
case 'BOOLEAN':
Expand Down Expand Up @@ -289,6 +292,9 @@ function generateRawType(ast: AST, options: Options): string {
function generateSetOperation(ast: TIntersection | TUnion, options: Options): string {
const members = (ast as TUnion).params.map(_ => generateType(_, options))
const separator = ast.type === 'UNION' ? '|' : '&'
if (!members.length) {
return 'never'
}
return members.length === 1 ? members[0] : '(' + members.join(' ' + separator + ' ') + ')'
}

Expand Down
21 changes: 21 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,27 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## emptySet.js

> Expected output to match snapshot for e2e test: emptySet.js

`/* eslint-disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface EmptySet {␊
a?: never;␊
b?: never;␊
c?: never;␊
d?: {␊
[k: string]: unknown;␊
};␊
}␊
`

## enum.2.js

> Expected output to match snapshot for e2e test: enum.2.js
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
10 changes: 10 additions & 0 deletions test/e2e/emptySet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const input = {
type: 'object',
properties: {
a: {anyOf: []},
b: {oneOf: []},
c: {allOf: []},
d: {multipleOf: []},
},
additionalProperties: false,
}