Skip to content

Commit 19d2104

Browse files
authored
Merge pull request #2 from mikunn/option-for-retaining-unsupported-fields
Option for retaining unsupported fields
2 parents b28839c + 00ae0ab commit 19d2104

File tree

3 files changed

+119
-22
lines changed

3 files changed

+119
-22
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The purpose of this project is to fill the grap by doing the conversion between
1919
* for example `type: "dateTime"` becomes `type: "string"` with `format: "date-time"`
2020
* deletes `nullable` and adds `"null"` to `type` array if `nullable` is `true`
2121
* supports deep structures with nested `allOf`s etc.
22-
* removes [OpenAPI specific properties](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-20) such as `discriminator`, `deprecated` etc.
22+
* removes [OpenAPI specific properties](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-20) such as `discriminator`, `deprecated` etc. unless specified otherwise
2323
* optionally supports `patternProperties` with `x-patternProperties` in the Schema Object
2424

2525
**NOTE**: `$ref`s are not dereferenced. Use a dereferencer such as [json-schema-ref-parser](https://www.npmjs.com/package/json-schema-ref-parser) prior to using this package.
@@ -96,6 +96,10 @@ prints
9696
}
9797
```
9898

99+
#### `keepNotSupported` (array)
100+
101+
By default, the following fields are removed from the result schema: `nullable`, `discriminator`, `readOnly`, `writeOnly`, `xml`, `externalDocs`, `example` and `deprecated` as they are not supported by JSON Schema Draft 4. Provide an array of the ones you want to keep (as strings) and they won't be removed.
102+
99103
#### `supportPatternProperties` (boolean)
100104

101105
If set to `true` and `x-patternProperties` property is present, change `x-patternProperties` to `patternProperties` and call `patternPropertiesHandler`. If `patternPropertiesHandler` is not defined, call the default handler. See `patternPropertiesHandler` for more information.

index.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@ var deepEqual = require('deep-equal');
33
module.exports = convert;
44

55
function convert(schema, options) {
6+
var notSupported = [
7+
'nullable', 'discriminator', 'readOnly',
8+
'writeOnly', 'xml', 'externalDocs',
9+
'example', 'deprecated'
10+
];
11+
612
options = options || {};
713
options.dateToDateTime = options.dateToDateTime || false;
814
options.cloneSchema = options.cloneSchema == false ? false : true;
915
options.supportPatternProperties = options.supportPatternProperties || false;
16+
options.keepNotSupported = options.keepNotSupported || [];
1017

1118
if (typeof options.patternPropertiesHandler !== 'function') {
1219
options.patternPropertiesHandler = patternPropertiesHandler;
1320
}
1421

1522
options._structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
16-
options._notSupported = [
17-
'nullable', 'discriminator', 'readOnly',
18-
'writeOnly', 'xml', 'externalDocs',
19-
'example', 'deprecated'
20-
];
23+
options._notSupported = resolveNotSupported(notSupported, options.keepNotSupported);
2124

2225
if (options.cloneSchema) {
2326
schema = JSON.parse(JSON.stringify(schema));
@@ -173,3 +176,19 @@ function patternPropertiesHandler(schema) {
173176

174177
return schema;
175178
}
179+
180+
function resolveNotSupported(notSupported, toRetain) {
181+
var i = 0
182+
, index
183+
;
184+
185+
for(i; i < toRetain.length; i++) {
186+
index = notSupported.indexOf(toRetain[i]);
187+
188+
if (index >= 0) {
189+
notSupported.splice(index, 1);
190+
}
191+
}
192+
193+
return notSupported;
194+
}

test/unsupported_properties.js

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var test = require('tape')
22
, convert = require('../')
33
;
44

5-
test('remove discriminator', function(assert) {
5+
test('remove discriminator by default', function(assert) {
66
var schema
77
, result
88
, expected
@@ -65,7 +65,7 @@ test('remove discriminator', function(assert) {
6565
assert.deepEqual(result, expected, 'discriminator removed');
6666
});
6767

68-
test('remove readOnly', function(assert) {
68+
test('remove readOnly by default', function(assert) {
6969
var schema
7070
, result
7171
, expected
@@ -74,21 +74,30 @@ test('remove readOnly', function(assert) {
7474
assert.plan(1);
7575

7676
schema = {
77-
type: 'string',
78-
readOnly: true
77+
type: 'object',
78+
properties: {
79+
readOnly: {
80+
type: 'string',
81+
readOnly: true
82+
}
83+
}
7984
};
8085

8186
result = convert(schema);
8287

8388
expected = {
8489
$schema: 'http://json-schema.org/draft-04/schema#',
85-
type: 'string'
90+
type: 'object',
91+
properties: {
92+
readOnly: {
93+
type: 'string'
94+
}
95+
}
8696
};
87-
8897
assert.deepEqual(result, expected, 'readOnly removed');
8998
});
9099

91-
test('remove writeOnly', function(assert) {
100+
test('remove writeOnly by default', function(assert) {
92101
var schema
93102
, result
94103
, expected
@@ -97,23 +106,33 @@ test('remove writeOnly', function(assert) {
97106
assert.plan(1);
98107

99108
schema = {
100-
type: 'string',
101-
format: 'password',
102-
writeOnly: true
109+
type: 'object',
110+
properties: {
111+
test: {
112+
type: 'string',
113+
format: 'password',
114+
writeOnly: true
115+
}
116+
}
103117
};
104118

105119
result = convert(schema);
106120

107121
expected = {
108122
$schema: 'http://json-schema.org/draft-04/schema#',
109-
type: 'string',
110-
format: 'password'
123+
type: 'object',
124+
properties: {
125+
test: {
126+
type: 'string',
127+
format: 'password'
128+
}
129+
}
111130
};
112131

113132
assert.deepEqual(result, expected, 'writeOnly removed');
114133
});
115134

116-
test('remove xml', function(assert) {
135+
test('remove xml by default', function(assert) {
117136
var schema
118137
, result
119138
, expected
@@ -148,7 +167,7 @@ test('remove xml', function(assert) {
148167
assert.deepEqual(result, expected, 'xml removed');
149168
});
150169

151-
test('remove externalDocs', function(assert) {
170+
test('remove externalDocs by default', function(assert) {
152171
var schema
153172
, result
154173
, expected
@@ -183,7 +202,7 @@ test('remove externalDocs', function(assert) {
183202
assert.deepEqual(result, expected, 'externalDocs removed');
184203
});
185204

186-
test('remove example', function(assert) {
205+
test('remove example by default', function(assert) {
187206
var schema
188207
, result
189208
, expected
@@ -206,7 +225,7 @@ test('remove example', function(assert) {
206225
assert.deepEqual(result, expected, 'example removed');
207226
});
208227

209-
test('remove deprecated', function(assert) {
228+
test('remove deprecated by default', function(assert) {
210229
var schema
211230
, result
212231
, expected
@@ -228,3 +247,58 @@ test('remove deprecated', function(assert) {
228247

229248
assert.deepEqual(result, expected, 'deprecated removed');
230249
});
250+
251+
test('retaining fields', function(assert) {
252+
var schema
253+
, result
254+
, expected
255+
;
256+
257+
assert.plan(1);
258+
259+
schema = {
260+
type: 'object',
261+
properties: {
262+
readOnly: {
263+
type: 'string',
264+
readOnly: true,
265+
example: 'foo'
266+
},
267+
anotherProp: {
268+
type: 'object',
269+
properties: {
270+
writeOnly: {
271+
type: 'string',
272+
writeOnly: true
273+
}
274+
}
275+
}
276+
},
277+
discriminator: 'bar'
278+
};
279+
280+
result = convert(schema, { keepNotSupported: ['readOnly', 'discriminator'] });
281+
282+
expected = {
283+
$schema: 'http://json-schema.org/draft-04/schema#',
284+
type: 'object',
285+
properties: {
286+
readOnly: {
287+
type: 'string',
288+
readOnly: true,
289+
},
290+
anotherProp: {
291+
type: 'object',
292+
properties: {
293+
writeOnly: {
294+
type: 'string',
295+
}
296+
}
297+
}
298+
},
299+
discriminator: 'bar'
300+
};
301+
302+
assert.deepEqual(result, expected, 'example and writeOnly removed');
303+
});
304+

0 commit comments

Comments
 (0)