Skip to content

Commit

Permalink
OpenAPI's components object is optional
Browse files Browse the repository at this point in the history
From discussion in this PR: #187 (comment)
  • Loading branch information
JacobLey committed Dec 18, 2019
1 parent c604025 commit 9438b80
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/framework/ajv/index.ts
Expand Up @@ -85,7 +85,7 @@ function createAjv(
});
}

if (openApiSpec.components.schemas) {
if (openApiSpec.components?.schemas) {
Object.entries(openApiSpec.components.schemas).forEach(
([id, schema]: any[]) => {
ajv.addSchema(schema, `#/components/schemas/${id}`);
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/openapi.request.validator.ts
Expand Up @@ -99,7 +99,7 @@ export class RequestValidator {

const securityQueryParameter = this.getSecurityQueryParams(
usedSecuritySchema,
this._apiDocs.components.securitySchemes,
this._apiDocs.components?.securitySchemes,
);

let requestBody = pathSchema.requestBody;
Expand Down
3 changes: 1 addition & 2 deletions src/middlewares/openapi.security.ts
Expand Up @@ -55,8 +55,7 @@ export function security(
return next();
}

const securitySchemes =
context.apiDoc.components && context.apiDoc.components.securitySchemes;
const securitySchemes = context.apiDoc.components?.securitySchemes;

if (!securitySchemes) {
const message = `security referenced at path ${path}, but not defined in 'components.securitySchemes'`;
Expand Down
2 changes: 0 additions & 2 deletions test/resources/serialized.objects.yaml
Expand Up @@ -69,5 +69,3 @@ paths:
application/json:
schema:
type: object

components: {}

0 comments on commit 9438b80

Please sign in to comment.