Skip to content

Commit

Permalink
Merge pull request #509 from notaphplover/fix/parse-enum-values-as-li…
Browse files Browse the repository at this point in the history
…terals

Update parser to parse enum values as literals
  • Loading branch information
bcherny committed Feb 23, 2023
2 parents de9b027 + cea04ae commit b73e1c7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function parseNonLiteral(
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition ?? keyName, usedNames)!,
params: schema.enum!.map((_, n) => ({
ast: parse(_, options, undefined, processed, usedNames),
ast: parseLiteral(_, undefined),
keyName: schema.tsEnumNames![n]
})),
type: 'ENUM'
Expand Down Expand Up @@ -257,7 +257,7 @@ function parseNonLiteral(
comment: schema.description,
keyName,
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames),
params: schema.enum!.map(_ => parse(_, options, undefined, processed, usedNames)),
params: schema.enum!.map(_ => parseLiteral(_, undefined)),
type: 'UNION'
}
case 'UNNAMED_SCHEMA':
Expand Down
20 changes: 20 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,26 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## enum.4.js

> Expected output to match snapshot for e2e test: enum.4.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 type EnumWithJSONLiteral =␊
| {type: "string"}␊
| {foo: 3}␊
| "foo"␊
| {foo: {bar: "baz"}}␊
| [2, 3, 4]␊
| [{foo: [4, 6]}];␊
`

## enum.js

> Expected output to match snapshot for e2e test: enum.js
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
4 changes: 4 additions & 0 deletions test/e2e/enum.4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const input = {
title: 'Enum with JSON literal',
enum: [{type: 'string'}, {foo: 3}, 'foo', {foo: {bar: 'baz'}}, [2, 3, 4], [{foo: [4, 6]}]]
}

0 comments on commit b73e1c7

Please sign in to comment.