From afe9020021d2be8268d447c44a4b1f6b2ff4d52f Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sat, 25 Jun 2016 13:02:13 +0300 Subject: [PATCH] Fix dereferencing --- lib/components/JsonSchema/json-schema.html | 2 +- lib/components/JsonSchema/json-schema.ts | 16 ++++++++-------- lib/components/ParamsList/params-list.ts | 3 ++- lib/services/schema-helper.service.ts | 14 ++++++++------ lib/services/schema-normalizer.service.ts | 8 +++++++- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/components/JsonSchema/json-schema.html b/lib/components/JsonSchema/json-schema.html index 85d3df36..f4e9d096 100644 --- a/lib/components/JsonSchema/json-schema.html +++ b/lib/components/JsonSchema/json-schema.html @@ -34,7 +34,7 @@ title="{{prop._displayTypeHint}}"> {{prop._displayType}} {{prop._displayFormat}} {{prop._range}} - Required + Required
Default: {{prop.default | json}}
{{enumItem.val | json}} diff --git a/lib/components/JsonSchema/json-schema.ts b/lib/components/JsonSchema/json-schema.ts index 2150bc44..3773e0aa 100644 --- a/lib/components/JsonSchema/json-schema.ts +++ b/lib/components/JsonSchema/json-schema.ts @@ -55,7 +55,7 @@ export class JsonSchema extends BaseComponent { enumOrder[enumItem.val] = idx; }); - this.schema._derived.sort((a, b) => { + this.schema._descendants.sort((a, b) => { return enumOrder[a.name] > enumOrder[b.name] ? 1 : -1; }); } @@ -63,17 +63,17 @@ export class JsonSchema extends BaseComponent { } prepareModel() { - let schema = this.schema = this.componentSchema; - if (!schema) { + this.schema = this.componentSchema; + if (!this.schema) { throw new Error(`Can't load component schema at ${this.pointer}`); } - schema = this.normalizer.normalize(schema, this.normPointer); - this.schema = schema = SchemaHelper.unwrapArray(schema, this.normPointer); - SchemaHelper.preprocess(schema, schema, this.normPointer, this.pointer); + this.schema = this.normalizer.normalize(this.schema, this.normPointer); + this.schema = SchemaHelper.unwrapArray(this.schema, this.normPointer); + SchemaHelper.preprocess(this.schema, this.normPointer, this.pointer); - if (!schema.isTrivial) { - SchemaHelper.preprocessProperties(schema, this.normPointer, { + if (!this.schema.isTrivial) { + SchemaHelper.preprocessProperties(this.schema, this.normPointer, { childFor: this.childFor, skipReadOnly: this.isRequestSchema }); diff --git a/lib/components/ParamsList/params-list.ts b/lib/components/ParamsList/params-list.ts index 45b8602f..00104a58 100644 --- a/lib/components/ParamsList/params-list.ts +++ b/lib/components/ParamsList/params-list.ts @@ -31,7 +31,8 @@ export class ParamsList extends BaseComponent { paramsList = paramsList.map(paramSchema => { let propPointer = paramSchema._pointer; if (paramSchema.in === 'body') return paramSchema; - return SchemaHelper.preprocess(paramSchema, paramSchema.name, propPointer, this.pointer); + paramSchema._name = paramSchema.name; + return SchemaHelper.preprocess(paramSchema,propPointer, this.pointer); }); let paramsMap = this.orderParams(paramsList); diff --git a/lib/services/schema-helper.service.ts b/lib/services/schema-helper.service.ts index 4fda5d44..234f084e 100644 --- a/lib/services/schema-helper.service.ts +++ b/lib/services/schema-helper.service.ts @@ -135,12 +135,11 @@ const injectors = { }; export class SchemaHelper { - static preprocess(schema, name, pointer, hostPointer?) { + static preprocess(schema, pointer, hostPointer?) { //propertySchema = Object.assign({}, propertySchema); if (schema['x-redoc-schema-precompiled']) { return schema; } - schema._name = name; SchemaHelper.runInjectors(schema, schema, pointer, hostPointer); schema['x-redoc-schema-precompiled'] = true; return schema; @@ -163,15 +162,16 @@ export class SchemaHelper { let discriminatorFieldIdx = -1; let props = schema.properties && Object.keys(schema.properties).map((propName, idx) => { - let propertySchema = schema.properties[propName]; + let propertySchema = Object.assign({}, schema.properties[propName]); let propPointer = propertySchema._pointer || JsonPointer.join(pointer, ['properties', propName]); - propertySchema = SchemaHelper.preprocess(propertySchema, propName, propPointer); + propertySchema = SchemaHelper.preprocess(propertySchema, propPointer); + propertySchema._name = propName; // stop endless discriminator recursion if (propertySchema._pointer === opts.childFor) { propertySchema._pointer = null; } - propertySchema.required = !!requiredMap[propName]; + propertySchema._required = !!requiredMap[propName]; propertySchema.isDiscriminator = (schema.discriminator === propName); if (propertySchema.isDiscriminator) { discriminatorFieldIdx = idx; @@ -202,7 +202,9 @@ export class SchemaHelper { static preprocessAdditionalProperties(schema:any, pointer:string) { var addProps = schema.additionalProperties; let ptr = addProps._pointer || JsonPointer.join(pointer, ['additionalProperties']); - return SchemaHelper.preprocess(addProps, ' *', ptr); + let res = SchemaHelper.preprocess(addProps, ptr); + res._name = ' *'; + return res; } static unwrapArray(schema, pointer) { diff --git a/lib/services/schema-normalizer.service.ts b/lib/services/schema-normalizer.service.ts index 9f74dd5b..a68aa343 100644 --- a/lib/services/schema-normalizer.service.ts +++ b/lib/services/schema-normalizer.service.ts @@ -46,9 +46,15 @@ class SchemaWalker { let ptr = JsonPointer.join(pointer, ['properties']); SchemaWalker.walkEach(obj.properties, ptr, visitor); } + if (obj.additionalProperties) { let ptr = JsonPointer.join(pointer, ['additionalProperties']); - SchemaWalker.walkEach(obj.additionalProperties, ptr, visitor); + if (Array.isArray(obj.additionalProperties)) { + SchemaWalker.walkEach(obj.additionalProperties, ptr, visitor); + } else { + let res = SchemaWalker.walk(obj.additionalProperties, ptr, visitor); + if (res) obj.additionalProperties = res; + } } if (obj.allOf) {