Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/TypeFormatter/UnionTypeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export class UnionTypeFormatter implements SubTypeFormatter {
const kindTypes = type
.getTypes()
.filter((item) => !(derefType(item) instanceof NeverType))
.map((item) => getTypeByKey(item, new LiteralType(discriminator)));
.map((item) => {
const propertyType = getTypeByKey(item, new LiteralType(discriminator));

return propertyType ? derefType(propertyType) : undefined;
Comment thread
arthurfiorette marked this conversation as resolved.
});

const undefinedIndex = kindTypes.findIndex((item) => item === undefined);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { assertValidSchema } from "../../utils";
import { test } from "node:test";

test("dereference-alias-types-discriminated-unions", assertValidSchema("dereference-alias-types-discriminated-unions", "MyUnion"));
Comment thread
arthurfiorette marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type KindA = "kind_a";
type KindB = "kind_b";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if you export KindB? will it keep being a reference instead of inlining like

{
                            "const": "kind_a",
                            "type": "string"
                        },

?


interface ObjectA {
kind: KindA;
value: number;
Comment thread
arthurfiorette marked this conversation as resolved.
}

interface ObjectB {
kind: KindB;
name: string;
}

export type MyUnion = ObjectA | ObjectB;
Comment on lines +13 to +14

Copilot AI Feb 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixture is named/used as a discriminated-union regression test, but MyUnion is not annotated with @discriminator kind, so UnionTypeFormatter.getJsonSchemaDiscriminatorDefinition() (and the new deref logic) will never run. Add a JSDoc @discriminator kind on MyUnion to actually exercise the discriminated-union code path.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is right, the PR attempts to fix discriminated unions but your union is not discriminated

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"$ref": "#/definitions/MyUnion",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyUnion": {
"anyOf": [
{
Comment on lines +5 to +7

Copilot AI Feb 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is meant to validate discriminated-union output, the expected schema should reflect the @discriminator handling (i.e., a definitions.MyUnion object with allOf containing if/then branches and a discriminator properties.kind.enum). The current expected schema is a plain anyOf of the variants, which does not exercise getJsonSchemaDiscriminatorDefinition() or the new dereferencing behavior.

Copilot uses AI. Check for mistakes.
"additionalProperties": false,
"properties": {
"kind": {
"const": "kind_a",
"type": "string"
},
"value": {
"type": "number"
}
},
"required": [
"kind",
"value"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"kind": {
"const": "kind_b",
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"kind",
"name"
],
"type": "object"
}
]
}
}
}
Loading