Skip to content

Commit

Permalink
fix: x-extendedDiscriminator not working
Browse files Browse the repository at this point in the history
closes #217
  • Loading branch information
RomanHotsiy committed Mar 9, 2017
1 parent e99d66d commit 4899f3e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/utils/spec-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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}`});
Expand Down
5 changes: 5 additions & 0 deletions manual-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/SpecManager.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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'
}])
});
});
});
});
57 changes: 57 additions & 0 deletions tests/unit/x-extended-defs.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
}

0 comments on commit 4899f3e

Please sign in to comment.