Skip to content

Commit

Permalink
fix: schema generation when property name cannot be escaped (#2017)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bence Balogh committed Jul 9, 2024
1 parent 840ac08 commit bf9616f
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/NodeParser/TypeLiteralNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ export class TypeLiteralNodeParser implements SubNodeParser {
} catch {
// When propertyName was programmatically created, it doesn't have a source file.
// Then, getText() will throw an error. But, for programmatically created nodes,`
// `escapedText` is available.
return (propertyName as ts.Identifier).escapedText as string;
// `escapedText` or `text` is available.
// Only `text` will be available when propertyName contains strange characters or it is a number.
return ((propertyName as ts.Identifier).escapedText as string) || (propertyName as ts.StringLiteral).text;
}
}
}
2 changes: 2 additions & 0 deletions test/valid-data-struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ describe("valid-data-struct", () => {
it("structure-anonymous", assertValidSchema("structure-anonymous", "MyObject"));
it("structure-recursion", assertValidSchema("structure-recursion", "MyObject"));
it("structure-extra-props", assertValidSchema("structure-extra-props", "MyObject"));

it("string-literal-property-names-class", assertValidSchema("string-literal-property-names"));
});
34 changes: 34 additions & 0 deletions test/valid-data/string-literal-property-names/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Class props has only implicit types!
export class MyClass {
PROP_1 = '';
"PROP_2" = '';
"PROP.3" = '';
"{PROP_4}" = '';
"400" = '';
500 = '';
CHILD = {
PROP_1: '',
"PROP_2": '',
"PROP.3": '',
"{PROP_4}" : '',
"400": '',
500: '',
};
}

export interface MyInterface {
PROP_1: string;
"PROP_2": string;
"PROP.3": string;
"{PROP_4}": string;
"400": string;
500: string;
CHILD : {
PROP_1: string,
"PROP_2": string,
"PROP.3": string,
"{PROP_4}": string,
"400": string,
500: string
};
}
135 changes: 135 additions & 0 deletions test/valid-data/string-literal-property-names/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyClass": {
"additionalProperties": false,
"properties": {
"400": {
"type": "string"
},
"500": {
"type": "string"
},
"CHILD": {
"additionalProperties": false,
"properties": {
"400": {
"type": "string"
},
"500": {
"type": "string"
},
"PROP.3": {
"type": "string"
},
"PROP_1": {
"type": "string"
},
"PROP_2": {
"type": "string"
},
"{PROP_4}": {
"type": "string"
}
},
"required": [
"PROP_1",
"PROP_2",
"PROP.3",
"{PROP_4}",
"400",
"500"
],
"type": "object"
},
"PROP.3": {
"type": "string"
},
"PROP_1": {
"type": "string"
},
"PROP_2": {
"type": "string"
},
"{PROP_4}": {
"type": "string"
}
},
"required": [
"PROP_1",
"PROP_2",
"PROP.3",
"{PROP_4}",
"400",
"500",
"CHILD"
],
"type": "object"
},
"MyInterface": {
"additionalProperties": false,
"properties": {
"400": {
"type": "string"
},
"500": {
"type": "string"
},
"CHILD": {
"additionalProperties": false,
"properties": {
"400": {
"type": "string"
},
"500": {
"type": "string"
},
"PROP.3": {
"type": "string"
},
"PROP_1": {
"type": "string"
},
"PROP_2": {
"type": "string"
},
"{PROP_4}": {
"type": "string"
}
},
"required": [
"PROP_1",
"PROP_2",
"PROP.3",
"{PROP_4}",
"400",
"500"
],
"type": "object"
},
"PROP.3": {
"type": "string"
},
"PROP_1": {
"type": "string"
},
"PROP_2": {
"type": "string"
},
"{PROP_4}": {
"type": "string"
}
},
"required": [
"PROP_1",
"PROP_2",
"PROP.3",
"{PROP_4}",
"400",
"500",
"CHILD"
],
"type": "object"
}
}
}

0 comments on commit bf9616f

Please sign in to comment.