Skip to content

Use overridable fetch instead of unidici's request #1002

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

Merged
merged 1 commit into from
Nov 23, 2022

Conversation

yacinehmito
Copy link
Contributor

@yacinehmito yacinehmito commented Nov 20, 2022

Changes

When loading the schema, the load function used to only consider the Content-Type header when capitalized. However, the HTTP specification states that headers are to be treated as case-insensitive. HTTP/2 further enforces this by stating that all HTTP headers must be converted to lowercase when encoded. Therefore, many recent implementations move away from the conventional capitalization in favour of headers in lowercase, such as the Nest framework.

In issue #988, it is shown that loading the schema from a live server implemented in Nest fails when the endpoint does not include the desired extension.

This commit fixes the issue by replacing unidici's request by fetch. The request function returns headers as a record, where the keys are left exactly as the headers are received, meaning that the casing of headers affect the output. Fetch, on the other hand, returns headers in a special object whose accessor method get will lookup headers in a case-insensitive way.

We took this opportunity to make the fetch implementation overridable. This way, it is possible for the caller to swap it out for another. It is a step towards portability, as openapiTS does not run as-is in the browser, Deno or Cloudflare Workers at the moment.

Additionally, unit tests have been added for the load function.

Fixes #988.

How to Review

I'll put comments where appropriate.

Checklist

  • Unit tests updated
  • README updated — not applicable
  • examples/ directory updated (if applicable) — not applicable

@yacinehmito
Copy link
Contributor Author

I only saw #990 after the fact. My apologies.

@drwpow
Copy link
Contributor

drwpow commented Nov 23, 2022

This is great! Love the new load test as well 💯. I don’t have any strong opinions on fetch vs request so this works for me. Happy to merge after rebasing.

When loading the schema, the `load` function used to only consider the
Content-Type header when capitalized. However, the HTTP specification
states that headers are to be treated as case-insensitive. HTTP/2
further enforces this by stating that all HTTP headers must be converted
to lowercase when encoded. Therefore, many recent implementations move
away from the conventional capitalization in favour of headers in
lowercase, such as the Nest framework.

In issue openapi-ts#998, it is shown that loading the schema from a live server
implemented in Nest fails when the endpoint does not include the desired
extension.

This commit fixes the issue by replacing unidici's request by fetch. The
request function returns headers as a record, where the keys are left
exactly as the headers are received, meaning that the casing of headers
affect the output. Fetch, on the other hand, returns header in a special
object whose accessor method `get` will lookup headers in a
case-insensitive way.

We took this opportunity to make the fetch implementation overridable.
This way, it is possible for the caller to swap it out for another. It
is step towards portability, as openapiTS does not run as-is in the
browser, Deno or Cloudflare Workers at the moment.

Additionally, unit tests have been added for the `load` function.

Fixes openapi-ts#998.
@yacinehmito yacinehmito force-pushed the pr/fix-lowercase-content-type branch from 3045235 to 33adc59 Compare November 23, 2022 17:11
@yacinehmito
Copy link
Contributor Author

yacinehmito commented Nov 23, 2022

I just rebased. Thank you for the positive message.

@drwpow drwpow merged commit f0cda7f into openapi-ts:main Nov 23, 2022
@drwpow
Copy link
Contributor

drwpow commented Nov 23, 2022

@all-contributors please add @yacinehmito for bug

@allcontributors
Copy link
Contributor

@drwpow

I've put up a pull request to add @yacinehmito! 🎉

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

Successfully merging this pull request may close these issues.

TypeError: Cannot read properties of undefined (reading 'schema')
2 participants