Skip to content

feat(common-avro,common-json,common-protobuf): retain and expose field-level schema annotations - #2368

Merged
jfallows merged 1 commit into
developfrom
claude/zilla-issue-2302-7wu9ht
Aug 13, 2026
Merged

jfallows merged 1 commit into
developfrom
claude/zilla-issue-2302-7wu9ht

Conversation

@jfallows

@jfallows jfallows commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part 1 of #2302 — codec-level retention and exposure of field-level schema annotations across common-avro, common-json, and common-protobuf. Today all three codecs silently discard any annotation they don't recognize as a standard keyword; this PR retains them and exposes them in the form natural to each format, read from the field object only (never inherited from the field's type or a named type reference).

Scope note: issue #2302 originally bundled two very differently-sized pieces of work: (a) codec-level annotation retention (§1–§3), and (b) a matcher/action config DSL with YAML schema, path-pattern matching, and per-model evaluation across model-avro/model-json/model-protobuf (§4–§8). (b) had no existing precedent in this codebase — no matcher config of any kind, no Avro path-pointer walker equivalent to JsonSchema.retainedPaths() — it was greenfield design in its own right. This PR delivers (a) as a complete, tested, mergeable unit.

Update: (b) was never picked up as a shared oss capability — the SPI issues that once cited #2302 as a blocker (#1681, #1682, #1683) only ever needed the retention/exposure work in this PR, and explicitly left tag-based field selection as "entirely its own design" for whatever extension installs it. The one concrete consumer (zilla-plus's field disclosure feature) built its own per-format matcher config directly rather than depending on a shared primitive. #2302 has been retitled/re-scoped to reflect that this PR is its complete fix, not a partial one.

Changes

  • common-avro: AvroField.attribute(String) reads an arbitrary key from the field's own JSON object, returned as JsonValue; null when absent.
  • common-json: JsonSchema.property(String) navigates to a property's compiled sub-schema (JSON Schema has no separate field object), and JsonSchema.attribute(String) reads an arbitrary key declared directly on a schema node — not inherited through $ref. JsonSchema.matchingPaths(Predicate) walks the compiled schema tree and returns the RFC 6901 pointers of every node a caller-supplied predicate matches; retainedPaths() is reimplemented on the same walker, replacing the old JsonSchemaPaths utility (deleted — it re-parsed the schema text into a second, parallel tree).
  • common-protobuf: new ProtobufConstant sealed type (Identifier/IntegerValue/FloatValue/TextValue/BooleanValue/MessageValue) plus ProtobufField.option(String), walked directly from the ANTLR parse tree rather than routed through ProtobufJson. Options need no pre-declaration by an imported .proto; message-valued options expose their structure rather than flattened text; string constants are unquoted. Along the way this fixes a grammar ambiguity where true/false field-option values parse as a bare fullIdent rather than boolLit (the ident rule folds BOOL_LIT into keywords), which would otherwise have misclassified boolean options as identifiers.
  • common-avro/common-protobuf: AvroType.matchingPaths(Predicate) and ProtobufMessage.matchingPaths(Predicate) port the same predicate-driven path-discovery shape, each over the type that actually carries annotations in that format. Both guard against infinite recursion on self-referential schemas with an identity-based "on this recursion path" Set.

Test plan

  • New unit tests added first, confirmed failing against the prior code, then implementation added until green (test-first per AGENTS.md)
  • ./mvnw clean verify -pl runtime/common-avro — tests, checkstyle, license headers, JaCoCo coverage all pass
  • ./mvnw clean verify -pl runtime/common-json — same, no regressions
  • ./mvnw clean verify -pl runtime/common-protobuf — same, no regressions
  • Verified no other implementers of AvroField/JsonSchema/ProtobufField/AvroType/ProtobufMessage exist in the codebase that the added methods could break

Fixes #2302


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Pushed a second commit filling out common-json's side of tagged-field discovery, ahead of the disclosure:/redaction work in #1689:

  • JsonSchema.matchingPaths(Predicate<JsonSchema>) — walks the compiled schema tree (properties, single-schema items, allOf/anyOf/oneOf/if/then/else) and returns the RFC 6901 pointers of every node the predicate matches, in schema declaration order. A match doesn't stop descent — nested matches under an already-matching ancestor are still reported independently, since that's a decision the caller (redaction/action dispatch) should make, not this API. $ref/patternProperties/tuple-items stay unexpanded, same boundary as retainedPaths(). This is the piece model-json needs to answer "which fields match tagged: - "zilla:tags": ["PII"]" without reimplementing schema traversal itself.
  • retainedPaths() reimplemented on the same walker, operating on the already-compiled schema tree instead of JsonSchemaPaths re-parsing the schema text into a second, parallel tree. Bonus: this incidentally fixes a latent inconsistency where pre-2019-09 $ref siblings were walked even though the validator itself (correctly) ignores them for those drafts.
  • JsonSchemaPaths/JsonSchemaPathsTest deleted outright (not deprecated) — confirmed their only call site was retainedPaths() itself, so once that migrated there were zero remaining callers. Its test coverage is preserved in JsonSchemaRetainedPathsTest, now driven through the public JsonSchema API instead of the internal utility.

Full verify (tests + checkstyle + license + JaCoCo) green on common-json, common-avro, and common-protobuf; 4935 tests total in common-json, no regressions.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Third commit: ported matchingPaths(Predicate) to common-avro and common-protobuf, so all three formats now support the same tagged-field-discovery shape ahead of #1689.

Not a literal copy of common-json's version — each format takes the predicate over the type that actually carries annotations, per the issue's "no shared evaluator" principle:

  • AvroType.matchingPaths(Predicate<AvroField>) — only AvroField has attribute(), not AvroType (Avro separates "the field" from "the field's type"; JSON Schema doesn't). Recurses through record fields, array items(), map values(), and union branches().
  • ProtobufMessage.matchingPaths(Predicate<ProtobufField>) — recurses through composite (message-typed) fields. A repeated composite field gets a synthetic - wildcard segment before descending, matching the array-of-objects convention from Avro/JSON — except a map field (protobuf desugars maps into a repeated synthetic entry message with named key/value fields) reaches its value type one segment deeper, at .../value/..., since that field is genuinely named in protobuf's own model. Worth flagging as a real cross-format caveat for whenever named: patterns need to work uniformly across all three.

Both needed something JSON Schema's port didn't: an identity-based cycle guard. Unlike JSON Schema (safe only because $ref is deliberately never expanded), Avro and Protobuf both allow genuine self-reference through their normal composite-field mechanism — AvroType's own javadoc already states recursive types compile to a cyclic graph by node identity, and there's an existing test proving it (shouldTerminateOnRecursiveType); Protobuf messages can self-reference the same way via a repeated field of their own type. Guarded with a plain HashSet tracking "on this recursion path" (neither AvroNode nor ProtobufMessage override equals/hashCode, so identity semantics fall out for free) — a match never stops descent, only revisiting a message/record already on the current path does.

Full verify (tests + checkstyle + license + JaCoCo) green on both: common-avro 143 tests, common-protobuf 198 tests, no regressions.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Correction to the two comments above: both quote or reference things that don't belong in this thread — a literal tagged: - "zilla:tags": ["PII"] example lifted from #2302's issue text, and repeated framing around "#1689's redaction/disclosure work." Neither is accurate context for this PR: no tagged:/matcher config exists yet (that's explicitly out of scope here, see the PR description), and #1689 is a separate, unbuilt feature this groundwork doesn't implement or depend on. I don't have a way to edit those comments in place, so noting the correction here instead.

For the record, what actually shipped in the two commits those comments describe, without the extraneous framing:

  • JsonSchema.matchingPaths(Predicate<JsonSchema>) walks the compiled schema tree (properties, single-schema items, allOf/anyOf/oneOf/if/then/else) and returns the RFC 6901 pointers of every node a caller-supplied predicate matches, in schema declaration order. A match doesn't stop descent. $ref/patternProperties/tuple-items stay unexpanded, same boundary as retainedPaths() (which is now reimplemented on this same walker, replacing the deleted JsonSchemaPaths utility).
  • AvroType.matchingPaths(Predicate<AvroField>) and ProtobufMessage.matchingPaths(Predicate<ProtobufField>) apply the same shape to the other two formats, each over the type that actually carries annotations there. Both add an identity-based recursion guard the JSON Schema version doesn't need, since Avro and Protobuf both permit genuine self-referential schemas through their normal composite-field mechanism.

All test/build claims in the two prior comments (test counts, verify results) are accurate and unaffected by this correction.


Generated by Claude Code

…d-level schema annotations

Part 1 of #2302 -- codec-level retention and exposure of field-level
schema annotations. Today all three codecs silently discard any
annotation they don't recognize as a standard keyword; this retains
them and exposes them in the form natural to each format, read from
the field object only (never inherited from the field's type or a
named type reference).

Issue #2302 bundles two very differently-sized pieces of work: (a)
codec-level annotation retention, and (b) a matcher/action config DSL
with YAML schema, path-pattern matching, and per-model evaluation
across model-avro/model-json/model-protobuf. (b) has no existing
precedent in this codebase and is greenfield design in its own right;
this change delivers (a) as a complete, tested, mergeable unit, and
leaves (b) for focused follow-up work.

common-avro:
- AvroField.attribute(String) reads an arbitrary key from the field's
  own JSON object, returned as JsonValue; null when absent
- AvroType.matchingPaths(Predicate<AvroField>) walks record fields,
  array items(), map values(), and union branches(), returning the
  path of every field a predicate matches, in declaration order. A
  match does not stop descent. Guards against infinite recursion on
  self-referential record types (already documented as legal by
  AvroType's own class javadoc) with an identity-based "on this
  recursion path" Set

common-json:
- JsonSchema.property(String) navigates to a property's compiled
  sub-schema (JSON Schema has no separate field object), and
  JsonSchema.attribute(String) reads an arbitrary key declared
  directly on a schema node -- not inherited through $ref
- JsonSchema.matchingPaths(Predicate<JsonSchema>) walks the compiled
  schema tree (properties, single-schema items, allOf/anyOf/oneOf/
  if/then/else) and returns the RFC 6901 pointers of every node a
  predicate matches. $ref, patternProperties, and tuple items are not
  expanded
- retainedPaths() is reimplemented on the same walker, operating on
  the already-compiled schema tree instead of a separate JsonSchemaPaths
  utility that re-parsed the schema text into a second, parallel tree.
  This also fixes a latent inconsistency where pre-2019-09 $ref siblings
  were walked even though the validator itself ignores them for those
  drafts. JsonSchemaPaths/JsonSchemaPathsTest deleted outright -- their
  only call site was retainedPaths() itself, and its test coverage is
  preserved in JsonSchemaRetainedPathsTest, now driven through the
  public JsonSchema API

common-protobuf:
- New ProtobufConstant sealed type (Identifier/IntegerValue/FloatValue/
  TextValue/BooleanValue/MessageValue) plus ProtobufField.option(String),
  walked directly from the ANTLR parse tree rather than routed through
  ProtobufJson. Options need no pre-declaration by an imported .proto;
  message-valued options expose their structure rather than flattened
  text; string constants are unquoted. Fixes a grammar ambiguity where
  true/false field-option values parse as a bare fullIdent rather than
  boolLit (the ident rule folds BOOL_LIT into keywords), which would
  otherwise have misclassified boolean options as identifiers
- ProtobufMessage.matchingPaths(Predicate<ProtobufField>) recurses
  through composite (message-typed) fields. A repeated composite field
  gets a synthetic "-" wildcard segment before descending, matching the
  array-of-objects convention used for Avro/JSON Schema -- except a map
  field (protobuf desugars maps into a repeated synthetic entry message
  with named key/value fields) reaches its value type one segment
  deeper, at .../value/..., since that field is genuinely named in
  protobuf's own model. Guards against recursive messages the same way
  AvroType does

Full verify (tests + checkstyle + license headers + JaCoCo coverage)
green on all three modules; no regressions.
@jfallows
jfallows force-pushed the claude/zilla-issue-2302-7wu9ht branch from fdb8c6f to 0c58bea Compare August 13, 2026 20:14

@jfallows jfallows left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jfallows
jfallows merged commit 0c5026a into develop Aug 13, 2026
42 checks passed
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.

Retain and expose schema field annotations from Avro, JSON Schema, and Protobuf codecs

2 participants