diff --git a/lib/components/JsonSchema/json-schema-lazy.ts b/lib/components/JsonSchema/json-schema-lazy.ts index 177d0fdc..24e2f1cb 100644 --- a/lib/components/JsonSchema/json-schema-lazy.ts +++ b/lib/components/JsonSchema/json-schema-lazy.ts @@ -64,7 +64,8 @@ export class JsonSchemaLazy implements OnDestroy, OnInit, AfterViewInit { // skip caching view with descendant schemas // as it needs attached controller - if (!this.disableLazy && (compRef.instance.hasDescendants || compRef.instance._hasSubSchemas)) { + let hasDescendants = compRef.instance.descendants && compRef.instance.descendants.length; + if (!this.disableLazy && (hasDescendants || compRef.instance._hasSubSchemas)) { this._loadAfterSelf(); return; } diff --git a/lib/components/JsonSchema/json-schema.ts b/lib/components/JsonSchema/json-schema.ts index 44b5f27b..59a548b9 100644 --- a/lib/components/JsonSchema/json-schema.ts +++ b/lib/components/JsonSchema/json-schema.ts @@ -31,7 +31,7 @@ export class JsonSchema extends BaseSearchableComponent implements OnInit { schema: any = {}; activeDescendant:any = {}; - hasDescendants: boolean = false; + discriminator: string = null; _hasSubSchemas: boolean = false; properties: any; _isArray: boolean; @@ -75,8 +75,7 @@ export class JsonSchema extends BaseSearchableComponent implements OnInit { initDescendants() { this.descendants = this.specMgr.findDerivedDefinitions(this.normPointer, this.schema); if (!this.descendants.length) return; - this.hasDescendants = true; - let discriminator = this.schema.discriminator || this.schema['x-extendedDiscriminator']; + let discriminator = this.discriminator = this.schema.discriminator || this.schema['x-extendedDiscriminator']; let discrProperty = this.schema.properties && this.schema.properties[discriminator]; if (discrProperty && discrProperty.enum) { @@ -91,6 +90,7 @@ export class JsonSchema extends BaseSearchableComponent implements OnInit { }).sort((a, b) => { return enumOrder[a.name] > enumOrder[b.name] ? 1 : -1; }); + this.descendants.forEach((d, idx) => d.idx = idx); } this.selectDescendantByIdx(0); } @@ -119,11 +119,12 @@ export class JsonSchema extends BaseSearchableComponent implements OnInit { if (!this.schema.isTrivial) { SchemaHelper.preprocessProperties(this.schema, this.normPointer, { - childFor: this.childFor + childFor: this.childFor, + discriminator: this.discriminator }); } - this.properties = this.schema._properties; + this.properties = this.schema._properties || []; if (this.isRequestSchema) { this.properties = this.properties && this.properties.filter(prop => !prop.readOnly); } diff --git a/lib/components/base.ts b/lib/components/base.ts index de8417ac..bf195723 100644 --- a/lib/components/base.ts +++ b/lib/components/base.ts @@ -6,22 +6,6 @@ import { Subscription } from 'rxjs/Subscription'; export { SpecManager }; -function snapshot(obj) { - if(obj == undefined || typeof(obj) !== 'object') { - return obj; - } - - var temp = new obj.constructor(); - - for(var key in obj) { - if (obj.hasOwnProperty(key)) { - temp[key] = snapshot(obj[key]); - } - } - - return temp; -} - /** * Generic Component * @class diff --git a/lib/services/schema-helper.service.ts b/lib/services/schema-helper.service.ts index c623fe5b..75a56424 100644 --- a/lib/services/schema-helper.service.ts +++ b/lib/services/schema-helper.service.ts @@ -7,6 +7,7 @@ import * as slugify from 'slugify'; interface PropertyPreprocessOptions { childFor: string; skipReadOnly?: boolean; + discriminator?: string; } // global var for this module @@ -220,8 +221,7 @@ export class SchemaHelper { propertySchema._pointer = null; } propertySchema._required = !!requiredMap[propName]; - propertySchema.isDiscriminator = (schema.discriminator === propName - || schema['x-extendedDiscriminator'] === propName); + propertySchema.isDiscriminator = opts.discriminator === propName; return propertySchema; }); diff --git a/lib/services/schema-normalizer.service.ts b/lib/services/schema-normalizer.service.ts index 7c62c457..a4a9ea11 100644 --- a/lib/services/schema-normalizer.service.ts +++ b/lib/services/schema-normalizer.service.ts @@ -27,6 +27,7 @@ export class SchemaNormalizer { let hasPtr = !!schema.$ref; if (opts.resolved && !hasPtr) this._dereferencer.visit(ptr); + if (opts.childFor) this._dereferencer.visit(opts.childFor); if (schema['x-redoc-normalized']) return schema; let res = SchemaWalker.walk(schema, ptr, (subSchema, ptr) => { let resolved = this._dereferencer.dereference(subSchema, ptr); @@ -38,6 +39,7 @@ export class SchemaNormalizer { return resolved; }); if (opts.resolved && !hasPtr) this._dereferencer.exit(ptr); + if (opts.childFor) this._dereferencer.exit(opts.childFor); res['x-redoc-normalized'] = true; return res; } @@ -113,6 +115,7 @@ export class AllOfMerger { defaults(into, subSchema); subSchema._pointer = tmpPtr; } + into.discriminator = null; into.allOf = null; } diff --git a/lib/services/search.service.ts b/lib/services/search.service.ts index 4888292f..0e9c3b25 100644 --- a/lib/services/search.service.ts +++ b/lib/services/search.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@angular/core'; import { AppStateService } from './app-state.service'; import { SchemaNormalizer } from './schema-normalizer.service'; -import { JsonPointer, groupBy, SpecManager, StringMap } from '../utils/'; +import { JsonPointer, groupBy, SpecManager, StringMap, snapshot } from '../utils/'; import * as lunr from 'lunr'; @@ -13,7 +13,7 @@ interface IndexElement { } const index = lunr(function () { - this.field('menuId', {boost: 0}); + //this.field('menuId', {boost: 0}); this.field('title', {boost: 1.5}); this.field('body'); this.ref('pointer'); @@ -33,13 +33,13 @@ export class SearchService { } indexAll() { - const swagger = this.spec.schema; - - this.indexPaths(swagger); + this.indexPaths(this.spec.schema); } search(q):StringMap { + var items = {}; const res:IndexElement[] = index.search(q).map(res => { + items[res.menuId] = res; return store[res.ref]; }); const grouped = groupBy(res, 'menuId'); @@ -79,6 +79,7 @@ export class SearchService { indexOperationParameters(operation: any, operationPointer: string) { const parameters = operation.parameters; + if (!parameters) return; for (let i=0; i 0 || (function (p) { return p.toString() === '[object SafariRemoteNotification]'; })(!window['safari'] || safari.pushNotification); + +export function snapshot(obj) { + if(obj == undefined || typeof(obj) !== 'object') { + return obj; + } + + var temp = new obj.constructor(); + + for(var key in obj) { + if (obj.hasOwnProperty(key)) { + temp[key] = snapshot(obj[key]); + } + } + + return temp; +} diff --git a/lib/utils/spec-manager.ts b/lib/utils/spec-manager.ts index 9fea813c..27fe56c7 100644 --- a/lib/utils/spec-manager.ts +++ b/lib/utils/spec-manager.ts @@ -176,7 +176,7 @@ export class SpecManager { if (!definition.discriminator && !definition['x-extendedDiscriminator']) return []; let globalDefs = this._schema.definitions || {}; - let res = []; + let res:DescendantInfo[] = []; let extendedDiscriminatorProp = definition['x-extendedDiscriminator']; for (let defName of Object.keys(globalDefs)) { let def = globalDefs[defName]; @@ -208,7 +208,7 @@ export class SpecManager { } } - res.push({name: derivedName, $ref: `#/definitions/${defName}`, idx: res.length}); + res.push({name: derivedName, $ref: `#/definitions/${defName}`, idx: null}); } return res; }