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

Response status code being typed as any number #208

Open
2 tasks done
Mahi opened this issue Jan 8, 2025 · 7 comments
Open
2 tasks done

Response status code being typed as any number #208

Mahi opened this issue Jan 8, 2025 · 7 comments

Comments

@Mahi
Copy link

Mahi commented Jan 8, 2025

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.1.0

Plugin version

5.1.0

Node.js version

22.13.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Fedora 41

Description

The following code doesn't error:

import fastify from 'fastify';
import {Type, TypeBoxTypeProvider} from '@fastify/type-provider-typebox';

const server = fastify().withTypeProvider<TypeBoxTypeProvider>();

server.get(
  '/',
  {
    schema: {
      response: {
        200: Type.String(),
        500: Type.Boolean(),
      },
    },
  },
  async (req, reply) => {
    // this works as expected (payload type incorrect):
    reply.status(200).send(false);

    // but this doesn't error, passes silently
    reply.status(300).send('asd');
  }
);

Expected Behavior

I would expect the reply.status() to be typed as (status: 200 | 500) but it's just (status: number) instead.

My expected behavior is also how it works out-of-the-box in fastify as well:

image

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 8, 2025

Why should be only 200 or 500 be valid. It could be any positive integer number.

@Mahi
Copy link
Author

Mahi commented Jan 8, 2025

Why should be only 200 or 500 be valid. It could be any positive integer number.

Because the response is documented as:

schema: {
  response: {
    200: Type.String(),
    500: Type.Boolean(),
  },
},

If the route had other possible return codes, I would have documented those. And since I haven't, those should be the only valid options. Which I'm obviously expecting the type checker to support, just like it does with the payload type.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 8, 2025

You make it look like a trivial change, which it possibly isnt.
You are welcome to propose a PR to handle this case.

@Mahi
Copy link
Author

Mahi commented Jan 8, 2025

You make it look like a trivial change

I have not said such thing, you are drawing conclusions.

You are welcome to propose a PR to handle this case.

I have no idea where you're coming from, I merely reported an issue on the project's GitHub issues page. That's what you're supposed to do when you face a bug in someone else's project. I have no time or interest in fixing the bug myself.

@Mahi
Copy link
Author

Mahi commented Jan 8, 2025

Also this is how fastify works out of the box, so clearly the type provider is lacking:

image

@Eomm
Copy link
Member

Eomm commented Jan 8, 2025

Not a strong TS user here, but I tend to agree that the only status codes accepted should be the ones listed by the user.

I can see only an issue, because in plain js you can define "2xx" as response schema key. In this case we should accept 201, 202, ..., 299

@jsumners
Copy link
Member

jsumners commented Jan 8, 2025

I have no time or interest in fixing the bug myself.

That is fine, but do not expect someone to pick it up unless they are also facing the same issue. The Fastify project is a community driven project. It is advanced by people working on the issues that draw their attention/needs.

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

4 participants