-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Handle null input #2618
Comments
Could you add a Minimal, Reproducible Example? Without it, we are unable to help you. |
I've added some code to reproduce the issue |
Maybe we should add a FAQ? |
I go back to my argument that we should just accept an empty body as valid. It seems to be common for people to want to ping a |
Let's do that. Validation would fail then, but it's a more meaningful error. |
Can I try this? Just wanted to start contributing. |
go for it! |
Correct. Basically, this should work: curl -v -X POST http://example.com/post-handler |
User can set an option for the body to be null when it its supposed to be an object according to its schema. Fixes fastify#2618.
I've spent some time inspecting the code to implement the option for the object to be nullable: #2646. However, by the end of writing the code of this PR, I realized: I ran some local tests with the example provided by @jefjos and it worked: const fastify = require('fastify')()
fastify.route({
method: 'POST',
url: '/api/resources',
schema: {
body: {
type: ['object', 'null'],
properties: {
someKey: { type: 'string' , minLength: 3 }
}
}
},
handler: async function(request, reply) {
reply.send({result: 0, requestBody: request.body })
}
});
fastify.listen(3000) ==> $ curl -v -X POST http://localhost:3000/api/resources
* Trying 127.0.0.1:3000...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /api/resources HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-type: application/json; charset=utf-8
< content-length: 31
< Date: Sat, 24 Oct 2020 20:45:15 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
<
* Connection #0 to host localhost left intact
{"result":0,"requestBody":null} |
Also, I set the PR as a draft, for it to be closed it |
Since |
Test the 2 possible variants for nullable body: - type: ['object', 'null'] - type: 'object', nullable: true See: fastify#2618.
Good idea, @RafaelGSS! I just added 2 tests to validate it: #2648 Also, I saw that not only |
🤔 'use strict'
const fastify = require('fastify')({ logger: { level: 'debug' } })
fastify.route({
path: '/',
method: 'POST',
handler (req, res) {
req.log.debug(`received body: ${JSON.stringify(req.body)}`)
res.code(204).send()
}
})
fastify.listen(3000) Issuing {"level":20,"time":1603714610937,"pid":58943,"hostname":"mini2018","reqId":1,"msg":"received body: null"} I'm not sure that is correct. |
I agree |
One thing that we should consider is that If we need to reply the request body back in the response (e.g.: I am not saying that Fastify shouldn't use undefined when passing the body value through the code, but what are the implications? Will the user have access to this undefined value and try to JSON.parse it? Because then it would break. Abscence of body could also be represented as an empty string |
If someone is writing a handler wherein they expect to receive empty payloads, then it is very common practice in JavaScript to at least use a coercion check like What I'm saying is, we should document that the the empty payload is |
* Test nullable body Test the 2 possible variants for nullable body: - type: ['object', 'null'] - type: 'object', nullable: true See: #2618. * Improve POST request test As noticed by @jsumners, `fastify.inject` will pass a null body in the POST request in the abscence of a body, which may lead to false positives. What we want is a POST request that pass an empty body, not a body with "null", which is a valid JSON. By using sget, it makes a POST request which pass an empty body. See #2648 (comment).
@jsumners To clarify, this is just lacking some docs? Where'd you put them? |
@mcollina no, it is lacking in implementation as well. We currently default to |
If the default changes to |
Not clear. Maybe major? |
Shall we close it as #2731 was merged? |
The REST API I have written takes a payload optionally. Each field in the payload is optional. So I have a schema configured that validates the fields if they are sent in payload.
However I want users to be able to call the API with no payload at all. This is the error I get when I call the API without a payload -
POST http://localhost:3000/api/resources
Is there a way to treat null payload the same as empty json object - {} ?
Expected result of POST call
{"result":0}
The text was updated successfully, but these errors were encountered: