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
2 changes: 1 addition & 1 deletion packages/quicktype-core/src/input/JSONSchemaInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export class Ref {
public get definitionName(): string | undefined {
const pe = arrayGetFromEnd(this.path, 2);
if (pe === undefined) return undefined;
if (keyOrIndex(pe) === "definitions")
if (keyOrIndex(pe) === "definitions" || keyOrIndex(pe) === "$defs")
return keyOrIndex(defined(arrayLast(this.path)));
return undefined;
}
Expand Down
7 changes: 7 additions & 0 deletions test/inputs/schema/light.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"LightParams": {
"outlet_id": "outlet-1",
"app_id": "app-1",
"rgba": "255,255,255,1"
}
}
21 changes: 21 additions & 0 deletions test/inputs/schema/light.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"LightParams": {
"type": "object",
"properties": {
"outlet_id": { "type": "string" },
"app_id": { "type": "string" },
"rgba": { "type": "string" }
},
"additionalProperties": false,
"required": ["outlet_id", "app_id", "rgba"]
}
},
"type": "object",
"properties": {
"LightParams": { "$ref": "#/$defs/LightParams" }
},
"additionalProperties": false,
"required": ["LightParams"]
}
51 changes: 51 additions & 0 deletions test/unit/json-schema-definitions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
FetchingJSONSchemaStore,
InputData,
JSONSchemaInput,
quicktype,
} from "quicktype-core";
import { expect, test } from "vitest";

const lightSchema = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$defs: {
LightParams: {
type: "object",
properties: {
outlet_id: { type: "string" },
app_id: { type: "string" },
rgba: { type: "string" },
},
additionalProperties: false,
required: ["outlet_id", "app_id", "rgba"],
},
},
type: "object",
properties: {
LightParams: { $ref: "#/$defs/LightParams" },
},
additionalProperties: false,
required: ["LightParams"],
};

test("a type under $defs keeps its definition name (issue #2778)", async () => {
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
await schemaInput.addSource({
name: "LightSchema",
schema: JSON.stringify(lightSchema),
});
const inputData = new InputData();
inputData.addInput(schemaInput);

const result = await quicktype({
inputData,
lang: "typescript",
rendererOptions: { "just-types": "true" },
});
const output = result.lines.join("\n");

// The fixture exercises this schema end-to-end, but generated symbol names
// are not observable at runtime, so assert the regression here.
expect(output).toContain("LightParams: LightParams;");
expect(output).toContain("export interface LightParams {");
});
Loading