Discriminator support

This commit is contained in:
Roman Hotsiy 2016-01-09 22:34:44 +02:00
parent c2e712b039
commit 26b9bbc4ae
5 changed files with 55 additions and 6 deletions

View File

@ -1,5 +1,5 @@
<span *ngIf="isTrivial" class="param-type param-type-trivial" [ngClass]="type">{{_displayType}}</span> <span *ngIf="isTrivial" class="param-type param-type-trivial" [ngClass]="type">{{_displayType}}</span>
<div *ngIf="!isTrivial" class="params-wrap" [ngClass]="{'params-array': isArray}"> <div *ngIf="!isTrivial && !data.derived.length" class="params-wrap" [ngClass]="{'params-array': isArray}">
<div *ngFor="#prop of data.properties" class="param-wrap"> <div *ngFor="#prop of data.properties" class="param-wrap">
<div class="param"> <div class="param">
<div class="param-name"> <div class="param-name">
@ -19,3 +19,12 @@
</div> </div>
</div> </div>
</div> </div>
<div *ngIf="data.derived.length">
<div class="discriminator"> Based on <strong>{{data.discriminator}}</strong> field value: </div>
<tabs>
<tab *ngFor="#derived of data.derived" tabTitle="{{derived.name}}">
<json-schema pointer="{{derived.$ref}}" [final]="derived.final" [isArray]='isArray'>
</json-schema>
</tab>
</tabs>
</div>

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
import {RedocComponent, BaseComponent} from '../base'; import {RedocComponent, BaseComponent} from '../base';
import {Tabs, Tab} from '../../common/components/Tabs/tabs';
import {ElementRef} from 'angular2/core'; import {ElementRef} from 'angular2/core';
import JsonPointer from '../../utils/JsonPointer'; import JsonPointer from '../../utils/JsonPointer';
@ -8,18 +9,20 @@ import JsonPointer from '../../utils/JsonPointer';
selector: 'json-schema', selector: 'json-schema',
templateUrl: './lib/components/JsonSchema/json-schema.html', templateUrl: './lib/components/JsonSchema/json-schema.html',
styleUrls: ['./lib/components/JsonSchema/json-schema.css'], styleUrls: ['./lib/components/JsonSchema/json-schema.css'],
directives: [JsonSchema], directives: [JsonSchema, Tabs, Tab],
inputs: ['isArray'] inputs: ['isArray', 'final']
}) })
export default class JsonSchema extends BaseComponent { export default class JsonSchema extends BaseComponent {
constructor(schemaMgr, elementRef) { constructor(schemaMgr, elementRef) {
super(schemaMgr); super(schemaMgr);
this.element = elementRef.nativeElement; this.element = elementRef.nativeElement;
this.final = false;
} }
prepareModel() { prepareModel() {
this.data = {}; this.data = {};
this.data.properties = []; this.data.properties = [];
this.data.derived = [];
if (!this.componentSchema) { if (!this.componentSchema) {
throw new Error(`Can't load component schema at ${this.pointer}`); throw new Error(`Can't load component schema at ${this.pointer}`);
@ -32,6 +35,19 @@ export default class JsonSchema extends BaseComponent {
this.isArray = true; this.isArray = true;
schema = schema.items; schema = schema.items;
} }
let normPtr = schema._pointer || this.pointer;
let derived = this.schemaMgr.findDerivedDefinitions( normPtr );
if (!this.final && derived.length) {
derived.unshift({
name: JsonPointer.baseName(normPtr),
$ref: normPtr,
final: true
});
this.data.derived = derived;
this.data.discriminator = schema.discriminator;
return;
}
this.joinAllOf(schema); this.joinAllOf(schema);
if (schema.type !== 'object') { if (schema.type !== 'object') {

View File

@ -146,6 +146,7 @@ $array-marker-line-height: 1.5;
.params-wrap.params-array:before { .params-wrap.params-array:before {
content: "Array ["; content: "Array [";
padding-top: 1em;
} }
.params-wrap.params-array { .params-wrap.params-array {
@ -168,3 +169,26 @@ $array-marker-line-height: 1.5;
border-left: $line-border-erase; border-left: $line-border-erase;
height: ($param-name-height/2) + $cell-padding; height: ($param-name-height/2) + $cell-padding;
} }
.discriminator {
margin-top: 1em;
}
:host tabs li {
padding: 0.2em 0;
margin-right: 1em;
font-size: 14px;
color: green;
&:before, &:after {
content: '"';
}
&:last-of-type {
margin: 0;
}
&.active {
border-bottom: 2px solid $tree-lines-color;
}
}

View File

@ -174,7 +174,7 @@ export default class SchemaManager {
}); });
if (idx < 0) continue; if (idx < 0) continue;
res.push(defName); res.push({name: defName, $ref: `#/definitions/${defName}`});
} }
return res; return res;
} }

View File

@ -238,7 +238,7 @@ describe('Utils', () => {
let deriveDefs = schemaMgr.findDerivedDefinitions('#/definitions/Pet'); let deriveDefs = schemaMgr.findDerivedDefinitions('#/definitions/Pet');
deriveDefs.should.be.instanceof(Array); deriveDefs.should.be.instanceof(Array);
deriveDefs.should.not.be.empty; deriveDefs.should.not.be.empty;
deriveDefs.should.be.deepEqual(['Cat']); deriveDefs.should.be.deepEqual([{name: 'Cat', $ref: '#/definitions/Cat'}]);
}); });
it('should return emtpy array for definitions that dont have discriminator', () => { it('should return emtpy array for definitions that dont have discriminator', () => {