Skip to content

Commit

Permalink
(fix) Error message unspecified content-type #114
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Nov 9, 2019
1 parent 56d39c6 commit c11242e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
9 changes: 7 additions & 2 deletions src/middlewares/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Request } from 'express';
import { ValidationError } from '../framework/types';

export function extractContentType(req: Request): string {
let contentType = req.headers['content-type'] || 'not_provided';
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 c11242e

Please sign in to comment.