fix: filter out non-existing security schemas + warn

This commit is contained in:
Roman Hotsiy 2018-02-08 00:51:00 +02:00
parent dead16199f
commit ee822f6ebe
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0

View File

@ -2,26 +2,36 @@ import { OpenAPISecurityRequirement } from '../../types';
import { SECURITY_SCHEMES_SECTION } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser';
interface SecurityScheme {
id: string;
sectionId: string;
type: string;
scopes: string[];
}
export class SecurityRequirementModel {
schemes: Array<{
id: string;
sectionId: string;
type: string;
scopes: string[];
}>;
schemes: SecurityScheme[];
constructor(requirement: OpenAPISecurityRequirement, parser: OpenAPIParser) {
const schemes = (parser.spec.components && parser.spec.components.securitySchemes) || {};
this.schemes = Object.keys(requirement || {}).map(id => {
const scheme = parser.deref(schemes[id]);
const scopes = requirement[id] || [];
return {
id,
sectionId: SECURITY_SCHEMES_SECTION + id,
type: scheme.type,
scopes,
};
});
this.schemes = Object.keys(requirement || {})
.map(id => {
const scheme = parser.deref(schemes[id]);
const scopes = requirement[id] || [];
if (!scheme) {
console.warn(`Non existing security scheme referenced: ${id}. Skipping`);
return null;
}
return {
id,
sectionId: SECURITY_SCHEMES_SECTION + id,
type: scheme.type,
scopes,
};
})
.filter(scheme => scheme !== undefined) as SecurityScheme[];
}
}