Nested allOf handling

This commit is contained in:
Roman Hotsiy 2016-03-15 16:09:42 +02:00
parent e957a92346
commit 3a6e2c14c4
4 changed files with 47 additions and 6 deletions

View File

@ -73,7 +73,6 @@ export default class JsonSchema extends BaseComponent {
let props = Object.keys(schema.properties).map((prop, idx) => {
let propData = schema.properties[prop];
let propPointer = JsonPointer.join(this.pointer, ['properties', prop]);
this.joinAllOf(propData);
propData = JsonSchema.injectPropData(propData, prop, propPointer, this.requiredMap, schema);
if (propData.isDiscriminator) discriminatorFieldIdx = idx;
return propData;

View File

@ -171,7 +171,6 @@ export class BaseComponent {
}
joinAllOf(schema = this.componentSchema, opts) {
var self = this;
function merge(into, schemas) {
for (let subSchema of schemas) {
if (opts && opts.omitParent && subSchema.discriminator) continue;
@ -182,13 +181,15 @@ export class BaseComponent {
throw new Error(errMessage);
}
self.joinAllOf(subSchema);
if (into.type && into.type !== subSchema.type) {
let errMessage = `allOf merging error: schemas with different types can't be merged`;
throw new Error(errMessage);
}
if (into.type === 'array') {
console.warn('allOf: subschemas with type array are not supported yet');
}
// TODO: add check if can be merged correctly (no different properties with the same name)
if (subSchema.type === 'object' && subSchema.properties) {
into.properties || (into.properties = {});
@ -203,9 +204,24 @@ export class BaseComponent {
}
into.allOf = null;
}
if (schema.allOf) {
merge(schema, schema.allOf);
function traverse(obj) {
if (obj === null || typeof(obj) !== 'object') {
return;
}
for(var key in obj) {
if (obj.hasOwnProperty(key)) {
traverse(obj[key]);
}
}
if (obj.allOf) {
merge(obj, obj.allOf);
}
}
traverse(schema);
}
/**

View File

@ -267,6 +267,17 @@ describe('Redoc components', () => {
component.dereference();
(() => component.joinAllOf()).should.throw();
});
it('should handle nested allOF', () => {
component.pointer = '/definitions/NestedAllOf';
component.ngOnInit();
component.dereference();
(() => component.joinAllOf()).should.not.throw();
let joined = component.componentSchema;
Object.keys(joined.properties).length.should.be.equal(4);
Object.keys(joined.properties).should.be.deepEqual(['prop1', 'prop2', 'prop3', 'prop4']);
joined.required.should.be.deepEqual(['prop1', 'prop3']);
});
});
xdescribe('Merge array allOf', () => {

View File

@ -97,6 +97,21 @@
}
}
]
},
"NestedAllOf": {
"allOf": [
{
"$ref": "#/definitions/SimpleAllOf"
},
{
"type": "object",
"properties": {
"prop4": {
"type": "string"
}
}
}
]
}
},
"paths": {