Skip to content

fix: const generation for not string types (#2333) #2371

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions packages/quicktype-core/src/input/JSONSchemaInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,9 @@ async function addTypesInSchema(

const includeObject = enumArray === undefined && !isConst && (typeSet === undefined || typeSet.has("object"));
const includeArray = enumArray === undefined && !isConst && (typeSet === undefined || typeSet.has("array"));
const needStringEnum =
includedTypes.has("string") &&
enumArray !== undefined &&
enumArray.find((x: any) => typeof x === "string") !== undefined;
const enumArrayHasString =
enumArray !== undefined && enumArray.find((x: any) => typeof x === "string") !== undefined;
const needStringEnum = includedTypes.has("string") && (enumArrayHasString || isConst);
const needUnion =
typeSet !== undefined ||
schema.properties !== undefined ||
Expand Down Expand Up @@ -952,7 +951,9 @@ async function addTypesInSchema(
combineProducedAttributes(({ forString }) => forString)
);

if (needStringEnum || isConst) {
if (needStringEnum) {
// FIXME: Currently isConst only works for string values because the name generation only works with strings
// to fix this issue the cases below have to support all types.
const cases = isConst
? [schema.const]
: ((enumArray as any[]).filter(x => typeof x === "string") as string[]);
Expand Down
Loading