Skip to content

Commit

Permalink
fix: filter out non-existing security schemas + warn
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Feb 7, 2018
1 parent dead161 commit ee822f6
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/services/models/SecurityRequirement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
}

0 comments on commit ee822f6

Please sign in to comment.