Skip to content

Commit

Permalink
feat(oas2): support boolean additionalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Feb 23, 2021
1 parent ef553d9 commit fd2584f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/openapi2-parser/lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class DataStructureGenerator {
});
element.content.push(...requiredMembers);

if (schema.additionalProperties === false) {
const typeAttributes = element.attributes.get('typeAttributes') || new this.minim.elements.Array([]);
typeAttributes.push('fixed-type');
element.attributes.set('typeAttributes', typeAttributes);
}

return element;
}

Expand Down
30 changes: 30 additions & 0 deletions packages/openapi2-parser/test/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,36 @@ describe('JSON Schema to Data Structure', () => {
expect(samples).to.be.instanceof(ArrayElement);
expect(samples.toValue()).to.be.deep.equal([{ name: 'Doe' }]);
});

it('produces object with additionalProperties as true', () => {
const schema = {
type: 'object',
additionalProperties: true,
};

const dataStructure = schemaToDataStructure(schema);

expect(dataStructure.element).to.equal('dataStructure');
expect(dataStructure.content).to.be.instanceof(ObjectElement);

expect(dataStructure.content.length).to.equal(0);
expect(dataStructure.content.attributes.getValue('typeAttributes')).to.be.undefined;
});

it('produces object with additionalProperties as false', () => {
const schema = {
type: 'object',
additionalProperties: false,
};

const dataStructure = schemaToDataStructure(schema);

expect(dataStructure.element).to.equal('dataStructure');
expect(dataStructure.content).to.be.instanceof(ObjectElement);

expect(dataStructure.content.length).to.equal(0);
expect(dataStructure.content.attributes.getValue('typeAttributes')).to.deep.equal(['fixed-type']);
});
});

context('array schema', () => {
Expand Down

0 comments on commit fd2584f

Please sign in to comment.