[rust] Fixed nullable byte arrays#20720
Conversation
|
This change will deserialize a |
Oh thanks, good catch. I suppose I need to take another approach. |
|
@jonasbb by the way, since you are the maintainer of I suppose one option would be to always generate |
|
In general the type within
Now the challenge here is capturing the JSON/Yaml semantics of missing/null/value into Rust. Since there is no such three state type in std, the
You don't have to use struct Foo {
#[serde_as(as = "DoubleOption<serde_with::base64::Base64>")]
#[serde(rename = "myByteArray", default, skip_serializing_if = "Option::is_none")]
pub my_byte_array: Option<Option<Vec<u8>>>,
}
struct DoubleOption<T>(PhantomData<T>);
impl<T, TAs> SerializeAs<Option<Option<T>>> for DoubleOption<TAs>
where
TAs: SerializeAs<T>,
{
fn serialize_as<S>(values: &Option<Option<T>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match values {
None => serializer.serialize_unit(),
Some(None) => serializer.serialize_none(),
Some(Some(v)) => serializer.serialize_some(&SerializeAsWrap::<T, TAs>::new(v)),
}
}
}
impl<'de, T, TAs> DeserializeAs<'de, Option<Option<T>>> for DoubleOption<TAs>
where
TAs: DeserializeAs<'de, T>,
{
fn deserialize_as<D>(deserializer: D) -> Result<Option<Option<T>>, D::Error>
where
D: Deserializer<'de>,
{
Ok(
DeserializeAsWrap::<Option<T>, Option<TAs>>::deserialize(deserializer)?
.into_inner()
.map(Some),
)
}
} |
1684ec9 to
396ae5f
Compare
|
Thanks for the details response! I tested and noticed a minor issue where Some(
DeserializeAsWrap::<Option<T>, Option<TAs>>::deserialize(deserializer)?
.into_inner(),
)since the deserializer for this will only be called when the value is included. |
Fixes one of the issues reported in #20500.
The issue is types with nullable string of bytes
will generate invalid code
this does not compile due to
serde_asnot being compatible withserde_with.This PR removes the
withand adds 2 Options if the type is a nullable byte arrayPR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)cc: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro