Publishing from TypeScript
Can a Node process publish a Klok datamodel that a Java route consumes with Schema.AVRO? This page records the answer,
measured rather than assumed, because the whole idea of publishing events from klok-cli rests on it.
Short answer: yes, but only if the Node side uses the schema Java registers, which is not the datamodel's own
.avsc.
What the Node client can do
pulsar-client for Node (checked at 1.18.0) supports an Avro schema on both ends:
SchemaType includes 'Avro', and both ProducerConfig and ConsumerConfig take schema?: SchemaInfo, where
SchemaInfo is { schemaType, name?, schema?, properties? } and schema is the schema JSON as a string.
ProducerMessage.data is a Buffer and Message.getData() returns a Buffer.
So the Node client registers the schema with the topic but does not serialise the payload. The caller encodes and
decodes, which in practice means avsc. That is a feature here: it puts the encoding under our control, in one place,
with the schema shipped alongside the types.
What Java registers, and why it is not the .avsc
The datamodel pipeline generates Java with avrotize a2java, and those classes are plain POJOs — no
SpecificRecord, no SCHEMA$ constant, nothing Avro specific in the jar. Schema.AVRO(SomeClass.class) therefore
falls back to Avro reflection and derives a schema from the Java class at runtime. That derived schema is what gets
registered on the topic, and it differs from the .avsc the same pipeline generated from the XSD:
|
the datamodel .avsc (avrotize x2a) |
what Schema.AVRO(POJO.class) registers |
| namespace |
klokprojectdocumentationv1 |
dev.klok.klok.datamodel.public_.klokprojectdocumentationv1 |
| field order |
the order of the XSD |
alphabetical |
| a required XSD element |
"string" |
["null","string"], default: null |
| an optional XSD element |
["null","string"], no default |
["null","string"], default: null |
| arrays |
a plain Avro array |
the array plus "java-class": "java.util.List" |
The namespace difference alone makes them different schemas: a record's full name is namespace plus name, so to Avro
these are two unrelated types.
The two experiments
1. Node encodes with the reflected schema, Java decodes. Works, completely.
id = spike-0001
type = dev.klok.documentation.project.prepared
time = 2026-09-10T00:26:40Z
repoPath = klok/integration/klok-egress-demo
projectType = INTEGRATION
route[0] = KLK001E1_Demo
Nested records, arrays, enums and the timestamp-micros logical type all survive the trip. avsc and Java's Avro agree
on the binary encoding, which is what the Avro specification promises.
2. Node encodes with the datamodel .avsc, Java's reader tries the same bytes. Fails:
invalid union index: 34
Not a clean "wrong schema" error — the reader walks into the middle of a field, because the two schemas order their
fields differently. Anything that got through would be silently wrong. The two schemas are mutually unreadable, and no
amount of Pulsar configuration changes that.
What this means for publishing from klok-cli
The TypeScript package a datamodel publishes must carry the schema Java registers, produced by the datamodel
pipeline itself — a small step that loads each generated class and writes
Schema.AVRO(clazz).getSchemaInfo().getSchemaDefinition() next to the generated types. Node then registers and encodes
with exactly the bytes-level contract every existing Java producer and consumer already uses.
The alternative — making the .avsc the wire contract on both sides by building the Java schema from it with
SchemaDefinition.withJsonDef(...) — is the better design on paper, because the XSD is supposed to be the contract and
a wire format derived from an accident of Java reflection is nobody's intent. It is not a compatible change, though: the
record's full name changes, so every topic that already carries a registered schema would see an unrelated type, and
every deployed consumer would have to move at the same moment. That is a migration with its own plan, not a step inside
this one.
Two smaller findings worth keeping
avrotize x2a writes optional fields as ["null", X] with no default. A writer must then supply every optional
field explicitly; omitting one is an encoding error rather than a null. New datamodels should carry default: null on
optional fields.
- Reflection makes every field nullable, including elements the XSD marks required. The Java type system therefore
does not enforce a required element; only the XSD and whatever validates against it do.
Reproducing this
# the schema Java registers; the shaded pulsar-client 5.0.0 also needs pulsar-client-admin-api on the classpath
javac -cp "<pulsar-client>:<pulsar-client-api>:<pulsar-client-admin-api>:<datamodel jar>" SchemaDump.java
java -cp ".:<same>" SchemaDump dev.klok.klok.datamodel.public_.klokprojectdocumentationv1.KlokProjectDocumentationV1
# the schema the datamodel generates
avrotize x2a schema-definition-xsd/<name>.xsd --out <name>.avsc
SchemaDump is eight lines: load the class, call Schema.AVRO, print getSchemaInfo().getSchemaDefinition(). Encoding
on the Node side is avro.Type.forSchema(schemaJson, { wrapUnions: false }).toBuffer(value) with avsc.
Confirmed on a live broker
The measurement above was of the wire format only. The TypeScript client 0.3 (see TypeScript
client) has since been run against a Pulsar 4.0.4 standalone: the admin API returns the schema it
registers byte for byte, a Java consumer (klok-brokerclient-java 0.3.3) reads its messages with the schema version it
registered (so Java and Node share one version and the compatibility check has nothing to reject), and TypeScript reads
what Java publishes. Against a Pulsar 5.0.0 standalone the automated part passes unchanged: the registered schema is the
reflected schema byte for byte, and TypeScript and Python read each other's messages. The packages the Gradle plugin now
generates carry the reflected schema as this page proposed.