Skip to content

M7, schemas part 4: JSON and Avro - #34

Merged
merlimat merged 1 commit into
mainfrom
m7-schema-json-avro
Nov 22, 2026
Merged

merlimat merged 1 commit into
mainfrom
m7-schema-json-avro

Conversation

@merlimat

Copy link
Copy Markdown
Contributor

The fourth of five pull requests for M7's schemas (see PLAN.md): JSON and Avro, Java V5's Schema.json(Class) and
Schema.avro(Class), through serde and apache-avro, each behind a feature that is on by default. Protobuf follows.

What's in it

Client crate

  • Schema::json() and Schema::avro(), for a record type that serde serializes and apache-avro describes
    (#[derive(AvroSchema)]). Both tell the broker an Avro schema, as Java's do: no name, the derived definition, and
    Java's two properties. An Option field is a nullable one, with a default of null, as Java's reflection makes them.
    • JSON values go through serde_json. Like Java's, they read whichever version wrote them: a field the type doesn't
      have is left, and an Option field the message doesn't have is None.
    • Avro values are datums, without a header.
  • Avro reads each message with the schema it was written with (schema/avro.rs). A consumer of Avro, alone or as
    a key's or a value's, fetches the schema of each version the way a generic consumer does, from PR 3's
    consumer/writers.rs. apache-avro then resolves each message into the consumer's schema: it fills in what a later
    version adds, and takes the fields in the writer's order. That matters even without evolution, since Java's
    reflection orders a record's fields its own way. A key/value schema hands each part its part of the writer's
    schema.
  • Definitions are parsed once each (schema/record.rs). Java's parser takes names that apache-avro refuses, such
    as the namespace ending in $ that older Java clients give a nested class's record. A definition's names and
    namespaces become valid Avro ones before it's parsed; fields keep theirs.
  • Generic records of JSON and Avro: GenericValue::Json and GenericValue::Avro hold serde_json's and
    apache-avro's values, read with the writer's schema. GenericRecord gains Java V5's fields(), in the writer
    schema's order, and field(name), which gives an Avro union's branch without the union, as Java's record holds it.

Dependencies

  • apache-avro 0.23 (Apache-2.0) and serde, optional, behind the json and avro features. Both features take
    apache-avro, as both describe their records in Avro, and both are on by default, like the codecs.

Integration tests

  • SchemaTool.java gains json and avro, a record of a name and an age, and avro-v1 and avro-v2, the two
    versions of Java's evolution test.

Docs

  • docs/7-api.md §8: JSON and Avro.
  • PLAN.md's M7.

Choices worth a look

  • The library doesn't take apache-avro's derive. Applications enable it themselves, since the derive's code
    names apache_avro; the tests take it as a dev-dependency. The crate re-exports neither apache-avro nor
    serde_json, so an application depends on apache-avro 0.23 itself, for the derive and the trait to match.
  • GenericValue holds serde_json::Value and apache_avro::types::Value. That puts both crates' types in the
    public API before the first release, rather than mapping every Avro type onto our own enum. value() is
    apache-avro's record, unions included; field(name) gives a union's branch, as Java's does.
  • __alwaysAllowNull is "false". Java records how it derived a schema in two properties; here the nullable
    fields are the Option ones. Only Java's SchemaDefinition reads the property back, not the broker or a decoder.
  • Every Avro consumer fetches the schema of each version, its own included, once per version. Java's field order
    differs from Rust's for the same record, so a consumer can't assume a message was written with its own schema.
  • JSON writes None as null, serde's default, where Java leaves null fields out. Each reads the other.
  • Names Java's parser takes and apache-avro doesn't: each other character in a name or a namespace becomes _.
    apache-avro's own validators are process-wide, and set once by the first schema derived or parsed, so a library
    can't swap them reliably.
  • A first cut on cost: the Avro encoder builds apache-avro's datum writer for each message, and a key/value schema
    with an Avro part reads its parts' schemas from the writer's for each message. Both are small next to a send; the
    benchmarks can say whether they matter.

Testing

  • New tests:
    • unit tests:
      • the schemas Java declares, and Avro's bytes;
      • records read with another field order and an older Java namespace, and across two versions;
      • key/value with an Avro value across versions;
      • Java's definitions parsed, and those that don't parse;
      • generic JSON and Avro records with their fields;
      • which consumers fetch their messages' schemas;
      • values that don't encode or decode;
    • over loopback sockets against the simulated cluster:
      • Java's round-trip test for JSON and Avro;
      • generic records of each;
      • Java's evolution test, with a split, for each kind of consumer: each reads both versions and fetches each
        version once;
    • against Pulsar 5.0.1:
      • JSON and Avro records both ways, typed and as generic records;
      • Java's evolution test across the two clients, under BACKWARD: Java writes the first version, the segment splits,
        and Rust writes the second; both clients read all four records with the second version; a version that requires
        its new field is refused; and the topic keeps one schema history with two versions.
  • Checks: fmt, clippy (with the default features, without them, and with json or avro alone), rustdoc, the 541
    workspace tests, cargo-deny and the 54 integration tests pass. The bridge test that main ignores stays ignored.
  • Coverage: measured the way CI does: 99.8%, 15 lines missed, the same lines as main; every line this PR adds is
    covered.

The fourth of the schema pull requests (docs/7-api.md §8): Java V5's
Schema.json(Class) and Schema.avro(Class), through serde and
apache-avro, each a feature on by default. Tested on the simulated
cluster and against a 5.0.1 broker with Java's schemas, Java's V5
evolution test included.

Client crate:
- Schema::json() and Schema::avro() for a record type that serde
  serializes and apache-avro describes (#[derive(AvroSchema)]). Both
  tell the broker an Avro schema, as Java's do: no name, the derived
  definition, and Java's two properties, with __alwaysAllowNull false,
  as an Option field is the nullable one. JSON values go through
  serde_json and read whichever version wrote them, as in Java.
- Avro values are datums. A consumer of Avro, alone or as a key's or a
  value's, nested key/value schemas included, fetches the schema of
  each version as a generic consumer does, and reads each message with
  it: apache-avro resolves it into the consumer's schema, which fills
  in what a later version adds and takes the fields in the writer's
  order, which Java's reflection picks. A key/value schema hands each
  part its part of the writer's.
- Definitions are parsed once each. Java's parser takes names that
  apache-avro refuses, such as the namespace ending in $ that older
  Java clients give a nested class's record: a definition's names
  become valid Avro names first.
- Generic records of JSON and Avro: GenericValue::Json and
  GenericValue::Avro hold serde_json's and apache-avro's values, and
  GenericRecord gains Java V5's fields() and field(name), which gives
  an Avro union's branch, as Java's record holds it.
- apache-avro 0.23 (Apache-2.0) and serde are optional dependencies,
  behind the json and avro features, both on by default. The library
  doesn't take apache-avro's derive; applications and the tests do.

Integration tests against Pulsar 5.0.1: JSON and Avro records both
ways, typed and as generic records; and Java's V5 evolution test across
the two clients, under BACKWARD: Java writes the first version, the
segment splits, Rust writes the second, both clients read every record
with the second version, and a version that requires its new field is
refused. SchemaTool gains json, avro, avro-v1 and avro-v2.

Docs: docs/7-api.md §8, PLAN.md's M7.
@merlimat
merlimat merged commit 6d83012 into main Nov 22, 2026
11 checks passed
@merlimat
merlimat deleted the m7-schema-json-avro branch November 22, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant