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

Commit 6e6502d

Browse files
authored
fix(oas3): required schema object keys which are not in properties
1 parent 4018f11 commit 6e6502d

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

packages/openapi3-parser/CHANGELOG.md

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

3+
## Master
4+
5+
### Bug Fixes
6+
7+
- Support required keys in a Schema Object which are not found in the
8+
properties list.
9+
310
## 0.12.1 (2020-04-30)
411

512
### Bug Fixes

packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ function constructObjectStructure(namespace, schema) {
3939

4040
const required = schema.get('required');
4141
if (required) {
42-
required.forEach((key) => {
43-
const member = element.getMember(key.toValue());
44-
45-
if (member) {
46-
member.attributes.set('typeAttributes', ['required']);
47-
}
48-
});
42+
const attachMember = element.push.bind(element);
43+
const findMember = element.getMember.bind(element);
44+
45+
const createMember = R.constructN(1, namespace.elements.Member);
46+
const findOrCreateMember = R.either(
47+
findMember,
48+
R.pipe(createMember, R.tap(attachMember))
49+
);
50+
51+
required
52+
.toValue()
53+
.map(findOrCreateMember)
54+
.forEach(member => member.attributes.set('typeAttributes', ['required']));
4955
}
5056

5157
return element;

packages/openapi3-parser/test/unit/parser/oas/parseSchemaObject-test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,25 @@ describe('Schema Object', () => {
315315
]);
316316
});
317317

318-
it('returns an object with required properties', () => {
318+
it('returns an object with members from required keys', () => {
319+
const schema = new namespace.elements.Object({
320+
type: 'object',
321+
required: ['name'],
322+
});
323+
const parseResult = parse(context, schema);
324+
325+
expect(parseResult.length).to.equal(1);
326+
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.DataStructure);
327+
expect(parseResult).to.not.contain.annotations;
328+
329+
const object = parseResult.get(0).content;
330+
expect(object).to.be.instanceof(namespace.elements.Object);
331+
332+
const name = object.getMember('name');
333+
expect(name.attributes.getValue('typeAttributes')).to.deep.equal(['required']);
334+
});
335+
336+
it('returns an object with required properties by merging properties', () => {
319337
const schema = new namespace.elements.Object({
320338
type: 'object',
321339
properties: {

0 commit comments

Comments
 (0)