Index parameters

This commit is contained in:
Roman Hotsiy 2017-01-26 16:46:28 +02:00
parent e1ce5ea316
commit 86f3a895a3
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 41 additions and 17 deletions

View File

@ -150,9 +150,14 @@ export class JsonSchema extends BaseSearchableComponent implements OnInit {
return item.name + (item._pointer || ''); return item.name + (item._pointer || '');
} }
trackByIdx(idx: number, _: any): number {
return idx;
}
ensureSearchIsShown(ptr: string) { ensureSearchIsShown(ptr: string) {
if (ptr.startsWith(this.absolutePointer)) { if (ptr.startsWith(this.absolutePointer)) {
let props = this.properties; let props = this.properties;
if (!props) return;
let relative = JsonPointer.relative(this.absolutePointer, ptr); let relative = JsonPointer.relative(this.absolutePointer, ptr);
let propName; let propName;
if (relative.length > 1 && relative[0] === 'properties') { if (relative.length > 1 && relative[0] === 'properties') {

View File

@ -1,6 +1,6 @@
<input #search (keyup)="update(search.value)" placeholder="Search"> <input #search (keyup)="update(search.value)" placeholder="Search">
<ul class="search-results"> <ul class="search-results">
<li class="result" *ngFor="let item of items" (click)="clickSearch(item)"> <li class="result" *ngFor="let item of items" (click)="clickSearch(item)">
{{item.menuItem.name}} {{item?.menuItem?.name}}
<li> <li>
</ul> </ul>

View File

@ -94,7 +94,7 @@ class SchemaWalker {
} }
} }
class AllOfMerger { export class AllOfMerger {
static merge(into, schemas) { static merge(into, schemas) {
into['x-derived-from'] = []; into['x-derived-from'] = [];
for (let i=0; i < schemas.length; i++) { for (let i=0; i < schemas.length; i++) {

View File

@ -1,18 +1,19 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { AppStateService } from './app-state.service'; import { AppStateService } from './app-state.service';
import { SchemaNormalizer } from './schema-normalizer.service';
import { JsonPointer, groupBy, SpecManager, StringMap } from '../utils/'; import { JsonPointer, groupBy, SpecManager, StringMap } from '../utils/';
import * as lunr from 'lunr'; import * as lunr from 'lunr';
interface IndexElement { interface IndexElement {
menuPtr: string; menuId: string;
title: string; title: string;
body: string; body: string;
pointer: string; pointer: string;
} }
const index = lunr(function () { const index = lunr(function () {
this.field('menuPtr', {boost: 0}); this.field('menuId', {boost: 0});
this.field('title', {boost: 1.5}); this.field('title', {boost: 1.5});
this.field('body'); this.field('body');
this.ref('pointer'); this.ref('pointer');
@ -22,7 +23,9 @@ const store:StringMap<IndexElement> = {};
@Injectable() @Injectable()
export class SearchService { export class SearchService {
normalizer: SchemaNormalizer;
constructor(private app: AppStateService, private spec: SpecManager) { constructor(private app: AppStateService, private spec: SpecManager) {
this.normalizer = new SchemaNormalizer(spec);
} }
ensureSearchVisible(containingPointers: string[]) { ensureSearchVisible(containingPointers: string[]) {
@ -39,7 +42,7 @@ export class SearchService {
const res:IndexElement[] = index.search(q).map(res => { const res:IndexElement[] = index.search(q).map(res => {
return store[res.ref]; return store[res.ref];
}); });
const grouped = groupBy(res, 'menuPtr'); const grouped = groupBy(res, 'menuId');
return grouped; return grouped;
} }
@ -61,14 +64,33 @@ export class SearchService {
}); });
} }
indexOperation(operation:any, opPtr:string) { indexOperation(operation:any, operationPointer:string) {
this.index({ this.index({
pointer: opPtr, pointer: operationPointer,
menuPtr: opPtr, menuId: operationPointer,
title: operation.summary, title: operation.summary,
body: operation.description body: operation.description
}); });
this.indexOperationResponses(operation, opPtr); this.indexOperationResponses(operation, operationPointer);
this.indexOperationParameters(operation, operationPointer)
}
indexOperationParameters(operation: any, operationPointer: string) {
const parameters = operation.parameters;
for (let i=0; i<parameters.length; ++i) {
const param = parameters[i];
const paramPointer = JsonPointer.join(operationPointer, ['parameters', i]);
this.index({
pointer: paramPointer,
menuId: operationPointer,
title: param.in === 'body' ? '' : param.name,
body: param.description
});
if (param.in === 'body') {
this.indexSchema(param.schema, '', JsonPointer.join(paramPointer, ['schema']), operationPointer);
}
}
} }
indexOperationResponses(operation:any, operationPtr:string) { indexOperationResponses(operation:any, operationPtr:string) {
@ -79,7 +101,7 @@ export class SearchService {
const respPtr = JsonPointer.join(operationPtr, ['responses', code]); const respPtr = JsonPointer.join(operationPtr, ['responses', code]);
this.index({ this.index({
pointer: respPtr, pointer: respPtr,
menuPtr: operationPtr, menuId: operationPtr,
title: code, title: code,
body: resp.description body: resp.description
}); });
@ -94,10 +116,8 @@ export class SearchService {
if (!_schema) return; if (!_schema) return;
let schema = _schema; let schema = _schema;
let title = name; let title = name;
if (schema.$ref) {
schema = this.spec.byPointer(_schema.$ref); schema = this.normalizer.normalize(schema, absolutePointer);
title = name + ' ' + JsonPointer.baseName(_schema.$ref);
}
let body = schema.description; // TODO: defaults, examples, etc... let body = schema.description; // TODO: defaults, examples, etc...
@ -108,12 +128,11 @@ export class SearchService {
this.index({ this.index({
pointer: absolutePointer, pointer: absolutePointer,
menuPtr: menuPointer, menuId: menuPointer,
title, title,
body body
}) })
// TODO: allof etc
if (schema.properties) { if (schema.properties) {
Object.keys(schema.properties).forEach(propName => { Object.keys(schema.properties).forEach(propName => {
let propPtr = JsonPointer.join(absolutePointer, ['properties', propName]); let propPtr = JsonPointer.join(absolutePointer, ['properties', propName]);