Skip to content

fix(avro): support maps, nullable fields and recursive schemas - #693

Merged
wilmveel merged 1 commit into
masterfrom
fix/avro-collections-and-recursive-schemas
Jul 29, 2026
Merged

fix(avro): support maps, nullable fields and recursive schemas#693
wilmveel merged 1 commit into
masterfrom
fix/avro-collections-and-recursive-schemas

Conversation

@wilmveel

Copy link
Copy Markdown
Contributor

Summary

The Avro extension emitted code that did not compile — or schemas Avro refused to parse — for several shapes the Wirespec language allows. This fixes the emitter for those shapes and adds tests covering each one.

What was broken

Shape Before
Map<String, T> TODO() while flattening the schema; no to/from conversion at all
Nullable field Dereferenced unconditionally — a null field threw instead of staying null
List<primitive> Converted per element as though it held records
String-shaped values Cast straight to String, but Avro hands them back as Utf8
bytes Round-tripped through String(...) rather than the ByteArray the model holds
Recursive type Spliced its own SCHEMA in and recursed forever (directly or via a nested record)

Approach

Each schema is now emitted per definition and self-contained, rather than picked out of the whole module. Names are tracked while flattening: a record or enum is written out in full the first time it appears and referred to by bare name after that — which is both how Avro deduplicates and the only way to express a recursive type.

to/from conversions were reworked to be driven by the reference shape: collections of primitives pass through untouched, record-valued ones convert per entry, and nullable slots keep their null under ?. instead of being dereferenced.

Test plan

  • New emitKotlinCollections — maps, arrays of primitives, nullable collections and nullable records, asserting both the emitted conversions and the map schema
  • New emitKotlinRecursiveSchema — a self-referencing node type, asserting the self reference is a bare name, a nested record appears in full once then by name, and SCHEMA never references itself
  • ./gradlew :src:integration:avro:jvmTest — passing

🤖 Generated with Claude Code

The Avro extension emitted code that did not compile — or schemas Avro
refused to parse — for several shapes the language allows:

- maps hit `TODO()` while flattening the schema, and had no conversion
  at all in `to`/`from`
- nullable fields were dereferenced unconditionally, so a null field
  threw instead of staying null
- arrays of primitives converted per element as though they held records
- values Avro hands back as `Utf8` were cast straight to `String`
- `bytes` round-tripped through `String(...)` rather than the ByteArray
  the model holds
- a type referring back to itself, directly or through a nested record,
  spliced its own `SCHEMA` in and recursed forever

Each schema is now emitted per definition and self-contained: names are
tracked while flattening, so a record or enum is written out in full the
first time it appears and referred to by name after that — which is both
how Avro deduplicates and the only way to express a recursive type.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@wilmveel
wilmveel merged commit 1d02ff4 into master Jul 29, 2026
11 of 12 checks passed
@wilmveel
wilmveel deleted the fix/avro-collections-and-recursive-schemas branch July 29, 2026 19:32
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