Skip to content
This repository was archived by the owner on Jun 22, 2023. It is now read-only.

Commit 59747b1

Browse files
author
Boris Cherny
committedMay 15, 2022
Add additionalProperties option (fix bcherny#335)
1 parent 8b3267f commit 59747b1

10 files changed

+198
-9
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
8383

8484
| key | type | default | description |
8585
|-|-|-|-|
86+
| additionalProperties | boolean | `true` | Default value for `additionalProperties`, when it is not explicitly set |
8687
| bannerComment | string | `"/* tslint:disable */\n/**\n* This file was automatically generated by json-schema-to-typescript.\n* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,\n* and run json-schema-to-typescript to regenerate this file.\n*/"` | Disclaimer comment prepended to the top of each generated file |
8788
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
8889
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |

‎src/cli.ts

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ With no OUT_FILE nor IN_FILE, write to standard output.
151151
You can use any of the following options by adding them at the end.
152152
Boolean values can be set to false using the 'no-' prefix.
153153
154+
--additionalProperties
155+
Default value for additionalProperties, when it is not explicitly set
154156
--cwd=XXX
155157
Root directory for resolving $ref
156158
--declareExternallyReferenced

‎src/index.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import {link} from './linker'
1818
export {EnumJSONSchema, JSONSchema, NamedEnumJSONSchema, CustomTypeJSONSchema} from './types/JSONSchema'
1919

2020
export interface Options {
21+
/**
22+
* [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s
23+
*/
24+
$refOptions: $RefOptions
25+
/**
26+
* Default value for additionalProperties, when it is not explicitly set.
27+
*/
28+
additionalProperties: boolean
2129
/**
2230
* Disclaimer comment prepended to the top of each generated file.
2331
*/
@@ -60,14 +68,11 @@ export interface Options {
6068
* Generate unknown type instead of any
6169
*/
6270
unknownAny: boolean
63-
/**
64-
* [$RefParser](https://github.com/BigstickCarpet/json-schema-ref-parser) Options, used when resolving `$ref`s
65-
*/
66-
$refOptions: $RefOptions
6771
}
6872

6973
export const DEFAULT_OPTIONS: Options = {
7074
$refOptions: {},
75+
additionalProperties: true, // TODO: default to empty schema (as per spec) instead
7176
bannerComment: `/* tslint:disable */
7277
/**
7378
* This file was automatically generated by json-schema-to-typescript.

‎src/normalizer.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ rules.set('Transform `required`=false to `required`=[]', schema => {
4444
}
4545
})
4646

47-
// TODO: default to empty schema (as per spec) instead
48-
rules.set('Default additionalProperties to true', schema => {
47+
rules.set('Default additionalProperties', (schema, _, options) => {
4948
if (isObjectType(schema) && !('additionalProperties' in schema) && schema.patternProperties === undefined) {
50-
schema.additionalProperties = true
49+
schema.additionalProperties = options.additionalProperties
5150
}
5251
})
5352

‎test/__snapshots__/test/test.ts.md

+79-2
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ Generated by [AVA](https://avajs.dev).
9595
}␊
9696
`
9797

98-
## additionalProperties.js
98+
## additionalProperties.1.js
9999

100-
> Expected output to match snapshot for e2e test: additionalProperties.js
100+
> Expected output to match snapshot for e2e test: additionalProperties.1.js
101101
102102
`/* tslint:disable */␊
103103
/**␊
@@ -112,6 +112,61 @@ Generated by [AVA](https://avajs.dev).
112112
}␊
113113
`
114114

115+
## additionalProperties.2.js
116+
117+
> Expected output to match snapshot for e2e test: additionalProperties.2.js
118+
119+
`/* tslint:disable */␊
120+
/**␊
121+
* This file was automatically generated by json-schema-to-typescript.␊
122+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
123+
* and run json-schema-to-typescript to regenerate this file.␊
124+
*/␊
125+
126+
export interface AdditionalPropertiesConfiguredToDefaultToFalse {␊
127+
a?: {};␊
128+
b?: {};␊
129+
c?: {␊
130+
[k: string]: unknown;␊
131+
};␊
132+
d?: {␊
133+
[k: string]: number;␊
134+
};␊
135+
e?: E;␊
136+
}␊
137+
export interface E {}␊
138+
`
139+
140+
## additionalProperties.3.js
141+
142+
> Expected output to match snapshot for e2e test: additionalProperties.3.js
143+
144+
`/* tslint:disable */␊
145+
/**␊
146+
* This file was automatically generated by json-schema-to-typescript.␊
147+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
148+
* and run json-schema-to-typescript to regenerate this file.␊
149+
*/␊
150+
151+
export interface AdditionalPropertiesDefaultToTrue {␊
152+
a?: {␊
153+
[k: string]: unknown;␊
154+
};␊
155+
b?: {};␊
156+
c?: {␊
157+
[k: string]: unknown;␊
158+
};␊
159+
d?: {␊
160+
[k: string]: number;␊
161+
};␊
162+
e?: E;␊
163+
[k: string]: unknown;␊
164+
}␊
165+
export interface E {␊
166+
[k: string]: unknown;␊
167+
}␊
168+
`
169+
115170
## allOf.js
116171

117172
> Expected output to match snapshot for e2e test: allOf.js
@@ -11270,6 +11325,28 @@ Generated by [AVA](https://avajs.dev).
1127011325
]␊
1127111326
}`
1127211327

11328+
## Default additionalProperties to false
11329+
11330+
> Snapshot 1
11331+
11332+
`{␊
11333+
"id": "foo",␊
11334+
"type": "object",␊
11335+
"properties": {␊
11336+
"a": {␊
11337+
"type": "integer",␊
11338+
"id": "a"␊
11339+
},␊
11340+
"b": {␊
11341+
"type": "object",␊
11342+
"required": [],␊
11343+
"additionalProperties": false␊
11344+
}␊
11345+
},␊
11346+
"required": [],␊
11347+
"additionalProperties": false␊
11348+
}`
11349+
1127311350
## Default additionalProperties to true
1127411351

1127511352
> Snapshot 1

‎test/__snapshots__/test/test.ts.snap

192 Bytes
Binary file not shown.
File renamed without changes.

‎test/e2e/additionalProperties.2.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const input = {
2+
title: 'AdditionalProperties (configured to default to false)',
3+
type: 'object',
4+
definitions: {
5+
e: {
6+
type: 'object'
7+
}
8+
},
9+
properties: {
10+
a: {
11+
type: 'object'
12+
},
13+
b: {
14+
type: 'object',
15+
additionalProperties: false
16+
},
17+
c: {
18+
type: 'object',
19+
additionalProperties: true
20+
},
21+
d: {
22+
type: 'object',
23+
additionalProperties: {
24+
type: 'number'
25+
}
26+
},
27+
e: {
28+
$ref: '#/definitions/e'
29+
}
30+
}
31+
}
32+
33+
export const options = {
34+
additionalProperties: false
35+
}

‎test/e2e/additionalProperties.3.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const input = {
2+
title: 'AdditionalProperties (default to true)',
3+
type: 'object',
4+
definitions: {
5+
e: {
6+
type: 'object'
7+
}
8+
},
9+
properties: {
10+
a: {
11+
type: 'object'
12+
},
13+
b: {
14+
type: 'object',
15+
additionalProperties: false
16+
},
17+
c: {
18+
type: 'object',
19+
additionalProperties: true
20+
},
21+
d: {
22+
type: 'object',
23+
additionalProperties: {
24+
type: 'number'
25+
}
26+
},
27+
e: {
28+
$ref: '#/definitions/e'
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "Default additionalProperties to false",
3+
"in": {
4+
"id": "foo",
5+
"type": [
6+
"object"
7+
],
8+
"properties": {
9+
"a": {
10+
"type": "integer",
11+
"id": "a"
12+
},
13+
"b": {
14+
"type": "object"
15+
}
16+
},
17+
"required": []
18+
},
19+
"options": {
20+
"additionalProperties": false
21+
},
22+
"out": {
23+
"id": "foo",
24+
"type": "object",
25+
"properties": {
26+
"a": {
27+
"type": "integer",
28+
"id": "a"
29+
},
30+
"b": {
31+
"additionalProperties": false,
32+
"required": [],
33+
"type": "object"
34+
}
35+
},
36+
"required": [],
37+
"additionalProperties": false
38+
}
39+
}

0 commit comments

Comments
 (0)
This repository has been archived.