More Swagger Typings + index headers

This commit is contained in:
Roman Hotsiy 2017-01-30 16:11:14 +02:00
parent 71edfafae2
commit 1ff87ce665
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
9 changed files with 74 additions and 35 deletions

View File

@ -58,7 +58,7 @@
<td class="param-name">
<span class="param-name-wrap" (click)="subSchema.toggle()">
<span class="param-name-content">
{{prop._name}}
{{prop.name}}
<span class="param-name-enumvalue" [hidden]="!prop._enumItem"> {{prop._enumItem?.val | json}} </span>
</span>
<svg *ngIf="prop._pointer" xmlns="http://www.w3.org/2000/svg" version="1.1" x="0" y="0" viewBox="0 0 24 24" xml:space="preserve">
@ -93,9 +93,9 @@
</tr>
<tr class="param-schema" [ngClass]="{'last': last}" [hidden]="!prop._pointer">
<td colspan="2">
<zippy [attr.disabled]="prop._name" #subSchema title="Expand" [headless]="true" (openChange)="lazySchema.load()" [(open)]="prop.expanded">
<zippy [attr.disabled]="prop.name" #subSchema title="Expand" [headless]="true" (openChange)="lazySchema.load()" [(open)]="prop.expanded">
<json-schema-lazy #lazySchema [auto]="prop.expanded" class="nested-schema" [pointer]="prop._pointer"
[nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema" absolutePointer="{{absolutePointer}}/properties/{{prop._name}}">
[nestOdd]="!nestOdd" [isRequestSchema]="isRequestSchema" absolutePointer="{{absolutePointer}}/properties/{{prop.name}}">
</json-schema-lazy>
</zippy>
</td>

View File

@ -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();

View File

@ -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;
}

View File

@ -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);
});

View File

@ -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 = '<Additional Properties> *';
res.name = '<Additional Properties> *';
return res;
}

View File

@ -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<IndexElement[]> {
@ -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; i<parameters.length; ++i) {
const param = parameters[i];
@ -119,7 +117,7 @@ export class SearchService {
if (param.in === 'body') {
this.normalizer.reset();
this.indexSchema((<BodyParameter>param).schema,
this.indexSchema((<SwaggerBodyParameter>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;

View File

@ -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);
}

View File

@ -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 };

View File

@ -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();
});
});