Skip to content

fix: fully unwrap union aliases in mapped keys to avoid generating incorrect additionalProperties #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/NodeParser/MappedTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ObjectProperty, ObjectType } from "../Type/ObjectType.js";
import { StringType } from "../Type/StringType.js";
import { SymbolType } from "../Type/SymbolType.js";
import { UnionType } from "../Type/UnionType.js";
import { derefAnnotatedType, derefType } from "../Utils/derefType.js";
import { derefAnnotatedType, derefType, isDeepLiteralUnion } from "../Utils/derefType.js";
import { getKey } from "../Utils/nodeKey.js";
import { preserveAnnotation } from "../Utils/preserveAnnotation.js";
import { removeUndefined } from "../Utils/removeUndefined.js";
Expand Down Expand Up @@ -158,6 +158,10 @@ export class MappedTypeNodeParser implements SubNodeParser {
keyListType: UnionType,
context: Context,
): BaseType | boolean {
if (isDeepLiteralUnion(keyListType)) {
return this.additionalProperties;
}

const key = keyListType.getTypes().filter((type) => !(derefType(type) instanceof LiteralType))[0];

if (key) {
Expand Down
19 changes: 19 additions & 0 deletions src/Utils/derefType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { AnnotatedType } from "../Type/AnnotatedType.js";
import type { BaseType } from "../Type/BaseType.js";
import { DefinitionType } from "../Type/DefinitionType.js";
import { HiddenType } from "../Type/HiddenType.js";
import { LiteralType } from "../Type/LiteralType.js";
import { NeverType } from "../Type/NeverType.js";
import { ReferenceType } from "../Type/ReferenceType.js";
import { UnionType } from "../Type/UnionType.js";

/**
* Dereference the type as far as possible.
Expand Down Expand Up @@ -38,6 +40,23 @@ export function isHiddenType(type: BaseType): boolean {
return false;
}

/**
* Recursively checks whether the given type is a union composed entirely of literal types.
*/
export function isDeepLiteralUnion(type: BaseType): boolean {
const resolved = derefType(type);

if (resolved instanceof LiteralType) {
return true;
}

if (resolved instanceof UnionType) {
return resolved.getTypes().every((t) => isDeepLiteralUnion(t));
}

return false;
}

export function derefAliasedType(type: BaseType): BaseType {
if (type instanceof AliasType) {
return derefAliasedType(type.getType());
Expand Down
20 changes: 15 additions & 5 deletions test/valid-data/type-mapped-pick-union-alias/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
interface SomeInterface {
foo: string;
bar: number;
a: number;
b: string;
c: boolean;
d: string[];
e: null;
}

type KeyFoo = "foo";
type KeyBar = "bar";
type A = "a";
type B = "b";
type C = "c";
type D = "d";
type E = "e";

export type PickAliasedLiteralUnion = Pick<SomeInterface, KeyFoo | KeyBar>;
type AB = A | B;
type ABC = AB | C;
type ABCD = ABC | D;

export type PickAliasedLiteralUnion = Pick<SomeInterface, ABCD | E>;
23 changes: 19 additions & 4 deletions test/valid-data/type-mapped-pick-union-alias/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@
"PickAliasedLiteralUnion": {
"additionalProperties": false,
"properties": {
"bar": {
"a": {
"type": "number"
},
"foo": {
"b": {
"type": "string"
},
"c": {
"type": "boolean"
},
"d": {
"items": {
"type": "string"
},
"type": "array"
},
"e": {
"type": "null"
}
},
"required": [
"foo",
"bar"
"a",
"b",
"c",
"d",
"e"
],
"type": "object"
}
Expand Down
4 changes: 1 addition & 3 deletions test/valid-data/type-mapped-union-union/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyType": {
"additionalProperties": {
"type": "string"
},
"additionalProperties": false,
"properties": {
"s1": {
"type": "string"
Expand Down
Loading