Skip to content

Commit b73718b

Browse files
Morten Christensendomoritz
authored andcommitted
fix for issue 85 - Generates wrong schema for readonly arrays
1 parent 9037a98 commit b73718b

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

test/programs/array-readonly/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ArrayThatIsReadonly {
2+
test: ReadonlyArray<any>;
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"properties": {
5+
"test": {
6+
"type": "array",
7+
"items": {}
8+
}
9+
},
10+
"required": [
11+
"test"
12+
]
13+
}

test/schema.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe("schema", () => {
6868
assertSchema("string-literals", "main.ts", "MyObject");
6969
assertSchema("string-literals-inline", "main.ts", "MyObject");
7070

71+
assertSchema("array-readonly", "main.ts", "ArrayThatIsReadonly");
7172
assertSchema("array-types", "main.ts", "MyArray");
7273
assertSchema("map-types", "main.ts", "MyObject");
7374

typescript-json-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export class JsonSchemaGenerator {
223223
if (value !== undefined) {
224224
definition.type = typeof value;
225225
definition.enum = [ value ];
226-
} else if (symbol && symbol.getName() === "Array") {
226+
} else if (symbol && (symbol.getName() === "Array" || symbol.getName() === "ReadonlyArray")) {
227227
const arrayType = (<ts.TypeReference>propertyType).typeArguments[0];
228228
definition.type = "array";
229229
definition.items = this.getTypeDefinition(arrayType, tc);
@@ -580,7 +580,7 @@ export class JsonSchemaGenerator {
580580
let returnedDefinition = definition; // returned definition, may be a $ref
581581

582582
const symbol = typ.getSymbol();
583-
const isRawType = (!symbol || symbol.name === "integer" || symbol.name === "Array" || symbol.name === "Date");
583+
const isRawType = (!symbol || symbol.name === "integer" || symbol.name === "Array" || symbol.name === "ReadonlyArray" || symbol.name === "Date");
584584

585585
// special case: an union where all child are string literals -> make an enum instead
586586
let isStringEnum = false;

0 commit comments

Comments
 (0)