Skip to content

Probe for nested classes named by Avro schema using context ClassLoader first, at read time (fix #908) - #910

Open
cowtowncoder wants to merge 8 commits into
2.xfrom
tatu-claude/2.x/908-avro-nested-class-name-probe
Open

cowtowncoder wants to merge 8 commits into
2.xfrom
tatu-claude/2.x/908-avro-nested-class-name-probe

Conversation

@cowtowncoder

@cowtowncoder cowtowncoder commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #908.

Problem

AvroSchemaHelper.getFullName(Schema) probed for nested classes with the ClassLoader that loaded the Avro module, while databind loads the class via TypeFactory.findClass() (context ClassLoader first). When Jackson jars are in a parent loader and POJOs in a child / context loader, the probe finds nothing, the dotted fallback name is used (and cached JVM-wide), and polymorphic values come back as Maps.

Changes

  • AvroSchemaHelper.getFullName() no longer loads classes at all: it returns the schema's dotted name. FullNameResolver and its static cache are removed, so nothing is probed at schema / reader construction.

  • AvroParser.getTypeId() translates the name into the actual class name (pkg.Outer.Inner to pkg.Outer$Inner) via new package-private helper AvroTypeIdClassNames:

  • AvroWriteContext.resolveUnionIndex() / resolveUnionType() match union branches by name only (exact, or with $ / . separators unified): no class loading on write.

  • AvroParser.setSchema() now assigns _rootSchema (it was never assigned), needed to reach the schema's cache.

AvroTypeIdResolver and AvroModule are unchanged.

Compatibility

  • Type ids exposed by AvroParser.getTypeId() are as before ($ for nested classes), for all consumers.
  • Public AvroSchemaHelper.getFullName(Schema) / getTypeId(Schema) now return pkg.Outer.Inner instead of pkg.Outer$Inner for nested classes named by RECORD / ENUM / FIXED schemas.
  • AvroParser.getSchema() now returns the schema in use (always returned null before).
  • Names that are not classes are probed for on each getTypeId() call (one failed lookup per dot, up to the limits): failures are not cached.
  • Lookup is not per-mapper: a custom TypeFactory.withClassLoader(...) is not consulted.

Tests

  • NestedClassTypeId908Test: compiles classes at test time into a ClassLoader the module cannot see; round-trips with and without context ClassLoader, with two loaders, and failure-then-success on one mapper and schema. Skipped if no Java compiler is available.
  • NestedClassTypeIdResolution908Test: streaming getTypeId() (no class initialization; unknown name), standard @JsonTypeInfo(use = Id.CLASS) resolver, FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED, mixed and legacy $ names.
  • NestedClassProbeLimits908Test: limits, including end-to-end count of class loading attempts.
  • SchemaNameClassInitTest (Do not initialize class named by Avro schema when resolving its full name #906) updated for dotted names.

Merge-forward note

NestedClassTypeId908Test uses javax.tools: when merging to 3.x line, avro/src/test/java/module-info.java needs requires java.compiler;.

🤖 Generated with Claude Code

…okup (#908)

`AvroSchemaHelper.getFullName()` probed for nested classes using the
ClassLoader of the Avro module, which may not see application classes
that databind loads via context ClassLoader; results were also retained
in a JVM-wide cache.

- `AvroSchemaHelper.getFullName()` no longer loads classes: returns
  dotted name from schema
- `AvroWriteContext` matches union branches by name only
- `AvroTypeIdResolver` probes for nested classes via
  `DatabindContext.resolveAndValidateSubType()`, with per-resolver cache;
  limits on dots/length (#906) moved there

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cowtowncoder cowtowncoder changed the title Resolve nested classes named by Avro schema using databind's class lookup (fix #908) Fix #908: Resolve nested classes named by Avro schema using databind's class lookup Nov 18, 2026
cowtowncoder and others added 2 commits November 17, 2026 20:48
- Always resolve via `ClassNameIdResolver` after probing, and skip probing
  if `FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED` enabled
- Add `AvroNestedClassTypeIdHandler` (registered by `AvroModule`) so that
  standard type id resolvers can resolve dotted ids of nested classes
- Only retain successful lookups (as resolved names) in resolver cache
- Handle names with mix of `$` and `.` separators, on read and write
- Move probing limit tests to class that needs no compiler; test limits
  end-to-end by counting class loading attempts

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Instead of exposing dotted names as type ids and having
`AvroTypeIdResolver` (plus a problem handler for standard resolvers)
resolve them, translate names where type id is produced, so that all
consumers get actual class names as before.

- Add `AvroTypeIdClassNames`: probes using context ClassLoader first,
  then module's, without initializing classes; retains only names of
  classes found
- `AvroParser` remembers outcome per type id for duration of parsing
- Remove `AvroNestedClassTypeIdHandler`; revert `AvroTypeIdResolver`
  and `AvroModule` to what 2.x has

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cowtowncoder cowtowncoder changed the title Fix #908: Resolve nested classes named by Avro schema using databind's class lookup Probe for nested classes named by Avro schema using context ClassLoader first, at read time (fix #908) Nov 18, 2026
cowtowncoder and others added 5 commits November 17, 2026 20:54
- `AvroTypeIdClassNames.resolve()` always returns usable id; split
  handling of generic ids from class name probing
- Single-expression two-loader lookup
- Write side: compare names with separators unified (one helper)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
#908)

- `AvroSchema.resolveTypeId()` retains type ids for which class was found
  (successful lookups only); shared by parsers using the schema
- Remove per-parser map and JVM-wide cache; `AvroTypeIdClassNames` is
  now stateless
- Assign `AvroParser._rootSchema` in `setSchema()` (was never assigned,
  so `getSchema()` always returned null)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Type id access may now require probing for nested classes, so only
get it once in `deserializeWithType()` and `mapObject()`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Probing for nested classes was repeated for every value for which no
class exists (common when binding as "untyped").

- `AvroSchema.resolveTypeId()` remembers type ids for which no class was
  found, for the ClassLoader most recently used (weakly referenced):
  lookup with another ClassLoader is done from scratch
- Add `AvroTypeIdClassNames.lookupClassLoader()`

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- `AvroSchema`: retain outcomes of type id lookups (class found or not)
  separately for each ClassLoader (weakly referenced), so that neither
  leaks to usage with another ClassLoader, nor is lost when alternating
- Retain type ids that are not probed for, too
- `AvroTypeDeserializer`: only get type id once
- Document change to `AvroSchemaHelper.getTypeId(Schema)`, and that
  `AvroParser.getSchema()` now returns schema

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant