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

Commit 4d7f429

Browse files
committed
fix(oas2): required schema object keys which are not in properties
1 parent f264f1f commit 4d7f429

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

packages/openapi2-parser/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
values of the array to be a string type. Instead provided an example that the
2222
value of the array MAY be a string.
2323
24+
- Support required keys in a Schema Object which are not found in the
25+
properties list.
26+
2427
## 0.30.0 (2020-04-29)
2528
2629
The package has been renamed to `@apielements/openapi2-parser`.

packages/openapi2-parser/lib/schema.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ class DataStructureGenerator {
111111
return member;
112112
}));
113113

114+
// Create member elements for required keys which are not defined in properties
115+
const missingRequiredProperties = required.filter(name => properties[name] === undefined);
116+
const requiredMembers = missingRequiredProperties.map((name) => {
117+
const member = new this.minim.elements.Member(name);
118+
member.attributes.set('typeAttributes', ['required']);
119+
return member;
120+
});
121+
element.content = element.content.concat(requiredMembers);
122+
114123
return element;
115124
}
116125

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,21 @@ describe('JSON Schema to Data Structure', () => {
460460
expect(member.attributes.getValue('typeAttributes')).to.deep.equal(['required']);
461461
});
462462

463+
it('produces object members from required which are not found in properties', () => {
464+
const schema = {
465+
type: 'object',
466+
required: ['name'],
467+
};
468+
469+
const dataStructure = schemaToDataStructure(schema);
470+
471+
expect(dataStructure.element).to.equal('dataStructure');
472+
expect(dataStructure.content).to.be.instanceof(ObjectElement);
473+
474+
const name = dataStructure.content.getMember('name');
475+
expect(name.attributes.getValue('typeAttributes')).to.deep.equal(['required']);
476+
});
477+
463478
it('produces object element with description of maxProperties', () => {
464479
const schema = {
465480
type: 'object',

0 commit comments

Comments
 (0)