Skip to content

AvroSchemaHelper.getFullName() probes for nested classes with different ClassLoader than databind uses to load them #908

Closed

Description

@cowtowncoder

Description

AvroSchemaHelper.getFullName(Schema) has to probe for nested classes to decide whether a RECORD / ENUM / FIXED schema with namespace com.app.Outer and name Inner refers to com.app.Outer.Inner or com.app.Outer$Inner. The probe (in AvroSchemaHelper.FullNameResolver) uses

Class.forName(className, false, FullNameResolver.class.getClassLoader());

that is, the ClassLoader that loaded jackson-dataformat-avro itself. (Before #906 this was the one-argument Class.forName(className), which resolves to the same loader, so this is long-standing behavior and not something #906 introduced; #906 only made the loader explicit.)

The resulting name is then used as a type id, and the class is actually loaded by databind via TypeFactory.findClass(), which tries the thread context ClassLoader first and only then falls back to its own loader.

So the two can disagree on whether a class exists.

Failure scenario

Jackson jars are in a shared / parent ClassLoader (app server, OSGi, some plugin setups) and the POJOs are in a child or context ClassLoader:

  1. Probe for namespace com.app.Outer, name Inner gets ClassNotFoundException for every candidate, since the Avro module's loader cannot see application classes.
  2. getFullName() falls back to the dotted name com.app.Outer.Inner (and caches it).
  3. That name does not match value.getClass().getName() (com.app.Outer$Inner) when resolving union branches on write (AvroWriteContext.resolveUnionIndex() / resolveUnionType()), and cannot be resolved as a type id on read (AvroTypeIdResolver), so polymorphic / @Union properties fail or come back as Maps.

Proposed fix

Align the probe with databind's lookup order: try the thread context ClassLoader first (if any), then the module's own loader, in both cases with initialize=false (must not initialize classes named by a possibly untrusted schema; see #906).

Things to consider:

  • The JVM-wide static name cache in FullNameResolver is keyed only by (namespace, name), so with a context-loader-dependent result the cache may need to account for the loader (or not cache negative results) to avoid one application's outcome leaking to another in a shared-loader deployment.
  • The limit on number of dots probed (added in Do not initialize class named by Avro schema when resolving its full name #906) should apply equally to both lookups.

Since this changes which classes are found, it should go in 2.x (2.24) rather than a patch branch.

Activity

  1. changed the title [-](avro) `AvroSchemaHelper.getFullName()` probes for nested classes with different ClassLoader than databind uses to load them[/-] [+]`AvroSchemaHelper.getFullName()` probes for nested classes with different ClassLoader than databind uses to load them[/+] on Nov 18, 2026
  2. cowtowncoder commented on Nov 18, 2026

    @cowtowncoder
    MemberAuthor

    Analysis of how to fix this on 2.x (beyond just changing the ClassLoader), including whether the name cache could be made per-ObjectMapper.

    Why a per-mapper cache is not straightforward

    None of the places that resolve schema names has an ObjectMapper in reach:

    • Entry points are public statics: AvroSchemaHelper.getFullName(Schema) and getTypeId(Schema) take only a schema, and all callers go through them.
    • Read side: names are resolved in AvroReaderFactory.createFor(schema), called from AvroSchema.getReader(). That reader is built once and cached inside the AvroSchema, which is independent of any mapper (and may be shared by several). The parser that triggers this may have no codec at all in streaming-only use.
    • Write side: AvroWriteContext.resolveUnionIndex(Schema, Object) / resolveUnionType() are public statics called from NonBSGenericDatumWriter (subclass of Avro's own datum writer), with no generator or mapper in scope; and they run once per union value written.

    Proposed approach

    1. Write side: do not probe at all. resolveUnionIndex() / resolveUnionType() already have the value's class, so they can compare schema's namespace.name against the class name with $ normalized to .: no class loading, no cache.
    2. Read side: move the probing into AvroTypeIdResolver. _typeFromId(id, ctxt) has a DatabindContext, so it can try the dotted name and its $ variants via TypeFactory.findClass(). That uses the same ClassLoader lookup as databind (the point of this issue) and is per-mapper by construction (TypeFactory and its caches belong to the mapper). The JVM-wide static name cache can then go away, along with class loading at schema/reader construction time.

    Caveat for (2): readers would then carry the plain dotted name, so JsonParser.getTypeId() would return com.app.Outer.Inner where it returns com.app.Outer$Inner today (for nested classes named by RECORD / ENUM / FIXED schemas). That is a visible change for code reading type ids directly; acceptable for a minor release but needs a release note.

    Smaller alternative (not preferred)

    Keep the static resolver but key the cache by ClassLoader (weak map from loader to name cache) and probe the context loader first. No API change, fixes cross-application leakage, but names baked into each AvroSchema's cached reader would still depend on whichever loader was current on first use, and schema-named classes would still be loaded at reader construction.

  3. added this to the 2.24.0 milestone on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions