-
Notifications
You must be signed in to change notification settings - Fork 192
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
Support for nullable fields #314
Comments
Nullable indeed is not supported since the Rust itself does not have null values. The absence is measured via It might not be a too big of a stretch to support double In Rust world I reckon the partial updates works really well without double Thinking this further the serde itself will actually serialize all Option fields with |
Let's say you have an API that allows updating a user account, it is a partial update API, so only properties that are included will be changed, e.g. The input So as far as Serde is concerned: pub struct OptionalVsNull {
// When serialized if empty it will be omitted from the JSON
#[serde(rename = "optional_nonnull", skip_serializing_if = "Option::is_none")]
pub optional_nonnull: Option<String>,
// If the field is missing from the JSON then it will error - however `null` is allowed.
#[serde(rename = "required_nullable", deserialize_with = "Option::deserialize")]
pub required_nullable: Option<String>,
// None will result in being omitted from the JSON, whereas Some(None) will result in `{ "optional_nullable": null }`
#[serde(rename = "optional_nullable", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub optional_nullable: Option<Option<String>>,
} |
Oh yes, if you make that distinction that null should be allowed to reset some value. Sure we can have support for this. |
A single Option is serialized as null by serde, so unless you're using |
By a strict interpretation of the specification that is correct. If you don't have I'm not sure how friendly or popular having that behavior would be though... |
That's my problem, I have a bunch of fields that are Edit: even worse: I have Edit2: yeah, for the 3.0 workaround, |
Yeah, thats true people abuse the nullability in all ways in OpenAPI usually. But strictier the interpretation is better it would be for code generation though.
This is a sad fact. utoipa does not support 3.1 types at the moment because swagger-ui does not support them. Adding OpenAPI 3.1 support is something that should be done in future, e.g. if support for https://github.com/Redocly/redoc will be added then it would make sence. But having 3.1 support wihout being able to use them is excess. |
I am adding the missing attributes to the OpenAPI types which indicates that soonish those "workarounds" can be implemented but there is one thing that need to be kept in mind. That in short coming future I start add support for OpenAPI 3.1 types (at least I hope so, when I get the now all missing pieces together) With that I need to introduce conditionaly to the OpenAPI schema version that is being used by the users. E.g users should / could choose whether they want to use 3.0 or 3.1 types. Yet Swagger UI does not support 3.1 spec but perhaps when redocly is added the support will come handly. |
I just came across this issue myself. I use openapi-zod-client to autogenerate a TypeScript client from the OpenAPI spec created by utoipa. I assumed that using an When my TypeScript client received the I was able to get things to work for my use case using |
I agree, I think it should do both. If user has defined |
There is a new breaking change coming up which changes this naive nullable and required field behavior. In short changes are
The support is so far only implemented to |
Currently it doesn't seem there is support for the nullable attribute on a property.
Not required means the field may be omitted completely from the JSON. Whereas nullable means it can be present but have a null value. This distinction is useful for partial update APIs where there is a distinction between making no changes, versus modifying an optional field.
See double_option for more info on usage.
At a minimum we should be able to manually mark fields as nullable. Even better would be to detect double Options and mark them as non-required and nullable.
The text was updated successfully, but these errors were encountered: