Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #114 from apiaryio/kylef/object-allOf
Browse files Browse the repository at this point in the history
Support `allOf` when generating data structures for objects
  • Loading branch information
pksunkara authored Jul 21, 2017
2 parents add4b65 + 0d092e4 commit 179f7ec
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 0.12.1

## Enhancements

- Support `allOf` in object JSON Schemas when producing object data structure
elements.

# 0.12.0

- Updates to fury 3.0.0-beta.3 which supports Refract 1.0 serialisation
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fury-adapter-swagger",
"version": "0.12.0",
"version": "0.12.1",
"description": "Swagger 2.0 parser for Fury.js",
"main": "./lib/adapter.js",
"tonicExampleFilename": "tonic-example.js",
Expand Down
37 changes: 26 additions & 11 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,35 @@ export default class DataStructureGenerator {
String: StringElement,
} = this.minim.elements;

const element = new ObjectElement();
let properties = schema.properties || {};
let required = schema.required || [];

if (schema.allOf && Array.isArray(schema.allOf)) {
// Merge all of the object allOf into properties and required
const allOf = schema.allOf.filter(subschema => subschema.type === 'object');

const allProperties = allOf
.filter(subschema => subschema.properties)
.map(subschema => subschema.properties);
properties = Object.assign(properties, ...allProperties);

required = allOf
.filter(subschema => subschema.required)
.map(subschema => subschema.required)
.reduce((accumulator, property) => accumulator.concat(property), required);
}

if (schema.properties) {
element.content = _.map(schema.properties, (subschema, property) => {
const member = this.generateMember(property, subschema);
const element = new ObjectElement();
element.content = _.map(properties, (subschema, property) => {
const member = this.generateMember(property, subschema);

const required = schema.required && schema.required.includes(property);
member.attributes.typeAttributes = new ArrayElement([
new StringElement(required ? 'required' : 'optional'),
]);
const isRequired = required.includes(property);
member.attributes.typeAttributes = new ArrayElement([
new StringElement(isRequired ? 'required' : 'optional'),
]);

return member;
});
}
return member;
});

return element;
}
Expand Down
37 changes: 37 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,43 @@ describe('JSON Schema to Data Structure', () => {
expect(dataStructure.content.description.toValue())
.to.equal('- Object must have more than, or equal to 2 properties');
});

it('produces object element from multiple allOf objects', () => {
const schema = {
type: 'object',
allOf: [
{
type: 'object',
properties: {
name: {
type: 'string',
},
},
},
{
type: 'object',
properties: {
admin: {
type: 'boolean',
},
},
required: ['admin'],
},
],
};

const dataStructure = schemaToDataStructure(schema);

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

const name = dataStructure.content.get('name');
expect(name).not.to.be.undefined;

const admin = dataStructure.content.getMember('admin');
expect(admin).not.to.be.undefined;
expect(admin.attributes.typeAttributes.toValue()).to.deep.equal(['required']);
});
});

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

0 comments on commit 179f7ec

Please sign in to comment.