fix: x-extendedDiscriminator not working

closes #217
This commit is contained in:
Roman Hotsiy 2017-03-09 19:56:50 +02:00
parent e99d66d9dd
commit 4899f3e08f
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 97 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import { MdRenderer } from './md-renderer';
import { SwaggerOperation, SwaggerParameter } from './swagger-typings';
import { snapshot } from './helpers';
import { WarningsService } from '../services/warnings.service';
function getDiscriminator(obj) {
return obj.discriminator || obj['x-extendedDiscriminator'];
@ -204,12 +205,23 @@ export class SpecManager {
if (idx < 0) continue;
let derivedName = defName;
let derivedName;
if (extendedDiscriminatorProp) {
let prop = def.properties && def.properties[extendedDiscriminatorProp];
if (prop && prop.enum && prop.enum.length === 1) {
derivedName = prop.enum[0];
let subDefs = def.allOf || [];
for (let def of subDefs) {
let prop = def.properties && def.properties[extendedDiscriminatorProp];
if (prop && prop.enum && prop.enum.length === 1) {
derivedName = prop.enum[0];
break;
}
}
if (!derivedName) {
WarningsService.warn(`Incorrect usage of x-extendedDiscriminator at ${defPointer}: `
+ `can't find corresponding enum with single value in definition "${defName}"`);
continue;
}
} else {
derivedName = defName;
}
res.push({name: derivedName, $ref: `#/definitions/${defName}`});

View File

@ -13,6 +13,11 @@ declare module "*.css" {
export default content;
}
declare module "*.json" {
const content: string;
export default content;
}
declare var LIB_VERSION: any;
declare var IS_PRODUCTION: any;
declare var AOT: any;

View File

@ -1,6 +1,8 @@
'use strict';
import { SpecManager } from '../../lib/utils/spec-manager';
import * as xExtendedDefs from './x-extended-defs.json';
describe('Utils', () => {
describe('Schema manager', () => {
let specMgr: SpecManager;
@ -175,6 +177,23 @@ describe('Utils', () => {
deriveDefs.should.be.instanceof(Array);
deriveDefs.should.be.empty();
});
it('should correctly work with x-extendedDiscriminator', () => {
specMgr._schema = {
definitions: xExtendedDefs
};
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Payment');
deriveDefs.should.be.instanceof(Array);
deriveDefs.should.be.deepEqual([
{
name: 'cash',
$ref: '#/definitions/CashPayment'
}, {
name: 'paypal',
$ref: '#/definitions/PayPalPayment'
}])
});
});
});
});

View File

@ -0,0 +1,57 @@
{
"Payment": {
"x-extendedDiscriminator": "type",
"type": "object",
"required": [
"type"
],
"properties": {
"type": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"CashPayment": {
"allOf": [
{
"$ref": "#/definitions/Payment"
},
{
"properties": {
"type": {
"type": "string",
"enum": [
"cash"
]
},
"currency": {
"type": "string"
}
}
}
]
},
"PayPalPayment": {
"allOf": [
{
"$ref": "#/definitions/Payment"
},
{
"properties": {
"type": {
"type": "string",
"enum": [
"paypal"
]
},
"userEmail": {
"type": "string"
}
}
}
]
}
}