Skip to content

Commit 1b95ebf

Browse files
authored
Merge pull request #25 from ehmicky/feature/parameter_object
Add support for OpenAPI 3.0 parameter object
2 parents f3b2566 + 8b06ad0 commit 1b95ebf

File tree

8 files changed

+358
-18
lines changed

8 files changed

+358
-18
lines changed

README.md

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenAPI Schema to JSON Schema
22

3-
A little NodeJS package to convert OpenAPI Schema Object to JSON Schema.
3+
A little NodeJS package to convert OpenAPI Schema Object or Parameter Object to JSON Schema.
44

55
Currently converts from [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) to [JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4).
66

@@ -19,6 +19,7 @@ If you need to do the conversion in reverse, checkout [json-schema-to-openapi-sc
1919
## Features
2020

2121
* converts OpenAPI 3.0 Schema Object to JSON Schema Draft 4
22+
* converts OpenAPI 3.0 Parameter Object to JSON Schema Draft 4
2223
* ~~converts [common named data types](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#data-types) to `type` and `format`~~ *(removed in version 2.0.0)*
2324
* ~~for example `type: "dateTime"` becomes `type: "string"` with `format: "date-time"`~~
2425
* deletes `nullable` and adds `"null"` to `type` array if `nullable` is `true`
@@ -76,7 +77,7 @@ If set to `false`, converts the provided schema in place. If `true`, clones the
7677
#### `dateToDateTime` (boolean)
7778

7879
This is `false` by default and leaves `date` format as is. If set to `true`, sets `format: 'date'` to `format: 'date-time'`.
79-
80+
8081
For example
8182

8283
```js
@@ -90,7 +91,7 @@ var convertedSchema = toJsonSchema(schema, {dateToDateTime: true});
9091
console.log(convertedSchema);
9192
```
9293

93-
prints
94+
prints
9495

9596
```js
9697
{
@@ -102,7 +103,7 @@ prints
102103

103104
#### `keepNotSupported` (array)
104105

105-
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.
106+
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.
106107

107108
#### `removeReadOnly` (boolean)
108109

@@ -113,7 +114,7 @@ If set to `true`, will remove properties set as `readOnly`. If the property is s
113114
Similar to `removeReadOnly`, but for `writeOnly` properties.
114115

115116
#### `supportPatternProperties` (boolean)
116-
117+
117118
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.
118119

119120
#### `patternPropertiesHandler` (function)
@@ -124,3 +125,63 @@ If the handler is not provided, the default handler is used. If `additionalPrope
124125

125126
See `test/pattern_properties.test.js` for examples how this works.
126127

128+
# Converting OpenAPI parameters
129+
130+
OpenAPI parameters can be converted.
131+
132+
```js
133+
{
134+
name: 'parameter name',
135+
in: 'query',
136+
schema: {
137+
type: 'string',
138+
format: 'date'
139+
}
140+
}
141+
```
142+
143+
would be converted to:
144+
145+
```js
146+
{
147+
type: 'string',
148+
format: 'date-time',
149+
'$schema': 'http://json-schema.org/draft-04/schema#'
150+
}
151+
```
152+
153+
When a parameter has several schemas (one per MIME type) a map is returned instead.
154+
155+
```js
156+
{
157+
name: 'parameter name',
158+
in: 'query',
159+
content: {
160+
'application/javascript': {
161+
schema: {
162+
type: 'string'
163+
}
164+
},
165+
'text/css': {
166+
schema: {
167+
type: 'string'
168+
}
169+
}
170+
}
171+
}
172+
```
173+
174+
would be converted to:
175+
176+
```js
177+
{
178+
'application/javascript': {
179+
type: 'string',
180+
'$schema': 'http://json-schema.org/draft-04/schema#'
181+
},
182+
'text/css': {
183+
type: 'string',
184+
'$schema': 'http://json-schema.org/draft-04/schema#'
185+
}
186+
}
187+
```

index.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,32 @@ var deepEqual = require('deep-equal');
22
var convert = require('./lib/convert');
33

44
module.exports = openapiSchemaToJsonSchema;
5+
module.exports.fromSchema = openapiSchemaToJsonSchema;
6+
module.exports.fromParameter = openapiParameterToJsonSchema;
57

68
function openapiSchemaToJsonSchema(schema, options) {
9+
options = resolveOptions(options);
10+
11+
if (options.cloneSchema) {
12+
schema = JSON.parse(JSON.stringify(schema));
13+
}
14+
15+
var jsonSchema = convert.fromSchema(schema, options);
16+
return jsonSchema;
17+
}
18+
19+
function openapiParameterToJsonSchema(parameter, options) {
20+
options = resolveOptions(options);
21+
22+
if (options.cloneSchema) {
23+
parameter = JSON.parse(JSON.stringify(parameter));
24+
}
25+
26+
var jsonSchema = convert.fromParameter(parameter, options);
27+
return jsonSchema;
28+
}
29+
30+
function resolveOptions(options) {
731
var notSupported = [
832
'nullable', 'discriminator', 'readOnly',
933
'writeOnly', 'xml', 'externalDocs',
@@ -33,14 +57,7 @@ function openapiSchemaToJsonSchema(schema, options) {
3357
options._structs = ['allOf', 'anyOf', 'oneOf', 'not', 'items', 'additionalProperties'];
3458
options._notSupported = resolveNotSupported(notSupported, options.keepNotSupported);
3559

36-
if (options.cloneSchema) {
37-
schema = JSON.parse(JSON.stringify(schema));
38-
}
39-
40-
schema = convert.schema(schema, options);
41-
schema['$schema'] = 'http://json-schema.org/draft-04/schema#';
42-
43-
return schema;
60+
return options;
4461
}
4562

4663
function patternPropertiesHandler(schema) {
@@ -78,4 +95,3 @@ function resolveNotSupported(notSupported, toRetain) {
7895

7996
return notSupported;
8097
}
81-

lib/convert.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var convertSchema = require('./converters/schema');
1+
var convertFromSchema = require('./converters/schema');
2+
var convertFromParameter = require('./converters/parameter');
23

34
module.exports = {
4-
schema: convertSchema,
5+
fromSchema: convertFromSchema,
6+
fromParameter: convertFromParameter,
57
};
6-

lib/converters/parameter.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var convertFromSchema = require('./schema');
2+
var InvalidInputError = require('../errors/invalid-input-error');
3+
4+
module.exports = convertFromParameter;
5+
6+
// Convert from OpenAPI 3.0 `ParameterObject` to JSON schema v4
7+
function convertFromParameter(parameter, options) {
8+
if (parameter.schema !== undefined) {
9+
return convertParameterSchema(parameter, parameter.schema, options);
10+
} else if (parameter.content !== undefined) {
11+
return convertFromContents(parameter, options);
12+
} else {
13+
throw new InvalidInputError('OpenAPI parameter must have either a \'schema\' or a \'content\' property');
14+
}
15+
}
16+
17+
function convertFromContents(parameter, options) {
18+
var schemas = {};
19+
20+
for (var mime in parameter.content) {
21+
schemas[mime] = convertParameterSchema(parameter, parameter.content[mime].schema, options);
22+
}
23+
24+
return schemas;
25+
}
26+
27+
function convertParameterSchema(parameter, schema, options) {
28+
var jsonSchema = convertFromSchema(schema || {}, options);
29+
30+
if (parameter.description) {
31+
jsonSchema.description = parameter.description;
32+
}
33+
34+
return jsonSchema;
35+
}

lib/converters/schema.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
var InvalidTypeError = require('../errors/invalid-type-error');
22

3-
module.exports = convertSchema;
3+
module.exports = convertFromSchema;
4+
5+
// Convert from OpenAPI 3.0 `SchemaObject` to JSON schema v4
6+
function convertFromSchema(schema, options) {
7+
schema = convertSchema(schema, options);
8+
9+
schema['$schema'] = 'http://json-schema.org/draft-04/schema#';
10+
11+
return schema;
12+
}
413

514
function convertSchema(schema, options) {
615
var structs = options._structs

lib/errors/invalid-input-error.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = InvalidInputError;
2+
3+
function InvalidInputError(message) {
4+
this.name = 'InvalidInputError';
5+
this.message = message;
6+
}
7+
8+
InvalidInputError.prototype = new Error();

test/converters.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var test = require('tape')
2+
, convert = require('../')
3+
;
4+
5+
test('using exports.fromSchema', function(assert) {
6+
var schema
7+
, result
8+
, expected
9+
;
10+
11+
assert.plan(1);
12+
13+
schema = {
14+
type: 'string',
15+
nullable: true,
16+
};
17+
18+
result = convert.fromSchema(schema);
19+
20+
expected = {
21+
$schema: 'http://json-schema.org/draft-04/schema#',
22+
type: ['string', 'null'],
23+
};
24+
25+
assert.deepEqual(result, expected, 'converted');
26+
});

0 commit comments

Comments
 (0)