-
-
Notifications
You must be signed in to change notification settings - Fork 208
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 schema compiler generated validation code does not check for null values on optional object fields #669
Comments
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
I suspect this is a bug in fast-json-stringify. |
I'd say that we should transfer it, it seems scoped to Most likely |
Sure, I can do it :) |
@rhighs If you want to cover the case when object can be null I think you should use |
Ok, sounds good. I'll have a look at the code first! |
I mean use the |
I saw the docs already at the time being, what I'm trying to say with this issue is that it's really hard to see what's happening when you stumble upon this bug. Having some sort of message would be great instead of exploding on the stack like above. Unfortunately there are cases where you can't say for sure if a type is |
Closing as completed by #670 |
Prerequisites
Fastify version
4.17.0
Plugin version
No response
Node.js version
18.14.2
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
13.2
Description
Defining a response schema with some non-required fields whose type is object, will carry @fastify/fast-json-stringify-compiler to generate static validation code that does not check for the possibility of such fields being null. In other words, it will only check for those fields being strictly equal to undefined. While it might seem reasonable to only check for undefined, the error caused by this scenario is very inconvenient and hard to track down as the generated code does not seem to lie on any specific file, instead it is allocated on some runtime memory.
I know this is kind of confusing so i'll provide a screenshot showing the portion of generated code I'm referring to, along with the schema that was associated with it.
and the schema that led to it:
If, for some reason, the "parent" field inside "genres" is presen with a value of
null
the validation code will enter the if branch calling whatever other generated function to validate its fields. Since accessing keys on null is illegal, the code will simply crash leading to this issue.Steps to Reproduce
Just run this
Then
Expected Behavior
With the steps provided above you should get this response:
The text was updated successfully, but these errors were encountered: