Repository navigation
Description
Issue Type
quicktype crash (internal error, no output).
Context (Environment, Version, Language)
Input Format: TypeScript (also JSON Schema with qt-accessors or qt-enum-values)
Output Language: all (tried C#, Python, Go, TypeScript, C (cJSON))
CLI, npm, or app.quicktype.io: CLI, built from master
Version: 24.0.0 (master at c39af7d)
Description
Found while porting quicktype to Rust (quickertype). A TypeScript string
enum used in a union stops quicktype with an internal error:
Error: Internal error: Cannot combine type attribute accessorNames.
The same happens with:
export interface X { f: E[] | F }(with a second string enumF);- two string enums with the same values,
export enum F { A = "a", B = "b" }
andexport interface X { e: E; f: F }; export interface X { f?: E; g?: E }with--lang python(C# accepts
this one).
The same declarations with a string-literal type alias,
export type E = "a" | "b";, work, and so does f: E on its own.
It is not specific to TypeScript. A JSON Schema with the same enum twice,
each copy with qt-accessors, fails the same way. With qt-enum-values,
the error names the enumValues attribute instead.
Input Data
e.ts:
export enum E { A = "a", B = "b" }
export interface X { f: E | null }s.schema:
{"definitions": {
"E": {"type": "string", "enum": ["a", "b"], "qt-accessors": {"a": "Alpha", "b": "Beta"}},
"F": {"type": "string", "enum": ["a", "b"], "qt-accessors": {"a": "Alpha", "b": "Other"}}}}v.schema (two identical copies):
{"definitions": {
"E": {"type": "string", "enum": ["a", "b"], "qt-enum-values": {"a": "0", "b": "1"}},
"F": {"type": "string", "enum": ["a", "b"], "qt-enum-values": {"a": "0", "b": "1"}}}}Expected Behaviour / Output
Code for the enum and the union, as for f: E, with the enum's cases
named after its members.
Current Behaviour / Output
Error: Internal error: Cannot combine type attribute accessorNames.
for e.ts (with every target language except schema) and for
s.schema, and
Error: Internal error: Cannot combine type attribute enumValues.
for v.schema.
Steps to Reproduce
node dist/index.js --src-lang typescript --lang cs e.tsnode dist/index.js --src-lang schema --lang cs --src "s.schema#/definitions/E" --src "s.schema#/definitions/F"node dist/index.js --src-lang schema --lang cjson --src "v.schema#/definitions/E" --src "v.schema#/definitions/F"
Possible Solution
The TypeScript input gives each enum's definition its member names as
qt-accessors (addEnumAccessorNames), which become the accessorNames
type attribute. AccessorNamesTypeAttributeKind
(attributes/AccessorNames.ts) does not override combine, so the base
class's combine, panic("Cannot combine type attribute ..."), runs
whenever two types with accessor names become one type.
For f: E | null, typescript-json-schema writes the enum inline in the
union as well as in the definition, and the type builder makes both the
same type. When the union is rewritten (UnifyUnionBuilder, through
TypeBuilder.getEnumType), the enum is built again with the union
member's attributes, the builder finds the existing E, and
addAttributes combines {a: "A", b: "B"} with {a: "A", b: "B"}. Two
enums with the same cases are one type too, so their attributes are
combined as well. EnumValuesTypeAttributeKind (attributes/EnumValues.ts)
has no combine either.
A fix could be:
- Accessor names: keep the entries the copies agree on, and drop a name
the copies disagree on, so that name gets its default. The common case
is the same enum meeting itself, so its member names are kept. Dropping
the name gives the same result as the alias form, and the result does
not depend on the order in which the types are combined. - Enum values: combine only when every copy is the same, and otherwise
fail as before. A value is part of what the enum means, and dropping
one could give two cases the same value. With copies
{"a": "0", "b": "1"}and{"a": "1", "b": "1"}, droppingagives C
F_A = 1, F_B = 1, and the duplicatecaselabels do not compile.
Written by Claude (an AI assistant made by Anthropic) and posted on behalf of the account owner (EloB). The reproduction, fix and tests were checked by running them as described above.
Activity