Skip to content
Merged
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
16 changes: 14 additions & 2 deletions packages/quicktype-core/src/language/CJSON/CJSONRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ export class CJSONRenderer extends ConvenienceRenderer {
const combinedName = allUpperWordStyle(
this.sourcelikeToString(enumName),
);
let isFirst = true;
this.forEachEnumCase(enumType, "none", (name, jsonName) => {
if (enumValues !== undefined) {
const [enumValue] = getAccessorName(
Expand All @@ -444,11 +445,22 @@ export class CJSONRenderer extends ConvenienceRenderer {
",",
);
} else {
this.emitLine(combinedName, "_", name, ",");
this.emitLine(
combinedName,
"_",
name,
isFirst ? " = 1," : ",",
);
}
} else {
this.emitLine(combinedName, "_", name, ",");
this.emitLine(
combinedName,
"_",
name,
isFirst ? " = 1," : ",",
);
}
isFirst = false;
});
},
"",
Expand Down
37 changes: 37 additions & 0 deletions test/unit/cjson-enum-default.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { InputData, JSONSchemaInput, quicktype } from "quicktype-core";
import { describe, expect, test } from "vitest";

const schema = JSON.stringify({
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
subscription: {
type: "string",
enum: ["state", "config", "heartbeat"],
},
},
required: ["subscription"],
});

async function cJSONOutput(): Promise<string> {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({ name: "TopLevel", schema });
const inputData = new InputData();
inputData.addInput(schemaInput);

const result = await quicktype({ inputData, lang: "cjson" });
return result.lines.join("\n");
}

describe("cJSON enum invalid value", () => {
test("does not collide with a real enumerator", async () => {
const output = await cJSONOutput();

expect(output).toContain(`enum Subscription {
SUBSCRIPTION_CONFIG = 1,
SUBSCRIPTION_HEARTBEAT,
SUBSCRIPTION_STATE,
};`);
expect(output).toContain("enum Subscription x = 0;");
});
});
Loading