Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 9c9ffc9

Browse files
committed
fix(oas2): handle schema object without type
1 parent 56667cf commit 9c9ffc9

File tree

6 files changed

+177
-63
lines changed

6 files changed

+177
-63
lines changed

packages/openapi2-parser/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# API Elements: OpenAPI 2 Parser Changelog
22

3+
## Master
4+
5+
### Bug Fixes
6+
7+
- The parser will now produce a data structure for Schema Object's which do not
8+
contain a `type`.
9+
310
## 0.30.0 (2020-04-29)
411

512
The package has been renamed to `@apielements/openapi2-parser`.

packages/openapi2-parser/lib/schema.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,6 @@ class DataStructureGenerator {
203203
return uniqueTypes[0];
204204
}
205205
}
206-
207-
if (schema.properties) {
208-
// Assume user meant object
209-
return 'object';
210-
}
211206
}
212207

213208
return schema.type;
@@ -271,6 +266,15 @@ class DataStructureGenerator {
271266
element = new typeGeneratorMap[type]();
272267
} else if (type) {
273268
throw new Error(`Unhandled schema type '${type}'`);
269+
} else {
270+
element = new this.minim.elements.Enum();
271+
element.enumerations = [
272+
new this.minim.elements.String(),
273+
new this.minim.elements.Number(),
274+
new this.minim.elements.Boolean(),
275+
this.generateArray(schema),
276+
this.generateObject(schema),
277+
];
274278
}
275279

276280
if (element) {

packages/openapi2-parser/test/fixtures/circular-example.json

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -405,28 +405,25 @@
405405
"content": "company"
406406
},
407407
"value": {
408-
"element": "object",
409-
"content": [
410-
{
411-
"element": "member",
412-
"attributes": {
413-
"typeAttributes": {
414-
"element": "array",
415-
"content": [
416-
{
417-
"element": "string",
418-
"content": "optional"
419-
}
420-
]
421-
}
422-
},
423-
"content": {
424-
"key": {
425-
"element": "string",
426-
"content": "data"
408+
"element": "enum",
409+
"attributes": {
410+
"enumerations": {
411+
"element": "array",
412+
"content": [
413+
{
414+
"element": "string"
427415
},
428-
"value": {
429-
"element": "definitions/Company",
416+
{
417+
"element": "number"
418+
},
419+
{
420+
"element": "boolean"
421+
},
422+
{
423+
"element": "array"
424+
},
425+
{
426+
"element": "object",
430427
"content": [
431428
{
432429
"element": "member",
@@ -444,24 +441,49 @@
444441
"content": {
445442
"key": {
446443
"element": "string",
447-
"content": "is_owner"
444+
"content": "data"
448445
},
449446
"value": {
450-
"element": "boolean",
451-
"attributes": {
452-
"default": {
453-
"element": "boolean",
454-
"content": false
447+
"element": "definitions/Company",
448+
"content": [
449+
{
450+
"element": "member",
451+
"attributes": {
452+
"typeAttributes": {
453+
"element": "array",
454+
"content": [
455+
{
456+
"element": "string",
457+
"content": "optional"
458+
}
459+
]
460+
}
461+
},
462+
"content": {
463+
"key": {
464+
"element": "string",
465+
"content": "is_owner"
466+
},
467+
"value": {
468+
"element": "boolean",
469+
"attributes": {
470+
"default": {
471+
"element": "boolean",
472+
"content": false
473+
}
474+
}
475+
}
476+
}
455477
}
456-
}
478+
]
457479
}
458480
}
459481
}
460482
]
461483
}
462-
}
484+
]
463485
}
464-
]
486+
}
465487
}
466488
}
467489
}

packages/openapi2-parser/test/schema-test.js

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,56 @@ function schemaToDataStructure(schema, root) {
2222
}
2323

2424
describe('JSON Schema to Data Structure', () => {
25+
context('schema without type', () => {
26+
it('produces enum of any value', () => {
27+
const schema = {};
28+
29+
const dataStructure = schemaToDataStructure(schema);
30+
31+
expect(dataStructure.element).to.equal('dataStructure');
32+
expect(dataStructure.content).to.be.instanceof(EnumElement);
33+
34+
expect(dataStructure.content.enumerations.length).to.equal(5);
35+
expect(dataStructure.content.enumerations.get(0)).to.be.instanceof(StringElement);
36+
expect(dataStructure.content.enumerations.get(1)).to.be.instanceof(NumberElement);
37+
expect(dataStructure.content.enumerations.get(2)).to.be.instanceof(BooleanElement);
38+
expect(dataStructure.content.enumerations.get(3)).to.be.instanceof(ArrayElement);
39+
expect(dataStructure.content.enumerations.get(4)).to.be.instanceof(ObjectElement);
40+
});
41+
42+
it('produces array with items', () => {
43+
const schema = {
44+
items: {
45+
type: 'string',
46+
},
47+
};
48+
49+
const dataStructure = schemaToDataStructure(schema);
50+
51+
expect(dataStructure.element).to.equal('dataStructure');
52+
expect(dataStructure.content).to.be.instanceof(EnumElement);
53+
54+
const array = dataStructure.content.enumerations.content.find(element => element instanceof ArrayElement);
55+
expect(array.get(0)).to.be.instanceof(StringElement);
56+
});
57+
58+
it('produces array with properties', () => {
59+
const schema = {
60+
properties: {
61+
name: { type: 'string' },
62+
},
63+
};
64+
65+
const dataStructure = schemaToDataStructure(schema);
66+
67+
expect(dataStructure.element).to.equal('dataStructure');
68+
expect(dataStructure.content).to.be.instanceof(EnumElement);
69+
70+
const array = dataStructure.content.enumerations.content.find(element => element instanceof ObjectElement);
71+
expect(array.get('name')).to.be.instanceof(StringElement);
72+
});
73+
});
74+
2575
it('null type schema', () => {
2676
const schema = {
2777
type: 'null',
@@ -511,24 +561,6 @@ describe('JSON Schema to Data Structure', () => {
511561
expect(admin.attributes.getValue('typeAttributes')).to.deep.equal(['required']);
512562
});
513563

514-
it('produces object element from properties when schema root doesnt provide a type', () => {
515-
const schema = {
516-
properties: {
517-
name: {
518-
type: 'string',
519-
},
520-
},
521-
};
522-
523-
const dataStructure = schemaToDataStructure(schema);
524-
525-
expect(dataStructure.element).to.equal('dataStructure');
526-
expect(dataStructure.content).to.be.instanceof(ObjectElement);
527-
528-
const name = dataStructure.content.get('name');
529-
expect(name).not.to.be.undefined;
530-
});
531-
532564
it('produces object element from multiple allOf where one allOf is reference base type', () => {
533565
const schema = {
534566
allOf: [
@@ -754,14 +786,15 @@ describe('JSON Schema to Data Structure', () => {
754786
it('produces empty array element with empty items', () => {
755787
const schema = {
756788
type: 'array',
757-
items: {},
789+
items: { type: 'string' },
758790
};
759791

760792
const dataStructure = schemaToDataStructure(schema);
761793

762794
expect(dataStructure.element).to.equal('dataStructure');
763795
expect(dataStructure.content).to.be.instanceof(ArrayElement);
764-
expect(dataStructure.content.content.length).to.be.equal(0);
796+
expect(dataStructure.content.content.length).to.be.equal(1);
797+
expect(dataStructure.content.content[0]).to.be.instanceof(StringElement);
765798
});
766799

767800
it('produces value from example', () => {
@@ -924,14 +957,6 @@ describe('JSON Schema to Data Structure', () => {
924957
.to.equal("- Value must be of format 'email'");
925958
});
926959

927-
it('returns null when there is no type in the given JSON schema', () => {
928-
const schema = {};
929-
930-
const dataStructure = schemaToDataStructure(schema);
931-
932-
expect(dataStructure).to.be.null;
933-
});
934-
935960
it('adds nullable typeAttribute with x-nullable extension', () => {
936961
const schema = {
937962
type: 'string',

packages/swagger-zoo/fixtures/features/api-elements/warnings.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@
116116
}
117117
},
118118
"content": "{\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
119+
},
120+
{
121+
"element": "dataStructure",
122+
"content": {
123+
"element": "enum",
124+
"attributes": {
125+
"enumerations": {
126+
"element": "array",
127+
"content": [
128+
{
129+
"element": "string"
130+
},
131+
{
132+
"element": "number"
133+
},
134+
{
135+
"element": "boolean"
136+
},
137+
{
138+
"element": "array"
139+
},
140+
{
141+
"element": "object"
142+
}
143+
]
144+
}
145+
}
146+
}
119147
}
120148
]
121149
},

packages/swagger-zoo/fixtures/features/api-elements/warnings.sourcemap.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,34 @@
289289
}
290290
},
291291
"content": "{\"$schema\":\"http://json-schema.org/draft-04/schema#\"}"
292+
},
293+
{
294+
"element": "dataStructure",
295+
"content": {
296+
"element": "enum",
297+
"attributes": {
298+
"enumerations": {
299+
"element": "array",
300+
"content": [
301+
{
302+
"element": "string"
303+
},
304+
{
305+
"element": "number"
306+
},
307+
{
308+
"element": "boolean"
309+
},
310+
{
311+
"element": "array"
312+
},
313+
{
314+
"element": "object"
315+
}
316+
]
317+
}
318+
}
319+
}
292320
}
293321
]
294322
},

0 commit comments

Comments
 (0)