Skip to content
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

Fix for union types of union types in Records. #1926

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 32 additions & 0 deletions src/NodeParser/MappedTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { derefAnnotatedType, derefType } from "../Utils/derefType.js";
import { getKey } from "../Utils/nodeKey.js";
import { preserveAnnotation } from "../Utils/preserveAnnotation.js";
import { removeUndefined } from "../Utils/removeUndefined.js";
import { AliasType } from "../Type/AliasType.js";

export class MappedTypeNodeParser implements SubNodeParser {
public constructor(
Expand Down Expand Up @@ -104,7 +105,38 @@ export class MappedTypeNodeParser implements SubNodeParser {
protected getProperties(node: ts.MappedTypeNode, keyListType: UnionType, context: Context): ObjectProperty[] {
return keyListType
.getTypes()
.flatMap((type) => {
if (type instanceof LiteralType) {
return type;
} else if (type instanceof AliasType) {
const itemsToProcess = [type.getType()];
const processedTypes = [];
while (itemsToProcess.length > 0) {
const currentType = itemsToProcess[0];
if (currentType instanceof LiteralType) {
processedTypes.push(currentType);
} else if (currentType instanceof AliasType) {
itemsToProcess.push(currentType.getType());
} else if (currentType instanceof UnionType) {
itemsToProcess.push(...currentType.getTypes());
}
itemsToProcess.shift();
}
return processedTypes;
}
return [];
})
.filter((type): type is LiteralType => type instanceof LiteralType)
.reduce((acc: LiteralType[], curr: LiteralType) => {
if (
acc.findIndex((val: LiteralType) => {
return val.getId() === curr.getId();
}) < 0
) {
acc.push(curr);
}
return acc;
}, [])
.map((type) => [type, this.mapKey(node, type, context)])
.filter((value): value is [LiteralType, LiteralType] => value[1] instanceof LiteralType)
.reduce((result: ObjectProperty[], [key, mappedKey]: [LiteralType, LiteralType]) => {
Expand Down
1 change: 1 addition & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function assertSchema(
// skip full check if we are not encoding refs
validateFormats: config.encodeRefs === false ? undefined : true,
keywords: config.markdownDescription ? ["markdownDescription"] : undefined,
allowUnionTypes: true,
Copy link
Member

Choose a reason for hiding this comment

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

Let's not add this here.

});

addFormats(validator);
Expand Down
1 change: 1 addition & 0 deletions test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe("valid-data-type", () => {
it("type-mapped-additional-props", assertValidSchema("type-mapped-additional-props", "MyObject"));
it("type-mapped-array", assertValidSchema("type-mapped-array", "MyObject"));
it("type-mapped-union-intersection", assertValidSchema("type-mapped-union-intersection", "MyObject"));
it("type-mapped-union-union", assertValidSchema("type-mapped-union-union", "MyType"));
it("type-mapped-enum", assertValidSchema("type-mapped-enum", "MyObject"));
it("type-mapped-enum-optional", assertValidSchema("type-mapped-enum-optional", "MyObject"));
it("type-mapped-enum-null", assertValidSchema("type-mapped-enum-null", "MyObject"));
Expand Down
6 changes: 6 additions & 0 deletions test/valid-data/type-mapped-union-union/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type MyType1 = "s1";
type MyType2 = MyType1 | "s2" | "s3";
type MyType3 = MyType2 | "s4" | "s5";
type MyType10 = MyType3 | MyType2 | 's6';

export type MyType = Record<MyType10, string>;
40 changes: 40 additions & 0 deletions test/valid-data/type-mapped-union-union/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"$ref": "#/definitions/MyType",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyType": {
"additionalProperties": {
"type": "string"
},
"properties": {
"s1": {
"type": "string"
},
"s2": {
"type": "string"
},
"s3": {
"type": "string"
},
"s4": {
"type": "string"
},
"s5": {
"type": "string"
},
"s6": {
"type": "string"
}
},
"required": [
"s4",
"s5",
"s2",
"s3",
"s1",
"s6"
],
"type": "object"
}
}
}
Loading