Skip to content

Commit

Permalink
Merge pull request #116 from cdimascio/fix
Browse files Browse the repository at this point in the history
(fix) Error message unspecified content-type #114
  • Loading branch information
cdimascio committed Nov 9, 2019
2 parents 56d39c6 + f4e3863 commit 562f861
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/middlewares/openapi.request.validator.ts
Expand Up @@ -49,7 +49,7 @@ export class RequestValidator {

// cache middleware by combining method, path, and contentType
// TODO contentType could have value not_provided
const contentType = extractContentType(req);
const contentType = extractContentType(req) || 'not_provided';
const key = `${req.method}-${req.path}-${contentType}`;

if (!this._middlewareCache[key]) {
Expand Down Expand Up @@ -184,11 +184,11 @@ export class RequestValidator {
if (requestBody.content) {
const content = requestBody.content[contentType];
if (!content) {
throw validationError(
415,
path,
`unsupported media type ${contentType}`,
);
const msg =
contentType === 'not_provided'
? 'media type not specified'
: `unsupported media type ${contentType}`;
throw validationError(415, path, msg);
}
return content.schema || {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/openapi.response.validator.ts
Expand Up @@ -45,7 +45,7 @@ export class ResponseValidator {
return this.buildValidators(responses);
}

const contentType = extractContentType(req);
const contentType = extractContentType(req) || 'not_provided';
const key = `${req.method}-${req.path}-${contentType}`;

let validators = this.validatorsCache[key];
Expand Down
11 changes: 8 additions & 3 deletions src/middlewares/util.ts
Expand Up @@ -3,8 +3,11 @@ import * as Ajv from 'ajv';
import { Request } from 'express';
import { ValidationError } from '../framework/types';

export function extractContentType(req: Request): string {
let contentType = req.headers['content-type'] || 'not_provided';
export function extractContentType(req: Request): string | null {
let contentType = req.headers['content-type'];
if (!contentType) {
return null;
}
let end = contentType.indexOf(';');
end = end === -1 ? contentType.length : end;
if (contentType) {
Expand Down Expand Up @@ -43,7 +46,9 @@ export function validationError(
* TODO - do this some other way
* @param errors
*/
export function augmentAjvErrors(errors: Ajv.ErrorObject[] = []): Ajv.ErrorObject[] {
export function augmentAjvErrors(
errors: Ajv.ErrorObject[] = [],
): Ajv.ErrorObject[] {
errors.forEach(e => {
if (e.keyword === 'enum') {
const params: any = e.params;
Expand Down

0 comments on commit 562f861

Please sign in to comment.