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

Body object in request could be null (Type change) #3299

Closed
wants to merge 3 commits into from
Closed

Body object in request could be null (Type change) #3299

wants to merge 3 commits into from

Conversation

ReeceXW
Copy link

@ReeceXW ReeceXW commented Sep 6, 2021

Checklist

@github-actions github-actions bot added the typescript TypeScript related label Sep 6, 2021
@Eomm
Copy link
Member

Eomm commented Sep 6, 2021

I don't remember if we cover this use case in a not-ts test actually.

Would you mind adding it?

@climba03003
Copy link
Member

I think it will be a breaking change for most of the TypeScript users

@ReeceXW
Copy link
Author

ReeceXW commented Sep 7, 2021

I think it will be a breaking change for most of the TypeScript users

I don't think this is a bad thing. 2 situations:

  1. They already have null checks on the body object, no type issues will arise
  2. They have no null checks on the body object, which I would consider a bug

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

Just for curiosity, the types accept req.body as undefined, what's the use case to check the specific null condition?

@climba03003
Copy link
Member

climba03003 commented Sep 7, 2021

I think it will be a breaking change for most of the TypeScript users

I don't think this is a bad thing. 2 situations:

1. They already have null checks on the body object, no type issues will arise

2. They have no null checks on the body object, which I would consider a bug

When the users specify a schema, the null check will be on the validation phase.
So, the users do not need to be double check it every time.

Also, the default type is unknown. Most of the time, the users need to cast it or use the RouteGenericInterface for the type guard.

Example,

// it is valid before and we know that body must be object
fastify.post<{ Body: object }>('/', { schema: { body: { type: 'object' } } }, function(request, reply) {
  reply.send(request.body.error)
})

For GET request, it is the only case that need the null for body as we do not validate or parse the payload.

@Eomm
Copy link
Member

Eomm commented Sep 7, 2021

For GET request, it is the only case that need the null for body as we do not validate or parse the payload.

Here the doubt: is it null or undefined?

null is still a valid json payload

@climba03003
Copy link
Member

climba03003 commented Sep 7, 2021

For GET request, it is the only case that need the null for body as we do not validate or parse the payload.

Here the doubt: is it null or undefined?

null is still a valid json payload

The default value is null and if we do not parse body and modify this value.
It should be null.

this.body = null

@RafaelGSS
Copy link
Member

For GET request, it is the only case that need the null for body as we do not validate or parse the payload.

Here the doubt: is it null or undefined?
null is still a valid json payload

The default value is null and if we do not parse body and modify this value.
It should be null.

this.body = null

@fastify/core We know why are we using null as default instead of undefined? I do feel that as @Eomm mentioned, null is still a valid payload. In those cases, might be better to use undefined, isn't it?

@jsumners
Copy link
Member

jsumners commented Sep 7, 2021

#2618

@RafaelGSS
Copy link
Member

Thanks a lot, @jsumners! So, since #2731 was landed I'm -1 in this PR, since as @climba03003 mentioned, it may break some typescript users and in fact, in the next major release this behavior will change again.

@L2jLiga
Copy link
Member

L2jLiga commented Sep 11, 2021

I'm -1 on this changes, in my code I mostly rely on request/reply schemas which should cover this case until I mark it explicitly as nullable

@Eomm Eomm requested a review from a team October 28, 2021 21:10
@Eomm Eomm added the semver-major Issue or PR that should land as semver major label Oct 28, 2021
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

@zekth
Copy link
Member

zekth commented Oct 30, 2021

I'm -1 on this changes, in my code I mostly rely on request/reply schemas which should cover this case until I mark it explicitly as nullable

Could you share a snippet? Isn't this declaration fixing this issue?

export const postWishlist = async (req: FastifyRequest<{ Body: foobar }>, resp: FastifyReply): Promise<void> => {

If so, i think we should enforce people to use this kind of declaration as it's TypeScript compliant. And users should not use something like const bar = req.body.foo as foo.

@L2jLiga
Copy link
Member

L2jLiga commented Oct 30, 2021

I'm -1 on this changes, in my code I mostly rely on request/reply schemas which should cover this case until I mark it explicitly as nullable

Could you share a snippet? Isn't this declaration fixing this issue?

export const postWishlist = async (req: FastifyRequest<{ Body: foobar }>, resp: FastifyReply): Promise<void> => {

If so, i think we should enforce people to use this kind of declaration as it's TypeScript compliant. And users should not use something like const bar = req.body.foo as foo.

Sure, here's repository: https://github.com/L2jLiga/fastify-request-body-validation

With changes from this PR typescript emit error:
image

@zekth
Copy link
Member

zekth commented Oct 30, 2021

That's a blocker indeed. It shouldn't break the defined non null types like @L2jLiga shared.

@Eomm Eomm added the v4.x Issue or pr related to Fastify v4 label Jan 5, 2022
@Eomm
Copy link
Member

Eomm commented Jan 6, 2022

@ReeceXW do you want to finalize this PR?

Reading the comments above I think we should:

  1. target the next branch
  2. within the PR feat: set undefined on null input #2731, the body can be:
    • undefined if the payload is missing --> already covered
    • null if the client sends a null payload --> should it be aligned within the JSON schema?

is that correct @fastify/typescript ?

@mcollina mcollina closed this Feb 3, 2022
@mcollina
Copy link
Member

mcollina commented Feb 3, 2022

Closed for no activity, feel free to reopen in case you still want to pursue this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-major Issue or PR that should land as semver major typescript TypeScript related v4.x Issue or pr related to Fastify v4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants