Skip to content

Commit

Permalink
fix: imported string-key loses annotations (#1293)
Browse files Browse the repository at this point in the history
Co-authored-by: Petr Spacek <[email protected]>
  • Loading branch information
grant-d and Petr Spacek authored Jan 20, 2023
1 parent 1ea8a25 commit d32af62
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/NodeParser/MappedTypeNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SubNodeParser } from "../SubNodeParser";
import { AnnotatedType } from "../Type/AnnotatedType";
import { ArrayType } from "../Type/ArrayType";
import { BaseType } from "../Type/BaseType";
import { DefinitionType } from "../Type/DefinitionType";
import { EnumType, EnumValue } from "../Type/EnumType";
import { LiteralType } from "../Type/LiteralType";
import { NeverType } from "../Type/NeverType";
Expand Down Expand Up @@ -57,8 +58,17 @@ export class MappedTypeNodeParser implements SubNodeParser {
const type = this.childNodeParser.createType(node.type!, context);
// const resultType = type instanceof NeverType ? new NeverType() : new ObjectType(id, [], [], type);
const resultType = new ObjectType(id, [], [], type);
if (resultType && constraintType instanceof AnnotatedType) {
const annotations = constraintType.getAnnotations();
if (resultType) {
let annotations;

if (constraintType instanceof AnnotatedType) {
annotations = constraintType.getAnnotations();
} else if (constraintType instanceof DefinitionType) {
const childType = constraintType.getType();
if (childType instanceof AnnotatedType) {
annotations = childType.getAnnotations();
}
}
if (annotations) {
return new AnnotatedType(resultType, { propertyNames: annotations }, false);
}
Expand Down
2 changes: 1 addition & 1 deletion test/valid-data-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("valid-data-type", () => {
it("type-mapped-symbol", assertValidSchema("type-mapped-symbol", "MyObject"));
it("type-mapped-never", assertValidSchema("type-mapped-never", "MyObject"));
it("type-mapped-empty-exclude", assertValidSchema("type-mapped-empty-exclude", "MyObject"));
it("type-mapped-annotated-string", assertValidSchema("type-mapped-annotated-string", "ExchangeRate", "extended"));
it("type-mapped-annotated-string", assertValidSchema("type-mapped-annotated-string", "*", "extended"));

it("type-conditional-simple", assertValidSchema("type-conditional-simple", "MyObject"));
it("type-conditional-inheritance", assertValidSchema("type-conditional-inheritance", "MyObject"));
Expand Down
7 changes: 7 additions & 0 deletions test/valid-data/type-mapped-annotated-string/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Size } from "./size";

/**
* Currency
* @pattern ^[A-Z]{3,3}$
Expand All @@ -8,3 +10,8 @@ type CurrencyISO = string;
* Exchange rate
*/
export type ExchangeRate = Record<CurrencyISO, number>;

/**
* Size chart
*/
export type SizeChart = Record<Size, number>;
17 changes: 16 additions & 1 deletion test/valid-data/type-mapped-annotated-string/schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$ref": "#/definitions/ExchangeRate",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ExchangeRate": {
Expand All @@ -12,6 +11,22 @@
"pattern": "^[A-Z]{3,3}$"
},
"type": "object"
},
"Size": {
"description": "Size",
"pattern": "^S|M|L$",
"type": "string"
},
"SizeChart": {
"additionalProperties": {
"type": "number"
},
"description": "Size chart",
"propertyNames": {
"description": "Size",
"pattern": "^S|M|L$"
},
"type": "object"
}
}
}
5 changes: 5 additions & 0 deletions test/valid-data/type-mapped-annotated-string/size.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Size
* @pattern ^S|M|L$
*/
export type Size = string;

0 comments on commit d32af62

Please sign in to comment.