mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-25 10:03:45 +03:00
parent
e99d66d9dd
commit
4899f3e08f
|
@ -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}`});
|
||||
|
|
5
manual-types/index.d.ts
vendored
5
manual-types/index.d.ts
vendored
|
@ -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;
|
||||
|
|
|
@ -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'
|
||||
}])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
57
tests/unit/x-extended-defs.json
Normal file
57
tests/unit/x-extended-defs.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user