mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-18 10:50:32 +03:00
Nested allOf handling
This commit is contained in:
parent
e957a92346
commit
3a6e2c14c4
|
@ -73,7 +73,6 @@ export default class JsonSchema extends BaseComponent {
|
||||||
let props = Object.keys(schema.properties).map((prop, idx) => {
|
let props = Object.keys(schema.properties).map((prop, idx) => {
|
||||||
let propData = schema.properties[prop];
|
let propData = schema.properties[prop];
|
||||||
let propPointer = JsonPointer.join(this.pointer, ['properties', prop]);
|
let propPointer = JsonPointer.join(this.pointer, ['properties', prop]);
|
||||||
this.joinAllOf(propData);
|
|
||||||
propData = JsonSchema.injectPropData(propData, prop, propPointer, this.requiredMap, schema);
|
propData = JsonSchema.injectPropData(propData, prop, propPointer, this.requiredMap, schema);
|
||||||
if (propData.isDiscriminator) discriminatorFieldIdx = idx;
|
if (propData.isDiscriminator) discriminatorFieldIdx = idx;
|
||||||
return propData;
|
return propData;
|
||||||
|
|
|
@ -171,7 +171,6 @@ export class BaseComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
joinAllOf(schema = this.componentSchema, opts) {
|
joinAllOf(schema = this.componentSchema, opts) {
|
||||||
var self = this;
|
|
||||||
function merge(into, schemas) {
|
function merge(into, schemas) {
|
||||||
for (let subSchema of schemas) {
|
for (let subSchema of schemas) {
|
||||||
if (opts && opts.omitParent && subSchema.discriminator) continue;
|
if (opts && opts.omitParent && subSchema.discriminator) continue;
|
||||||
|
@ -182,13 +181,15 @@ export class BaseComponent {
|
||||||
throw new Error(errMessage);
|
throw new Error(errMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.joinAllOf(subSchema);
|
|
||||||
|
|
||||||
if (into.type && into.type !== subSchema.type) {
|
if (into.type && into.type !== subSchema.type) {
|
||||||
let errMessage = `allOf merging error: schemas with different types can't be merged`;
|
let errMessage = `allOf merging error: schemas with different types can't be merged`;
|
||||||
throw new Error(errMessage);
|
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)
|
// TODO: add check if can be merged correctly (no different properties with the same name)
|
||||||
if (subSchema.type === 'object' && subSchema.properties) {
|
if (subSchema.type === 'object' && subSchema.properties) {
|
||||||
into.properties || (into.properties = {});
|
into.properties || (into.properties = {});
|
||||||
|
@ -203,9 +204,24 @@ export class BaseComponent {
|
||||||
}
|
}
|
||||||
into.allOf = null;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -267,6 +267,17 @@ describe('Redoc components', () => {
|
||||||
component.dereference();
|
component.dereference();
|
||||||
(() => component.joinAllOf()).should.throw();
|
(() => 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', () => {
|
xdescribe('Merge array allOf', () => {
|
||||||
|
|
|
@ -97,6 +97,21 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"NestedAllOf": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/SimpleAllOf"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"prop4": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"paths": {
|
"paths": {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user