import assert from "node:assert/strict"; import test from "node:test"; import type { JSONSchema7 } from "json-schema"; import { collectNestedDiscriminatedUnionTypeNames, schemaTypeToJava, } from "./java.js"; test("nested discriminated array items use their named Java type", () => { const definitions: Record = { MessageResult: { anyOf: [ { $ref: "#/definitions/MessageDelivered" }, { $ref: "#/definitions/MessageRejected" }, ], }, MessageDelivered: { type: "object", additionalProperties: false, properties: { status: { const: "delivered" }, actions: { type: "array", items: { $ref: "#/definitions/ActionChoice" }, }, }, }, MessageRejected: { type: "object", additionalProperties: false, properties: { status: { const: "rejected" }, reason: { type: "string" }, }, }, ActionChoice: { anyOf: [ { $ref: "#/definitions/PhoneAction" }, { $ref: "#/definitions/EmailAction" }, ], }, PhoneAction: { type: "object", title: "PhoneAction", additionalProperties: false, properties: { kind: { const: "phone" }, number: { type: "string" }, sources: { type: "object", additionalProperties: { $ref: "#/definitions/ActionSource" }, }, }, }, EmailAction: { type: "object", title: "EmailAction", additionalProperties: false, properties: { kind: { const: "email" }, address: { type: "string" }, sources: { type: "object", additionalProperties: { $ref: "#/definitions/ActionSource" }, }, }, }, ActionSource: { anyOf: [ { $ref: "#/definitions/LocalActionSource" }, { $ref: "#/definitions/RemoteActionSource" }, ], }, LocalActionSource: { type: "object", title: "LocalActionSource", additionalProperties: false, properties: { location: { const: "local" }, }, }, RemoteActionSource: { type: "object", title: "RemoteActionSource", additionalProperties: false, properties: { location: { const: "remote" }, url: { type: "string" }, }, }, }; const standaloneTypes = new Map(); const promotedUnionTypes = collectNestedDiscriminatedUnionTypeNames( { $ref: "#/definitions/MessageResult" }, definitions ); const result = schemaTypeToJava( { type: "array", items: { $ref: "#/definitions/ActionChoice" }, }, false, "MessageEnvelope", "actions", new Map(), { definitions, standaloneTypes, promotedUnionTypes, } ); assert.equal(result.javaType, "List"); assert.deepEqual([...standaloneTypes.keys()], ["ActionChoice"]); assert.deepEqual([...promotedUnionTypes], ["ActionChoice", "ActionSource"]); });