Skip to content

Commit 00ae0ab

Browse files
committed
add an option to retain unsupported fields
1 parent 4634169 commit 00ae0ab

File tree

3 files changed

+91
-13
lines changed

3 files changed

+91
-13
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: 62 additions & 7 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
@@ -97,7 +97,7 @@ test('remove readOnly', function(assert) {
9797
assert.deepEqual(result, expected, 'readOnly removed');
9898
});
9999

100-
test('remove writeOnly', function(assert) {
100+
test('remove writeOnly by default', function(assert) {
101101
var schema
102102
, result
103103
, expected
@@ -132,7 +132,7 @@ test('remove writeOnly', function(assert) {
132132
assert.deepEqual(result, expected, 'writeOnly removed');
133133
});
134134

135-
test('remove xml', function(assert) {
135+
test('remove xml by default', function(assert) {
136136
var schema
137137
, result
138138
, expected
@@ -167,7 +167,7 @@ test('remove xml', function(assert) {
167167
assert.deepEqual(result, expected, 'xml removed');
168168
});
169169

170-
test('remove externalDocs', function(assert) {
170+
test('remove externalDocs by default', function(assert) {
171171
var schema
172172
, result
173173
, expected
@@ -202,7 +202,7 @@ test('remove externalDocs', function(assert) {
202202
assert.deepEqual(result, expected, 'externalDocs removed');
203203
});
204204

205-
test('remove example', function(assert) {
205+
test('remove example by default', function(assert) {
206206
var schema
207207
, result
208208
, expected
@@ -225,7 +225,7 @@ test('remove example', function(assert) {
225225
assert.deepEqual(result, expected, 'example removed');
226226
});
227227

228-
test('remove deprecated', function(assert) {
228+
test('remove deprecated by default', function(assert) {
229229
var schema
230230
, result
231231
, expected
@@ -247,3 +247,58 @@ test('remove deprecated', function(assert) {
247247

248248
assert.deepEqual(result, expected, 'deprecated removed');
249249
});
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)