-
Notifications
You must be signed in to change notification settings - Fork 34
fix #226: Index-Out-Of-Bounds panic when using #[serde(skip_serializing_if=..)] #227
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for skip_field
in SerializeStruct
to prevent index-out-of-bounds panics when using #[serde(skip_serializing_if)]
, and introduces tests covering various skip scenarios.
- Implemented
skip_field
to increment the item counter for skipped fields - Added tests for skipping the first, middle, last, and multiple struct fields
- Ensures no panic occurs when serializing with skipped optional fields
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
avro/tests/avro-rs-226.rs | New tests for skip-serializing-if behavior in structs |
avro/src/ser_schema.rs | Added skip_field method to SerializeStruct impl |
Comments suppressed due to low confidence (2)
avro/tests/avro-rs-226.rs:6
- [nitpick] The test function names reference issue 225 but the PR fixes issue 226. Consider renaming these to
avro_rs_226_...
for consistency.
fn avro_rs_225_index_out_of_bounds_with_serde_skip_serializing_skip_middle_field() -> TestResult {
avro/tests/avro-rs-226.rs:22
- These tests only ensure no panic occurs but don’t verify the serialized output. Consider adding assertions to check that only the expected fields are serialized.
writer.into_inner()?;
I applied the suggestions by Copilot (compare the serialized record with the deserialized one) and the assertion fails ... The |
This won't work with the current version of avro-rs. With your PR the field is properly skipped, i.e. nothing is serialized for this field. For full roundtrip we will need a Serde-driven deserialization too. @jdarais Do you agree with me here ? |
@martin-g any ETA on this? I do understand that my PR is not fixing anything. My current situation is that I have a struct that derives serde::Serialize, and I need to use the same struct for CSV, JSON, and AVRO. Since I am trying to avoid redundant fields (mostly for JSON), I cannot remove the Is there a workaround you can suggest? |
…ializing_if = "Option::is_none")]
…d one Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
… field We need to serialize something, otherwise the deserialization does not know that something has been skipped and tries to read a value according to the schema Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
@elad-yosifon While working on the serde-driven deserialization I've realized that |
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
@martin-g thanks for the update. Are you done with the debugging, or still at it? |
I have some other things to do now but I will continue debugging it later. |
@martin-g maybe it is a niche optimization, as all of the NONE optionals are at the tail, so one might think there is no need to indicate the skip. |
The The |
|
Serde's `skip` breaks it because it does not notify us when `#[serde(skip_serialize)]` is used. Drop the support for Struct tuple (e.g. `struct S(A, B, C)` - it is hard to map it to Avro record schema. It should still work for Avro array. Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
@elad-yosifon I'd be OK to merge this PR in its current state if it is enough for your use case. |
That’s good for now 👍
…On Wed, 23 Jul 2025 at 16:08 Martin Grigorov ***@***.***> wrote:
*martin-g* left a comment (apache/avro-rs#227)
<#227 (comment)>
@elad-yosifon <https://github.com/elad-yosifon> I'd be OK to merge this
PR in its current state if it is enough for your use case.
It supports skip_serializing_if but it does not support skip_serializing/
skip!
The latter will probably be supported once #237
<#237> is finished (but it is a ton
of work...).
—
Reply to this email directly, view it on GitHub
<#227 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAWZ45CIUXPDYOTJ5SHKR6L3J6CLPAVCNFSM6AAAAACBWGU2ECVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTCMBYGE4TKNJQG4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Sorry about the delayed response, I've been traveling. On this question:
I was looking into creating a serde-driven deserializer, but in the end it seemed easier to first deserialize into an avro I feel like this approach should still work for a round-trip of serialization and deserialization. If it doesn't then I think that would mean that either the serde-driven serialization or the schema-driven deserialization has a bug, or simply that the default value used for serialization, (provided by the schema,) is different from the default value used for deserialization, (which as far as I can tell uses In general, the Using the Now that I think about it, I'm guessing that On a different topic: I see from the PR that there was some logic in there before that ensured that struct fields were written in the correct order, (they must be written in the order in which they appear in the schema,) even if the order of the fields in the rust struct is different from what's in the schema. It'd be nice to get that back in. (I realize there should have been a test for it.) |
Hi @jdarais !
The problem here is that the users expect that the schema-driven deserialization in de.rs should take into account the Serde attributes, like the
Supporting
We kinda do this at the moment.
You are totally right here! I removed all logic that didn't break any test :-/ |
#227 (comment) Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
|
…#242) #227 (comment) Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Ah, those are some good points...
I think
Ah, that's a very good point. That's a bummer that the value isn't provided in |
fix #226: Index-Out-Of-Bounds panic when using #[serde(skip_serializing_if=..)]