diff --git a/lib/components/JsonSchema/json-schema.js b/lib/components/JsonSchema/json-schema.js index 6dbe12a3..73dae7a0 100644 --- a/lib/components/JsonSchema/json-schema.js +++ b/lib/components/JsonSchema/json-schema.js @@ -17,6 +17,7 @@ export default class JsonSchema extends BaseComponent { prepareModel() { this.dereference(); + this.joinAllOf(); this.requiredMap = {}; if (this.schema.required) { this.schema.required.forEach(prop => this.requiredMap[prop] = true); @@ -26,7 +27,7 @@ export default class JsonSchema extends BaseComponent { this.data.properties = []; if (schema.type !== 'object') { // TODO - this.errorMessage = 'Non-object (array-based or all-of) schemas are not implemented yet'; + this.errorMessage = 'Non-object (array-based) schemas are not implemented yet'; return; } if (!schema.properties) return; diff --git a/lib/components/base.js b/lib/components/base.js index b112bc62..e2cae3cf 100644 --- a/lib/components/base.js +++ b/lib/components/base.js @@ -98,6 +98,36 @@ export class BaseComponent { }); } + joinAllOf(schema = this.componentSchema) { + var self = this; + function merge(into, schemas) { + if (into.required || into.properties) { + console.warn('WARN: properties or required field set on the same level as allOf'); + } + into.required = []; + into.properties = {}; + for (let subSchema of schemas) { + if (typeof subSchema !== 'object' || subSchema.type !== 'object') { + console.warn('WARN: incorrect allOf element skipped\nObject: ', subSchema); + } + + self.joinAllOf(subSchema); + + if (subSchema.properties) { + Object.assign(into.properties, subSchema.properties); + } + if (subSchema.required) { + into.required.push(...subSchema.required); + } + } + into.type = 'object'; + into.allOf = null; + } + if (schema.allOf) { + merge(schema, schema.allOf); + } + } + /** * Used to prepare model based on component schema * @abstract