diff --git a/test/inputs/schema/nested-intersection-union.1.fail.union.json b/test/inputs/schema/nested-intersection-union.1.fail.union.json new file mode 100644 index 000000000..cedeb3f00 --- /dev/null +++ b/test/inputs/schema/nested-intersection-union.1.fail.union.json @@ -0,0 +1,8 @@ +{ + "input": [ + { + "content": "valid item" + }, + ["this array is not a valid item"] + ] +} diff --git a/test/inputs/schema/nested-intersection-union.1.json b/test/inputs/schema/nested-intersection-union.1.json new file mode 100644 index 000000000..3018c7a10 --- /dev/null +++ b/test/inputs/schema/nested-intersection-union.1.json @@ -0,0 +1,20 @@ +{ + "input": [ + { + "content": "hello", + "id": "item-1", + "output": "42", + "summary": "all fields present" + }, + { + "content": "just a message" + }, + { + "id": "item-2", + "output": "function result" + }, + { + "summary": "just reasoning" + } + ] +} diff --git a/test/inputs/schema/nested-intersection-union.schema b/test/inputs/schema/nested-intersection-union.schema new file mode 100644 index 000000000..0453188e3 --- /dev/null +++ b/test/inputs/schema/nested-intersection-union.schema @@ -0,0 +1,58 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "additionalProperties": false, + "properties": { + "input": { + "type": "array", + "items": { + "oneOf": [ + { "$ref": "#/definitions/Message" }, + { "$ref": "#/definitions/Item" } + ] + } + } + }, + "required": ["input"], + "definitions": { + "Message": { + "type": "object", + "properties": { + "content": { "type": "string" } + }, + "required": ["content"] + }, + "Item": { + "oneOf": [ + { "$ref": "#/definitions/FunctionOutput" }, + { "$ref": "#/definitions/ReasoningItem" } + ] + }, + "FunctionOutput": { + "allOf": [ + { "$ref": "#/definitions/BaseItem" }, + { + "type": "object", + "properties": { + "output": { "type": "string" } + }, + "required": ["output"] + } + ] + }, + "BaseItem": { + "type": "object", + "properties": { + "id": { "type": "string" } + }, + "required": ["id"] + }, + "ReasoningItem": { + "type": "object", + "properties": { + "summary": { "type": "string" } + }, + "required": ["summary"] + } + } +} diff --git a/test/unit/nested-intersection-union.test.ts b/test/unit/nested-intersection-union.test.ts deleted file mode 100644 index d9c3a4a5f..000000000 --- a/test/unit/nested-intersection-union.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -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: { - input: { - type: "array", - items: { - oneOf: [ - { $ref: "#/definitions/Message" }, - { $ref: "#/definitions/Item" }, - ], - }, - }, - }, - definitions: { - Message: { - type: "object", - properties: { content: { type: "string" } }, - required: ["content"], - }, - Item: { - oneOf: [ - { $ref: "#/definitions/FunctionOutput" }, - { $ref: "#/definitions/ReasoningItem" }, - ], - }, - FunctionOutput: { - allOf: [ - { $ref: "#/definitions/BaseItem" }, - { - type: "object", - properties: { output: { type: "string" } }, - required: ["output"], - }, - ], - }, - BaseItem: { - type: "object", - properties: { id: { type: "string" } }, - required: ["id"], - }, - ReasoningItem: { - type: "object", - properties: { summary: { type: "string" } }, - required: ["summary"], - }, - }, -}); - -describe("nested intersections in unions", () => { - test("flattens an array item union containing an indirect allOf", async () => { - const schemaInput = new JSONSchemaInput(undefined); - await schemaInput.addSource({ name: "TopLevel", schema }); - const inputData = new InputData(); - inputData.addInput(schemaInput); - - const result = await quicktype({ inputData, lang: "rust" }); - const output = result.lines.join("\n"); - - expect(output).toContain("pub struct Item"); - expect(output).toContain("output: Option"); - expect(output).toContain("summary: Option"); - }); -});