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

fix: lowercase type-providers headers types #4928

Merged
merged 2 commits into from Jul 24, 2023

Conversation

toomuchdesign
Copy link
Contributor

@toomuchdesign toomuchdesign commented Jul 23, 2023

As far as I understand, Fastify converts request headers to lowercase, while Fastify type provider seems to pass down to the type provider libraries the headers with the same casing provided in the route handler schemas.

Here is an example:

fastify.get(
  "/",
  {
    schema: {
      headers: {
        type: "object",
        properties: {
          lowercase: { type: "string" },
          UPPERCASE: { type: "number" },
        },
        required: ["lowercase", "UPPERCASE"],
      } as const,
    },
  },
  (req) => {
    req.headers.lowercase; // Correct types and runtime value
    req.headers.UPPERCASE; // Gets schema types but it's actually undefined and type should be, too
    req.headers.uppercase; // Gets generic headers types but it actually holds the actual request header value
  }
);

This PR is an attempt to align Fastify type provider types with the actual Fastify implementation. Where:

  (req) => {
    req.headers.lowercase; // Correct types and runtime value
    req.headers.UPPERCASE; // Gets schema generic headers types
    req.headers.uppercase; // Gets schema-defined types
  }

Technically speaking we might maybe considering typing the original uppercase version of the header as undefined but it would be probably too much :)

The case conversion happens on the types returned by the type provider libraries, therefore it should be transparent to them.

Open points

  • Is there any exception worth considering in Fastify headers lowercase conversion?
  • Where I'm supposed to add tests for this type change (couldn't find relevant type tests)
  • Should we consider this a fix or a breaking change?

Happy to review this PR based on your suggestions!
Thank you!

Checklist

@github-actions github-actions bot added the typescript TypeScript related label Jul 23, 2023
@mcollina
Copy link
Member

Thanks for opening a PR! Can you please add a unit test? We use tsd for types.

@toomuchdesign
Copy link
Contributor Author

Hi @mcollina! Of course, I've just added a few test cases targeting request headers types.

Extra point

I've realised that user provided route schemas can set headers value to any value even though headers can only be of type string | string[] | undefined.

Shall we consider tackling this issue in another PR to try and set request header types different then string | string[] | undefined to never?

Cheers!

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

Shall we consider tackling this issue in another PR to try and set request header types different then string | string[] | undefined to never?

I'm not sure I understand this, maybe open an issue first, or you can do a PR and talk there.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 23, 2023

Yeah the last point can be an issue, as there imho is no casting in place. The values will be imho just tested if they pass the schema validation.

So if you type a header as number you can expect that the value can be casted to a number, but is still a string.

Well that is what i know so far.

@toomuchdesign
Copy link
Contributor Author

Yep, good point @Uzlopak :)

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 23, 2023

@mcollina

Ajv will internally cast it to a number. If it can be casted it will pass. Fastify sets an option of ajv by default to cast the values. But they are only casted for the validation. It will not pass the casted values to the request. So there will be issues...

@mcollina
Copy link
Member

Ah, seems like a bummer, how should we address that? Leave things as is?

@Eomm
Copy link
Member

Eomm commented Jul 24, 2023

. It will not pass the casted values to the request. So there will be issues...

Nope:

const app = require('fastify')({ logger: true })
app.get('/', {
  schema: {
    headers: {
      type: 'object',
      properties: {
        custom: { type: 'number' }
      }
    }
  }
}, async (request, reply) => {
  return { hello: typeof request.headers.custom }
})

app.listen({ port: 8080 })
$ curl -X GET http://localhost:8080/ -H custom:42
{"hello":"number"}

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 24, 2023

well, then this PR is ready for merging

@mcollina mcollina merged commit 7b0f8ed into fastify:main Jul 24, 2023
25 checks passed
renovate bot added a commit to tomacheese/telcheck that referenced this pull request Jul 27, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [fastify](https://www.fastify.io/)
([source](https://togithub.com/fastify/fastify)) | [`4.20.0` ->
`4.21.0`](https://renovatebot.com/diffs/npm/fastify/4.20.0/4.21.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>fastify/fastify (fastify)</summary>

###
[`v4.21.0`](https://togithub.com/fastify/fastify/releases/tag/v4.21.0)

[Compare
Source](https://togithub.com/fastify/fastify/compare/v4.20.0...v4.21.0)

#### What's Changed

- chore: remove license-checker package by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[fastify/fastify#4914
- chore: remove pump devDependency by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[fastify/fastify#4913
- ci: create artifacts in coverage workflows by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[fastify/fastify#4909
- docs(server): grammar and structure fixes by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify#4904
- docs: Fix typo in TypeScript docs
("[@&#8203;types/node](https://togithub.com/types/node)", not
"[@&#8203;node/types](https://togithub.com/node/types)") by
[@&#8203;jasongwartz](https://togithub.com/jasongwartz) in
[fastify/fastify#4922
- \[readme] add CII Best Practices Badge by
[@&#8203;ljharb](https://togithub.com/ljharb) in
[fastify/fastify#4926
- fix: lowercase type-providers headers types by
[@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) in
[fastify/fastify#4928
- ERR_REP_ALREADY_SENT hint that a route may be missing "return reply"
by [@&#8203;mcollina](https://togithub.com/mcollina) in
[fastify/fastify#4921
- fix: ReplyTypeConstrainer array type inference by
[@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) in
[fastify/fastify#4885

#### New Contributors

- [@&#8203;jasongwartz](https://togithub.com/jasongwartz) made their
first contribution in
[fastify/fastify#4922
- [@&#8203;ljharb](https://togithub.com/ljharb) made their first
contribution in
[fastify/fastify#4926
- [@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) made their
first contribution in
[fastify/fastify#4928
- [@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) made
their first contribution in
[fastify/fastify#4885

**Full Changelog**:
fastify/fastify@v4.20.0...v4.21.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tomacheese/telcheck).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNC4yIiwidXBkYXRlZEluVmVyIjoiMzYuMjQuMiIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
renovate bot added a commit to redwoodjs/redwood that referenced this pull request Aug 2, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [fastify](https://www.fastify.io/)
([source](https://togithub.com/fastify/fastify)) | [`4.20.0` ->
`4.21.0`](https://renovatebot.com/diffs/npm/fastify/4.20.0/4.21.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/fastify/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/fastify/4.20.0/4.21.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>fastify/fastify (fastify)</summary>

###
[`v4.21.0`](https://togithub.com/fastify/fastify/releases/tag/v4.21.0)

[Compare
Source](https://togithub.com/fastify/fastify/compare/v4.20.0...v4.21.0)

#### What's Changed

- chore: remove license-checker package by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[fastify/fastify#4914
- chore: remove pump devDependency by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[fastify/fastify#4913
- ci: create artifacts in coverage workflows by
[@&#8203;Uzlopak](https://togithub.com/Uzlopak) in
[fastify/fastify#4909
- docs(server): grammar and structure fixes by
[@&#8203;Fdawgs](https://togithub.com/Fdawgs) in
[fastify/fastify#4904
- docs: Fix typo in TypeScript docs
("[@&#8203;types/node](https://togithub.com/types/node)", not
"[@&#8203;node/types](https://togithub.com/node/types)") by
[@&#8203;jasongwartz](https://togithub.com/jasongwartz) in
[fastify/fastify#4922
- \[readme] add CII Best Practices Badge by
[@&#8203;ljharb](https://togithub.com/ljharb) in
[fastify/fastify#4926
- fix: lowercase type-providers headers types by
[@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) in
[fastify/fastify#4928
- ERR_REP_ALREADY_SENT hint that a route may be missing "return reply"
by [@&#8203;mcollina](https://togithub.com/mcollina) in
[fastify/fastify#4921
- fix: ReplyTypeConstrainer array type inference by
[@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) in
[fastify/fastify#4885

#### New Contributors

- [@&#8203;jasongwartz](https://togithub.com/jasongwartz) made their
first contribution in
[fastify/fastify#4922
- [@&#8203;ljharb](https://togithub.com/ljharb) made their first
contribution in
[fastify/fastify#4926
- [@&#8203;toomuchdesign](https://togithub.com/toomuchdesign) made their
first contribution in
[fastify/fastify#4928
- [@&#8203;pedroescumalha](https://togithub.com/pedroescumalha) made
their first contribution in
[fastify/fastify#4885

**Full Changelog**:
fastify/fastify@v4.20.0...v4.21.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/redwoodjs/redwood).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4yNy4xIiwidXBkYXRlZEluVmVyIjoiMzYuMjcuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript TypeScript related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants