Skip to content

apache-avro crate interacts with #[serde(flatten)] #219

Closed

Description

@bompandreean

Starting from version 0.18.0 of the apache-avro crate, serde JSON deserialization behaves unexpectedly even if the avro crate is not directly used. The mere presence of the avro crate as a dependency causes a failure in deserialization logic.

Steps to reproduce:

  1. Create a new project with any version of serde.

  2. Add the following test code (which should pass as expected):

#[test]
fn test(){
    #[derive(Deserialize, PartialEq, Debug)]
    struct S3{
        f1: Option<f64>,

        #[serde(flatten)]
        inner: Inner,
    }

    #[derive(Deserialize, PartialEq, Debug)]
    struct Inner{
        f2: f64,
    }

    let test = "{
  \"f1\": 0.3,
  \"f2\": 3.76

  }";

    let result= serde_json::from_str::<S3>(&test);
    println!("result : {:#?}", result);
    result.unwrap();
}
  1. Now add any version of apache-avro greater than 0.17.0 to Cargo.toml, e.g.:
    apache-avro = "0.18.0"

  2. Re-run the test. The test will now fail with the following error:
    Error: invalid type: map, expected f64 at line 5 column 3

Notes:
The error does not occur when apache-avro = "0.17.0" is used.
The issue only appears when using floating point numbers like 0.3 and 3.76. If the values are whole numbers (e.g., 1, 2), the deserialization works fine.
The avro crate is not used in the deserialization code – its presence as a dependency appears to alter serde behavior globally, which may indicate a side effect or conflicting serde deserializer implementation introduced in avro >= 0.18.0.

Activity

  1. Kriskras99 commented on Jul 1, 2025

    @Kriskras99
    Contributor

    Introduced in commit f92cf3f (#139)

  2. martin-g commented on Jul 1, 2025

    @martin-g
    Member
  3. martin-g commented on Jul 1, 2025

    @martin-g
    Member

    Yep! It fixes it!

  4. martin-g commented on Jul 1, 2025

    @martin-g
    Member

    I am going to file an issue for bigdecimal crate.
    I can't explain myself why bigdecimal = { default-features = false, version = "0.4.8", features = ["std", "serde"] } works when there is no such feature at https://github.com/akubera/bigdecimal-rs/blob/v0.4.8/Cargo.toml#L48-L52

  5. added a commit that references this issue on Jul 1, 2025
    848a171
  6. Kriskras99 commented on Jul 1, 2025

    @Kriskras99
    Contributor

    Optional dependencies automatically generate a feature (if they're not used in other features), see the Cargo reference.

  7. Kriskras99 commented on Jul 1, 2025

    @Kriskras99
    Contributor

    @bompandreean the issue is caused by the arbitrary_precision feature that was enabled on serde_json through apache-avro.

    I can reproduce it with your test case and the following dependencies:

    [dependencies]
    serde = { version = "1.0.219", features = ["derive"] }
    serde_json = { version = "1.0.140", features = ["arbitrary_precision"] }

    See issue serde-rs/json#1157

  8. added a commit that references this issue on Jul 1, 2025
    d969805
  9. bompandreean commented on Jul 1, 2025

    @bompandreean
    Author

    Thank you all for the super speedy fix and the extra information 🚀✨

  10. bompandreean commented on Jul 1, 2025

    @bompandreean
    Author

    when are you going to make a new release with the fix ?

  11. martin-g commented on Jul 1, 2025

    @martin-g
    Member

    We just released 0.19.0.
    Maybe next month.
    Please use it as a Git dependency for now.

  12. bmitc commented on Aug 11, 2025

    @bmitc

    We are looking forward to this fix, as we recently came across this as well. We have an internal note tracking to update to the latest published version of the apache-avro crate. Hopefully it makes it into 0.20.0!

  13. martin-g commented on Aug 11, 2025

    @martin-g
    Member

    It will be part of the next release!

  14. bmitc commented on Aug 12, 2025

    @bmitc

    Sounds great! Thank you!

  15. bmitc commented on Aug 14, 2025

    @bmitc

    Do you have an estimation as to when the release will be made? The reason I ask is that since Git dependencies cannot be used when publishing crates, I am unable to publish to our internal crates registry for the package that uses apache_avro. So the options I have are to wait for 0.20.0 or internally fork this repo and release it as a crate to our internal crates registry as a stop-gap solution.

    Just posting here to try and gauge timelines, which will help me choose which solution.

  16. added this to the 0.20.0 milestone on Aug 15, 2025
  17. martin-g commented on Aug 25, 2025

    @martin-g
    Member

    0.20.0 has been released!

  18. bmitc commented on Aug 25, 2025

    @bmitc

    0.20.0 has been released!

    Awesome! Thank you for pushing this through. This will be a big help in our release process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions