mswjs / source

Generate MSW request handlers from various sources (HAR files, OpenAPI documents, etc).
https://source.mswjs.io
134 stars 5 forks source link

`fromOpenApi` mishandling `Accept` header #61

Closed Kiskae closed 2 weeks ago

Kiskae commented 3 weeks ago

Description

The accept header can contain whitespace in addition to ',' between content types, if the content schema does not match the first content type then it doesn't match at all.

Reproduction steps

Expected behavior

The content returns as described by the openapi spec.

Initial assessment

In https://github.com/mswjs/source/blob/main/src/open-api/utils/open-api-utils.ts toHeaders and toBody split the accept header using .split(','). This means that all content-types besides the first are prefixed by existing whitespace in the accept header. This then gets included in the regex, which makes it fail to match since the openapi content types usually don't contain whitespace.

Currently I'm patching this by adding .trim() to the contentTypeToRegExp method.

Screenshots

kettanaito commented 3 weeks ago

Hi, @Kiskae. Thanks for reporting this.

Would I be correct to assume that this can be fixed by ading .map(x => x.trim()) in here?

https://github.com/mswjs/source/blob/adaa00fd06bc0d67e9aa19790494c283f982e9a8/src/open-api/utils/open-api-utils.ts#L85-L87

If so, would you be interested in opening a pull request to help us fix this?

kettanaito commented 2 weeks ago

Released: v0.3.1 🎉

This has been released in v0.3.1!

Make sure to always update to the latest version (npm i @mswjs/source@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.