Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsupported media type for "application/json; charset=utf-8" #124

Closed
kikyomits opened this issue Nov 12, 2019 · 5 comments
Closed

unsupported media type for "application/json; charset=utf-8" #124

kikyomits opened this issue Nov 12, 2019 · 5 comments

Comments

@kikyomits
Copy link
Contributor

kikyomits commented Nov 12, 2019

Hi, my company has a rule that we mandatory set Content-Type: application.json; charset=utf-8 to all the request/response header. (I know the charset=utf-8 is probably redundant for json though.)
Since OpenAPI Validator drop charactors of Content-Type after the charactor of ; as below, our OpenAPI Validator cannot find the appropriate request body schema and respond unsupported media type application/json.

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) {
return contentType.substring(0, end);
}
return contentType;
}

Sample api spec is described as below. Due to the issue above, requests which has Content-Type: application.json; charset=utf-8 cannot be associated with the schema $ref: '#/components/schemas/user'.

paths:
  /user/:userId:
    post:
# --- skip ---
      requestBody:
        content:
          application/json; charset=utf-8:
            schema:
              $ref: '#/components/schemas/user'
# --- skip ---

I think we should use original raw value of Content-Type in request header to find appropriate schema.

const content = requestBody.content[contentType];

If you feel okay with my suggestion, let me create PR.

@cdimascio
Copy link
Owner

cdimascio commented Nov 12, 2019

@Mk811 i agree, the raw should be used. a PR is greatly appreciated!
we likely also want to fall back to the media-type if the charset or boundary are not present

The syntax is as such:
e.g.

Content-Type: text/html; charset=UTF-8
Content-Type: multipart/form-data; boundary=something

we should fall back to the media-type if a charset or boundary is not provided

@kikyomits
Copy link
Contributor Author

@cdimascio Sounds great. Thank you for the quick reply.

To sum up, if the Content-Type has charset or boundary in itself, we will use Content-Type to find appropriate request/response body from spec yaml, e.g. text/html; charset=UTF-8.
If not, we will use media-type instead, e.g. text/html.

Does it sound good for you?

@cdimascio
Copy link
Owner

Yes, that sounds good.

@cdimascio
Copy link
Owner

@Mk811 how are you making out? If u have any questions or want to discuss more freely, feel free to join us on gitter. https://gitter.im/cdimascio-oss/community

kikyomits added a commit to kikyomits/express-openapi-validator that referenced this issue Nov 16, 2019
@kikyomits kikyomits mentioned this issue Nov 16, 2019
cdimascio added a commit that referenced this issue Nov 17, 2019
@cdimascio
Copy link
Owner

fix available in v2.15.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants