Skip to content

Commit 0876146

Browse files
committed
fix: 🐛 resolve types when computing discriminator
1 parent 033ae2d commit 0876146

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/type/classes/RefType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class RefType<T extends Type = any> extends AbsType<schema.RefSchema<Sche
1616
return options as any;
1717
}
1818

19+
public resolve(): Type {
20+
return this.getSystem().resolve(this.ref()).type as Type;
21+
}
22+
1923
public toStringTitle(tab: string = ''): string {
2024
const options = this.toStringOptions();
2125
return `${super.toStringTitle()} → [${this.schema.ref}]` + (options ? ` ${options}` : '');

src/type/discriminator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ArrType, BoolType, ConType, NumType, type ObjKeyType, ObjType, StrType} from './classes';
22
import type {Expr} from '@jsonjoy.com/json-expression';
3-
import type {Type} from './types';
3+
import type {RefType, Type} from './types';
44

55
/**
66
* Discriminator class for automatically identifying distinguishing patterns in
@@ -78,9 +78,11 @@ export class Discriminator {
7878
const length = types.length;
7979
const specifiers = new Set<string>();
8080
const discriminators: Discriminator[] = [];
81+
// TODO: expand all "or" types...
8182
for (let i = 1; i < length; i++) {
8283
const type = types[i];
83-
const d = Discriminator.find(type);
84+
const resolved = type.kind() === 'ref' ? (type as RefType).resolve() : type;
85+
const d = Discriminator.find(resolved);
8486
const specifier = d.toSpecifier();
8587
if (specifiers.has(specifier)) throw new Error('Duplicate discriminator: ' + specifier);
8688
specifiers.add(specifier);

0 commit comments

Comments
 (0)