diff --git a/lib/components/JsonSchema/json-schema.html b/lib/components/JsonSchema/json-schema.html index 1a2fbcb6..da41e5f4 100644 --- a/lib/components/JsonSchema/json-schema.html +++ b/lib/components/JsonSchema/json-schema.html @@ -58,7 +58,7 @@ - {{prop._name}} + {{prop.name}} {{prop._enumItem?.val | json}} @@ -93,9 +93,9 @@
- + + [nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema" absolutePointer="{{absolutePointer}}/properties/{{prop.name}}"> diff --git a/lib/components/JsonSchema/json-schema.ts b/lib/components/JsonSchema/json-schema.ts index 59a548b9..e4e15ad3 100644 --- a/lib/components/JsonSchema/json-schema.ts +++ b/lib/components/JsonSchema/json-schema.ts @@ -180,11 +180,11 @@ export class JsonSchema extends BaseSearchableComponent implements OnInit { if (relative.length > 1 && relative[0] === 'properties') { propName = relative[1]; } - let prop = props.find(p => p._name === propName); + let prop = props.find(p => p.name === propName); if (!prop) { let d = this.findDescendantWithField(propName); this.selectDescendant(d); - prop = this.properties.find(p => p._name === propName); + prop = this.properties.find(p => p.name === propName); } if (prop && !prop.isTrivial) prop.expanded = true; this.cdr.markForCheck(); diff --git a/lib/components/Method/method.ts b/lib/components/Method/method.ts index 0590c1d1..7067b4c8 100644 --- a/lib/components/Method/method.ts +++ b/lib/components/Method/method.ts @@ -77,7 +77,7 @@ export class Method extends BaseComponent implements OnInit { } findBodyParam() { - let pathParams = this.specMgr.getMethodParams(this.pointer, true); + let pathParams = this.specMgr.getMethodParams(this.pointer); let bodyParam = pathParams.find(param => param.in === 'body'); return bodyParam; } diff --git a/lib/components/ParamsList/params-list.ts b/lib/components/ParamsList/params-list.ts index 3bf40dcf..6878176b 100644 --- a/lib/components/ParamsList/params-list.ts +++ b/lib/components/ParamsList/params-list.ts @@ -27,12 +27,11 @@ export class ParamsList extends BaseComponent implements OnInit { init() { this.params = []; - let paramsList = this.specMgr.getMethodParams(this.pointer, true); + let paramsList = this.specMgr.getMethodParams(this.pointer); paramsList = paramsList.map(paramSchema => { let propPointer = paramSchema._pointer; if (paramSchema.in === 'body') return paramSchema; - paramSchema._name = paramSchema.name; return SchemaHelper.preprocess(paramSchema, propPointer, this.pointer); }); diff --git a/lib/services/schema-helper.service.ts b/lib/services/schema-helper.service.ts index 75a56424..3fc16333 100644 --- a/lib/services/schema-helper.service.ts +++ b/lib/services/schema-helper.service.ts @@ -215,7 +215,7 @@ export class SchemaHelper { let propPointer = propertySchema._pointer || JsonPointer.join(pointer, ['properties', propName]); propertySchema = SchemaHelper.preprocess(propertySchema, propPointer); - propertySchema._name = propName; + propertySchema.name = propName; // stop endless discriminator recursion if (propertySchema._pointer === opts.childFor) { propertySchema._pointer = null; @@ -244,7 +244,7 @@ export class SchemaHelper { var addProps = schema.additionalProperties; let ptr = addProps._pointer || JsonPointer.join(pointer, ['additionalProperties']); let res = SchemaHelper.preprocess(addProps, ptr); - res._name = ' *'; + res.name = ' *'; return res; } diff --git a/lib/services/search.service.ts b/lib/services/search.service.ts index 6116342b..eaaeb3ae 100644 --- a/lib/services/search.service.ts +++ b/lib/services/search.service.ts @@ -5,11 +5,12 @@ import { JsonPointer, groupBy, SpecManager, StringMap, snapshot } from '../utils import * as slugify from 'slugify'; import { - Spec as SwaggerSpec, - Operation as SwaggerOperation, - Schema as SwaggerSchema, - BodyParameter -} from '@types/swagger-schema-official'; + SwaggerSpec, + SwaggerOperation, + SwaggerSchema, + SwaggerBodyParameter, + SwaggerResponse +} from '../utils/swagger-typings'; import * as lunr from 'lunr'; @@ -20,12 +21,7 @@ interface IndexElement { pointer: string; } -interface SwaggerSchemaExt extends SwaggerSchema { - _pointer?: string; -} - const index = lunr(function () { - //this.field('menuId', {boost: 0}); this.field('title', {boost: 1.5}); this.field('body'); this.ref('pointer'); @@ -45,8 +41,10 @@ export class SearchService { } indexAll() { + console.time('Indexing'); this.indexPaths(this.spec.schema); this.indexTags(this.spec.schema); + console.time('Indexing end'); } search(q):StringMap { @@ -105,7 +103,7 @@ export class SearchService { } indexOperationParameters(operation: SwaggerOperation, operationPointer: string) { - const parameters = operation.parameters; + const parameters = this.spec.getMethodParams(operationPointer); if (!parameters) return; for (let i=0; iparam).schema, + this.indexSchema((param).schema, '', JsonPointer.join(paramPointer, ['schema']), operationPointer); } } @@ -142,10 +140,26 @@ export class SearchService { this.normalizer.reset(); this.indexSchema(resp.schema, '', JsonPointer.join(respPtr, 'schema'), operationPtr); } + if (resp.headers) { + this.indexOperationResponseHeaders(resp, respPtr, operationPtr); + } }); } - indexSchema(_schema:SwaggerSchemaExt, name: string, absolutePointer: string, + indexOperationResponseHeaders(response: SwaggerResponse, responsePtr: string, operationPtr: string, ) { + let headers = response.headers || []; + Object.keys(headers).forEach(headerName => { + let header = headers[headerName]; + this.index({ + pointer: `${responsePtr}/${headerName}`, + menuId: operationPtr, + title: headerName, + body: header.description + }); + }); + } + + indexSchema(_schema:SwaggerSchema, name: string, absolutePointer: string, menuPointer: string, parent?: string) { if (!_schema) return; let schema = _schema; diff --git a/lib/utils/spec-manager.ts b/lib/utils/spec-manager.ts index df367561..22ded568 100644 --- a/lib/utils/spec-manager.ts +++ b/lib/utils/spec-manager.ts @@ -7,6 +7,8 @@ import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { MdRenderer } from './md-renderer'; +import { SwaggerOperation, SwaggerParameter } from './swagger-typings'; + function getDiscriminator(obj) { return obj.discriminator || obj['x-extendedDiscriminator']; } @@ -121,9 +123,9 @@ export class SpecManager { return obj; } - getMethodParams(methodPtr, resolveRefs) { + getMethodParams(methodPtr:string):SwaggerParameter[] { /* inject JsonPointer into array elements */ - function injectPointers(array, root) { + function injectPointers(array:SwaggerParameter[], root) { if (!Array.isArray(array)) { throw new Error(`parameters must be an array. Got ${typeof array} at ${root}`); } @@ -140,17 +142,16 @@ export class SpecManager { //get path params let pathParamsPtr = JsonPointer.join(JsonPointer.dirName(methodPtr), ['parameters']); - let pathParams = this.byPointer(pathParamsPtr) || []; + let pathParams:SwaggerParameter[] = this.byPointer(pathParamsPtr) || []; let methodParamsPtr = JsonPointer.join(methodPtr, ['parameters']); - let methodParams = this.byPointer(methodParamsPtr) || []; + let methodParams:SwaggerParameter[] = this.byPointer(methodParamsPtr) || []; pathParams = injectPointers(pathParams, pathParamsPtr); methodParams = injectPointers(methodParams, methodParamsPtr); - if (resolveRefs) { - methodParams = this.resolveRefs(methodParams); - pathParams = this.resolveRefs(pathParams); - } + // resolve references + methodParams = this.resolveRefs(methodParams); + pathParams = this.resolveRefs(pathParams); return methodParams.concat(pathParams); } diff --git a/lib/utils/swagger-typings.ts b/lib/utils/swagger-typings.ts new file mode 100644 index 00000000..530f07f9 --- /dev/null +++ b/lib/utils/swagger-typings.ts @@ -0,0 +1,25 @@ +import { + Operation, + Parameter, + Schema, + BodyParameter, + HeaderParameter, + QueryParameter, + FormDataParameter, + Spec, + Response +} from '@types/swagger-schema-official'; + +interface RedocInjectedPointer { + _pointer?: string; +} + + +export interface SwaggerOperation extends Operation, RedocInjectedPointer {}; +export interface SwaggerBodyParameter extends BodyParameter, RedocInjectedPointer {} +export interface SwaggerHeaderParameter extends HeaderParameter, RedocInjectedPointer {} +export interface SwaggerQueryParameter extends QueryParameter, RedocInjectedPointer {} +export interface SwaggerFormDataParameter extends FormDataParameter, RedocInjectedPointer {} +export type SwaggerParameter = SwaggerBodyParameter | SwaggerHeaderParameter | SwaggerQueryParameter | SwaggerFormDataParameter; +export interface SwaggerSchema extends Schema, RedocInjectedPointer {}; +export { Spec as SwaggerSpec, Response as SwaggerResponse }; diff --git a/tests/unit/SpecManager.spec.ts b/tests/unit/SpecManager.spec.ts index 0ac1a03f..935d73fd 100644 --- a/tests/unit/SpecManager.spec.ts +++ b/tests/unit/SpecManager.spec.ts @@ -122,13 +122,13 @@ describe('Utils', () => { }); it('should accept pointer directly to parameters', () => { - let params = specMgr.getMethodParams('/paths/test1/get/parameters', true); + let params = specMgr.getMethodParams('/paths/test1/get/parameters'); expect(params).not.toBeNull(); params.length.should.be.equal(2); }); it('should resolve path params from Parameters Definitions Object', () => { - let params = specMgr.getMethodParams('/paths/test2/get', true); + let params = specMgr.getMethodParams('/paths/test2/get'); params.length.should.be.equal(2); params[0].name.should.be.equal('methodParam'); params[1].name.should.be.equal('extParam'); @@ -136,14 +136,14 @@ describe('Utils', () => { }); it('should resolve method params from Parameters Definitions Object', () => { - let params = specMgr.getMethodParams('/paths/test3/get', true); + let params = specMgr.getMethodParams('/paths/test3/get'); params.length.should.be.equal(1); params[0].name.should.be.equal('extParam'); params[0]._pointer.should.be.equal('#/parameters/extparam'); }); it('should throw for parameters other than array', () => { - let func = () => specMgr.getMethodParams('/paths/test4/get', true); + let func = () => specMgr.getMethodParams('/paths/test4/get'); expect(func).toThrow(); }); });