From 31c0db8c3ae1f1d915edbe67c927584125fb38ec Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 17 Nov 2026 20:00:47 -0800 Subject: [PATCH 1/8] Resolve nested classes named by Avro schema using databind's class lookup (#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 --- .../dataformat/avro/AvroTypeIdResolver.java | 147 ++++++++++- .../avro/schema/AvroSchemaHelper.java | 133 ++-------- .../dataformat/avro/ser/AvroWriteContext.java | 23 +- .../avro/NestedClassTypeId908Test.java | 230 ++++++++++++++++++ .../avro/schema/SchemaNameClassInitTest.java | 69 ++---- release-notes/VERSION-2.x | 8 + 6 files changed, 434 insertions(+), 176 deletions(-) create mode 100644 avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java index 2dac4c936..810491c27 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java @@ -10,10 +10,16 @@ import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; import com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver; import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.databind.util.LRUMap; /** * {@link com.fasterxml.jackson.databind.jsontype.TypeIdResolver} for Avro type IDs embedded in schemas. Avro generally uses class names, * but we want to also support named subtypes so that developers can easily remap the embedded type IDs to a different runtime class. + *

+ * Since 2.24 also handles resolution of names of nested classes: schemas (as of Avro 1.9) use dot as + * the separator between enclosing and nested class (package.Outer.Inner) so resolver needs to probe + * to find the actual class (package.Outer$Inner). This is done using the same class lookup as + * databind uses for loading the class (see [dataformats-binary#908]). * * @since 2.9 */ @@ -21,11 +27,29 @@ public class AvroTypeIdResolver extends ClassNameIdResolver { private static final long serialVersionUID = 1L; + // Maximum number of dots in namespace for which nested-class probing is done + // (moved from {@code AvroSchemaHelper} in 2.24) + final static int MAX_NAMESPACE_DOTS = 32; + + // Maximum length of full name (namespace, dot, name) for which nested-class + // probing is done: well above length of any real class name + final static int MAX_CLASS_NAME_LENGTH = 1024; + + // Marker for type ids for which no class was found + private final static Object NO_CLASS = new Object(); + private final Map> _idTypes = new HashMap<>(); - // 04-Jan-2021, tatu: Not sure why this is being kept around, not used as of - // 2.12; commenting out may be removed from 2.13 -// private final Map, String> _typeIds = new HashMap<>(); + /** + * Results of probing for (possibly nested) classes by type id: either resolved + * {@link JavaType} or marker for "no class found". Needed since type ids of + * names that are not classes (common when reading untyped content) would + * otherwise be probed for on every value read. + * Per-resolver (and thereby per-mapper), unlike JVM-wide cache used before 2.24. + * + * @since 2.24 + */ + private final LRUMap _probedTypes = new LRUMap<>(16, 200); public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, PolymorphicTypeValidator ptv, @@ -37,6 +61,15 @@ public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, //registerSubtype(namedType.getType(), namedType.getName()); _idTypes.put(namedType.getName(), namedType.getType()); } + // [dataformats-binary#908]: type ids from schema use dots for nested classes + // too: add aliases so that registered subtypes are found without probing + // (but never overriding exact matches) + for (NamedType namedType : subTypes) { + final String name = namedType.getName(); + if ((name != null) && name.indexOf('$') >= 0) { + _idTypes.putIfAbsent(name.replace('$', '.'), namedType.getType()); + } + } } } @@ -56,10 +89,16 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti } // check if there's a specific type we should be using for this ID Class subType = _idTypes.get(id); - if (subType != null) { - id = _idFrom(null, subType, _typeFactory); - } try { + if (subType != null) { + id = _idFrom(null, subType, _typeFactory); + } else { + // [dataformats-binary#908]: may need to probe for nested classes + JavaType type = _probeForType(id, ctxt); + if (type != null) { + return type; + } + } return super._typeFromId(id, ctxt); } catch (InvalidTypeIdException | IllegalArgumentException e) { // AvroTypeDeserializer expects null if we can't map the type ID to a class; It will throw an appropriate error if we can't @@ -67,4 +106,100 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti return null; } } + + /** + * Method that tries to find class that given type id (name from Avro schema) + * refers to: first as-is, then considering that trailing segments may be + * names of nested classes (based on {@code SpecificData.getClass()} of Apache Avro). + * Lookups use same mechanism (and so same ClassLoaders) as databind does, + * and are subject to the configured {@link PolymorphicTypeValidator}. + *

+ * Also handles canonical generic type ids {@code AvroReaderFactory} produces for + * {@code EnumSet} and {@code EnumMap} (like {@code java.util.EnumSet}), + * for which the first type parameter is name from schema. + * + * @return Type resolved, if any; {@code null} if no class was found + * + * @since 2.24 + */ + protected JavaType _probeForType(String id, DatabindContext ctxt) throws IOException + { + // Name to probe is either the whole id, or the first type parameter + final int ltIndex = id.indexOf('<'); + final int start, end; + if (ltIndex < 0) { + start = 0; + end = id.length(); + } else { + start = ltIndex + 1; + int ix = id.indexOf(',', start); + end = (ix < 0) ? id.indexOf('>', start) : ix; + if (end < 0) { // malformed, leave for databind to report + return null; + } + } + // Only probe names that could be dotted nested class names; and for + // which variants are worth trying (no real class has name very long + // or nested very deep) + final String name = id.substring(start, end); + if ((name.indexOf('.') < 0) || (name.indexOf('$') >= 0) || (name.indexOf('<') >= 0) + || !_shouldProbe(name)) { + return null; + } + Object cached = _probedTypes.get(id); + if (cached != null) { + return (cached == NO_CLASS) ? null : (JavaType) cached; + } + JavaType type = null; + final StringBuilder sb = new StringBuilder(id); + int lastDot = end; + while (true) { + type = _findType(sb.toString(), ctxt, ltIndex >= 0); + if (type != null) { + break; + } + // Class does not exist - perhaps last dot is actually a nested class + lastDot = sb.lastIndexOf(".", lastDot); + if (lastDot < start) { + break; + } + sb.setCharAt(lastDot, '$'); + } + _probedTypes.put(id, (type == null) ? NO_CLASS : type); + return type; + } + + private JavaType _findType(String id, DatabindContext ctxt, boolean generic) throws IOException + { + if (!generic) { + // returns `null` if no class found + return ctxt.resolveAndValidateSubType(_baseType, id, _subTypeValidator); + } + // But for generic types failure to find class (of type parameter) is + // signaled with exception by `TypeFactory` + try { + return ctxt.resolveAndValidateSubType(_baseType, id, _subTypeValidator); + } catch (IllegalArgumentException e) { + return null; + } + } + + // Whether to probe for nested classes for given (possibly untrusted) schema + // name: each dot means one more class loading attempt, so limit both number + // of dots and length. + // Package-visible for tests + static boolean _shouldProbe(String className) { + if (className.length() > MAX_CLASS_NAME_LENGTH) { + return false; + } + // One more dot than in namespace: separator between namespace and name + final int maxDots = MAX_NAMESPACE_DOTS + 1; + int count = 0; + for (int ix = className.indexOf('.'); ix >= 0; ix = className.indexOf('.', ix + 1)) { + if (++count > maxDots) { + return false; + } + } + return true; + } } diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java index 47a8345e7..072cab2d1 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java @@ -21,7 +21,6 @@ import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; import com.fasterxml.jackson.databind.util.ClassUtil; -import com.fasterxml.jackson.databind.util.LRUMap; import com.fasterxml.jackson.dataformat.avro.annotation.AvroNamespace; public abstract class AvroSchemaHelper @@ -354,12 +353,16 @@ public static String getTypeId(Schema schema) { } /** - * Returns the full name of a schema; This is similar to {@link Schema#getFullName()}, except that it properly handles namespaces for - * nested classes. (package.name.ClassName$NestedClassName instead of package.name.ClassName$.NestedClassName) - *

- * WARNING! This method has to probe for nested classes in order to resolve whether or not a schema references a top-level - * class or a nested class and return the corresponding name for each. - *

+ * Returns the full name of a schema; this is similar to {@link Schema#getFullName()}, + * except for handling of namespaces that end with dollar sign (used for nested + * classes by Apache Avro before 1.9), for which name is appended as-is. + *

+ * NOTE: since 2.24 name returned for nested classes uses dot as the separator + * ({@code package.name.ClassName.NestedClassName}), exactly as schema has it: + * this method no longer probes for existence of classes to figure out + * whether a namespace refers to a package or an enclosing class + * (see [dataformats-binary#908]). Resolution of name into an actual class + * is done by {@code AvroTypeIdResolver}, when (and only if) class is needed. */ public static String getFullName(Schema schema) { final Schema.Type type = schema.getType(); @@ -380,10 +383,10 @@ public static String getFullName(Schema schema) { if (namespace.charAt(len-1) == '$') { return namespace + name; } - // 19-Sep-2020, tatu: Due to very expensive contortions of lookups introduced - // in [dataformats-binary#195], attempts to resolve [dataformats-binary#219] - // isolated into separate class - return FullNameResolver.instance.resolve(namespace, name); + // 17-Nov-2026, tatu: [dataformats-binary#908] No longer probe for nested + // classes here: no ClassLoader that could be reliably used, and should + // not load classes just because (possibly untrusted) schema names them + return namespace + '.' + name; default: return type.getName(); @@ -442,112 +445,4 @@ public static JsonNode parseDefaultValue(String defaultValue) throws JsonMapping throw new JsonMappingException(null, "Failed to parse default value", e); } } - - // Maximum number of dots in namespace for which nested-class probing is done - final static int MAX_NAMESPACE_DOTS = 32; - - // Maximum length of full name (namespace, dot, name) for which nested-class - // probing is done: well above length of any real class name - final static int MAX_CLASS_NAME_LENGTH = 1024; - - // Whether to probe for nested classes for given (possibly untrusted) schema - // name: each dot of namespace means one more class loading attempt, and both - // probed and cached names are retained, so limit both dots and length. - // Package-visible for tests - static boolean _shouldProbe(String namespace, String name) { - if ((namespace.length() + 1 + name.length()) > MAX_CLASS_NAME_LENGTH) { - return false; - } - int count = 0; - for (int ix = namespace.indexOf('.'); ix >= 0; ix = namespace.indexOf('.', ix + 1)) { - if (++count > MAX_NAMESPACE_DOTS) { - return false; - } - } - return true; - } - - // @since 2.11.3 - private final static class FullNameResolver { - private final LRUMap SCHEMA_NAME_CACHE = new LRUMap<>(80, 800); - - public final static FullNameResolver instance = new FullNameResolver(); - - public String resolve(final String namespace, final String name) { - // No real class has name this long or nested this deep: skip - // probing (and caching) altogether - if (!_shouldProbe(namespace, name)) { - return namespace + '.' + name; - } - final FullNameKey cacheKey = new FullNameKey(namespace, name); - String schemaName = SCHEMA_NAME_CACHE.get(cacheKey); - - if (schemaName == null) { - schemaName = _resolve(cacheKey); - SCHEMA_NAME_CACHE.put(cacheKey, schemaName); - } - return schemaName; - } - - private static String _resolve(FullNameKey key) { - // 28-Feb-2020: [dataformats-binary#195] somewhat complicated logic of trying - // to support differences between avro-lib 1.8 and 1.9... - // Check if this is a nested class - // 19-Sep-2020, tatu: This is a horrible, horribly inefficient and all-around - // wrong mechanism. To be abolished if possible. - // 23-Aug-2024:[dataformats-binary#167] Based on SpecificData::getClass - // from Apache Avro. Initially assume that namespace is a Java package - StringBuilder sb = new StringBuilder(key.nameWithSeparator('.')); - int lastDot = sb.length(); - while (true) { - try { - // Try to resolve the class; only need to know it exists, so - // must not initialize it (name comes from possibly untrusted schema) - String className = sb.toString(); - Class.forName(className, false, FullNameResolver.class.getClassLoader()); - return className; - } catch (ClassNotFoundException e) { - // Class does not exist - perhaps last dot is actually a nested class. - // NOTE: `LinkageError`s (including `NoClassDefFoundError` for class - // that exists but cannot be loaded, e.g. due to missing superclass) - // are intentionally NOT caught: class may well exist, and fallback - // name would get retained by callers, masking the actual problem - lastDot = sb.lastIndexOf(".", lastDot); - if (lastDot == -1) { - // No more dots so we are unable to resolve, should we throw an exception? - return key.nameWithSeparator('.'); - } - sb.setCharAt(lastDot, '$'); - } - } - } - } - - // @since 2.11.3 - private final static class FullNameKey { - private final String _namespace, _name; - private final int _hashCode; - - public FullNameKey(String namespace, String name) { - _namespace = namespace; - _name = name; - _hashCode = namespace.hashCode() + name.hashCode(); - } - - public String nameWithSeparator(char sep) { - return _namespace + sep + _name; - } - - @Override - public int hashCode() { return _hashCode; } - - @Override - public boolean equals(Object o) { // lgtm [java/unchecked-cast-in-equals] - if (o == this) return true; - if (o == null) return false; - // Only used internally don't bother with type checks - final FullNameKey other = (FullNameKey) o; - return other._name.equals(_name) && other._namespace.equals(_namespace); - } - } } diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java index b72a0d980..a03ae9c6b 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java @@ -320,11 +320,12 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) { // - ByteBuffer // - Number wrappers (Integer, ...) - String typeId = AvroSchemaHelper.getTypeId(datum.getClass()); + final String typeId = AvroSchemaHelper.getTypeId(raw); + final String altTypeId = _dottedTypeId(typeId); for (int i = 0, size = types.size(); i < size; ++i) { Schema schema = types.get(i); // Exact schema match? - if (typeId.equals(AvroSchemaHelper.getTypeId(schema))) { + if (_matchesTypeId(typeId, altTypeId, AvroSchemaHelper.getTypeId(schema))) { return i; } } @@ -369,11 +370,12 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) { return types.get(_resolveMapIndex(unionSchema, types, datum)); } - String typeId = AvroSchemaHelper.getTypeId(datum.getClass()); + final String typeId = AvroSchemaHelper.getTypeId(raw); + final String altTypeId = _dottedTypeId(typeId); for (int i = 0, size = types.size(); i < size; ++i) { Schema schema = types.get(i); // Exact schema match? - if (typeId.equals(AvroSchemaHelper.getTypeId(schema))) { + if (_matchesTypeId(typeId, altTypeId, AvroSchemaHelper.getTypeId(schema))) { return schema; } } @@ -415,6 +417,19 @@ private static int _resolveStringIndex(Schema unionSchema, List types, return ReflectData.get().resolveUnion(unionSchema, value); } + // [dataformats-binary#908]: schemas (as of Avro 1.9) use dot as separator for + // nested classes too, whereas class names use dollar sign: so need to match + // class name against both variants. Done by comparing names only, without + // trying to load class named by schema. + private static String _dottedTypeId(String typeId) { + return (typeId.indexOf('$') < 0) ? null : typeId.replace('$', '.'); + } + + private static boolean _matchesTypeId(String typeId, String altTypeId, String schemaTypeId) { + return typeId.equals(schemaTypeId) + || ((altTypeId != null) && altTypeId.equals(schemaTypeId)); + } + private static Schema _findNotNull(List types) { if (types.size() == 2) { diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java new file mode 100644 index 000000000..ff00eb2d1 --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java @@ -0,0 +1,230 @@ +package com.fasterxml.jackson.dataformat.avro; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +import javax.tools.JavaCompiler; +import javax.tools.ToolProvider; + +import org.apache.avro.Schema; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import com.fasterxml.jackson.dataformat.avro.ser.AvroWriteContext; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +// [dataformats-binary#908]: nested classes named by schema (with dot as separator +// between enclosing and nested class) must be found using same ClassLoader(s) +// databind uses for loading classes, not just one that loaded Avro module itself. +public class NestedClassTypeId908Test extends AvroTestBase +{ + // Classes that are NOT visible via ClassLoader of the module (or tests): + // compiled on the fly, loaded via separate ClassLoader(s) + private final static String ISOLATED_SOURCE = + "package isolated908;\n" + +"public class Outer {\n" + +" public static class Bean { public Object value; }\n" + +" public static class ImplA { public String a; }\n" + +" public static class ImplB { public int b; }\n" + +"}\n"; + + private final static String NS = "isolated908.Outer"; + + private final static String SCHEMA_JSON = "{\"type\":\"record\",\"name\":\"Bean\",\"namespace\":\"" + NS + "\"," + +"\"fields\":[{\"name\":\"value\",\"type\":[" + +"{\"type\":\"record\",\"name\":\"ImplA\",\"fields\":[{\"name\":\"a\",\"type\":\"string\"}]}," + +"{\"type\":\"record\",\"name\":\"ImplB\",\"fields\":[{\"name\":\"b\",\"type\":\"int\"}]}" + +"]}]}"; + + @TempDir + static Path TEMP_DIR; + + private static URL CLASSES_URL; + + @BeforeAll + static void compileIsolatedClasses() throws Exception + { + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + assumeTrue(compiler != null, "No Java compiler available (running on JRE?)"); + + Path srcDir = TEMP_DIR.resolve("src").resolve("isolated908"); + Files.createDirectories(srcDir); + Path src = srcDir.resolve("Outer.java"); + Files.write(src, ISOLATED_SOURCE.getBytes(StandardCharsets.UTF_8)); + File classesDir = TEMP_DIR.resolve("classes").toFile(); + assertTrue(classesDir.mkdirs()); + int status = compiler.run(null, null, null, + "-d", classesDir.getAbsolutePath(), src.toString()); + assertEquals(0, status, "Compilation of isolated classes failed"); + CLASSES_URL = classesDir.toURI().toURL(); + } + + private URLClassLoader isolatedLoader() { + return new URLClassLoader(new URL[] { CLASSES_URL }, + NestedClassTypeId908Test.class.getClassLoader()); + } + + /* + /********************************************************************** + /* Test methods, ClassLoader handling + /********************************************************************** + */ + + // Sanity check for test setup itself + @Test + public void testIsolatedClassesNotVisibleToModule() throws Exception + { + try { + Class.forName(NS + "$ImplB", false, AvroMapper.class.getClassLoader()); + fail("Should not find isolated class via module's ClassLoader"); + } catch (ClassNotFoundException e) { + // expected + } + } + + // Write side: union branch chosen by comparing names, no class loading + @Test + public void testWriteResolvesUnionForNestedClass() throws Exception + { + try (URLClassLoader loader = isolatedLoader()) { + Object implA = loader.loadClass(NS + "$ImplA").getDeclaredConstructor().newInstance(); + Object implB = loader.loadClass(NS + "$ImplB").getDeclaredConstructor().newInstance(); + Schema union = new Schema.Parser().parse(SCHEMA_JSON).getField("value").schema(); + + // note: no context ClassLoader set + assertEquals(0, AvroWriteContext.resolveUnionIndex(union, implA)); + assertEquals(1, AvroWriteContext.resolveUnionIndex(union, implB)); + assertEquals("ImplA", AvroWriteContext.resolveUnionType(union, implA).getName()); + assertEquals("ImplB", AvroWriteContext.resolveUnionType(union, implB).getName()); + } + } + + @Test + public void testRoundTripWithContextClassLoader() throws Exception + { + try (URLClassLoader loader = isolatedLoader()) { + Object result = _roundTrip(loader, loader); + assertEquals(NS + "$ImplB", result.getClass().getName()); + assertSame(loader, result.getClass().getClassLoader()); + assertEquals(42, result.getClass().getField("b").get(result)); + } + } + + // Without access to ClassLoader, name does not resolve to a class: bound as "untyped" + @Test + public void testReadWithoutContextClassLoader() throws Exception + { + try (URLClassLoader loader = isolatedLoader()) { + Object result = _roundTrip(loader, null); + assertTrue(result instanceof Map, "Expected Map, got: "+result.getClass().getName()); + assertEquals(42, ((Map) result).get("b")); + } + } + + // Outcome with one ClassLoader must not leak to usage with another (as it + // did with JVM-wide name cache), regardless of order + @Test + public void testResolutionIsNotSharedAcrossClassLoaders() throws Exception + { + try (URLClassLoader loader1 = isolatedLoader(); + URLClassLoader loader2 = isolatedLoader()) { + // first: lookup that fails + assertTrue(_roundTrip(loader1, null) instanceof Map); + + Object result1 = _roundTrip(loader1, loader1); + assertSame(loader1, result1.getClass().getClassLoader()); + + Object result2 = _roundTrip(loader2, loader2); + assertSame(loader2, result2.getClass().getClassLoader()); + + // and failing lookup still fails + assertTrue(_roundTrip(loader1, null) instanceof Map); + } + } + + // Writes `Bean` with `ImplB` value, reads it back (with new mapper) using + // given context ClassLoader (if any); returns value of `Bean.value` + private Object _roundTrip(ClassLoader beanLoader, ClassLoader contextLoader) throws Exception + { + final Class beanClass = beanLoader.loadClass(NS + "$Bean"); + final Object bean = beanClass.getDeclaredConstructor().newInstance(); + final Object implB = beanLoader.loadClass(NS + "$ImplB").getDeclaredConstructor().newInstance(); + implB.getClass().getField("b").set(implB, 42); + beanClass.getField("value").set(bean, implB); + + final AvroMapper mapper = newMapper(); + final AvroSchema schema = mapper.schemaFrom(SCHEMA_JSON); + final byte[] avro = mapper.writer(schema).writeValueAsBytes(bean); + // union index 1 (zigzag: 2), then int 42 (zigzag: 84) + assertArrayEquals(new byte[] { 2, 84 }, avro); + + final Thread thread = Thread.currentThread(); + final ClassLoader oldLoader = thread.getContextClassLoader(); + thread.setContextClassLoader(contextLoader); + try { + Object result = mapper.readerFor(beanClass).with(schema).readValue(avro); + assertSame(beanClass, result.getClass()); + return beanClass.getField("value").get(result); + } finally { + thread.setContextClassLoader(oldLoader); + } + } + + /* + /********************************************************************** + /* Test methods, probing limits + /********************************************************************** + */ + + @Test + public void testDotCountLimitBoundary() throws Exception + { + final int max = AvroTypeIdResolver.MAX_NAMESPACE_DOTS; + assertEquals(32, max); + assertTrue(AvroTypeIdResolver._shouldProbe("Rec")); + assertTrue(AvroTypeIdResolver._shouldProbe("abc.Rec")); + assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max - 1) + ".Rec")); + assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max) + ".Rec")); + assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 1) + ".Rec")); + assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 100) + ".Rec")); + } + + // Limit applies to length of full name: namespace, dot, name + @Test + public void testNameLengthLimitBoundary() throws Exception + { + final int max = AvroTypeIdResolver.MAX_CLASS_NAME_LENGTH; + assertEquals(1024, max); + final String name = ".Rec"; + final int maxNamespace = max - name.length(); + assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace - 1) + name)); + assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace) + name)); + assertFalse(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace + 1) + name)); + assertFalse(AvroTypeIdResolver._shouldProbe(_longName(100_000) + name)); + } + + private static String _longName(int len) { + StringBuilder sb = new StringBuilder(len); + while (sb.length() < len) { + sb.append('x'); + } + return sb.toString(); + } + + // Name with given number of dots + private static String _dottedName(int dots) { + StringBuilder sb = new StringBuilder("a"); + for (int i = 0; i < dots; ++i) { + sb.append(".a"); + } + return sb.toString(); + } +} diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaNameClassInitTest.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaNameClassInitTest.java index 2016030ba..b84646bf3 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaNameClassInitTest.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaNameClassInitTest.java @@ -13,10 +13,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -// Resolving the Java class name of a record, enum or fixed schema must not -// initialize the class named by the (possibly untrusted) schema +// Resolving the name of a record, enum or fixed schema must not load (and +// certainly not initialize) the class named by the (possibly untrusted) schema. +// As of 2.24 [dataformats-binary#908] no probing for nested classes is done +// for schema names at all: see `NestedClassTypeId908Test` for resolution of +// type ids into nested classes. public class SchemaNameClassInitTest extends AvroTestBase { static class InitTracker { @@ -69,10 +71,10 @@ public void testReadDoesNotInitializeClassNamedBySchema() throws Exception .readValue(new byte[] { 2, 'a' }); assertEquals("a", result.value.x); - // Name is still resolved to the nested class... - assertEquals(SchemaNameClassInitTest.class.getName() + "$NotToBeInitialized", + // Name is that of schema, with no probing for nested class... + assertEquals(SchemaNameClassInitTest.class.getName() + ".NotToBeInitialized", AvroSchemaHelper.getFullName(schema.getAvroSchema().getField("value").schema())); - // ... but without running its static initializer + // ... and without running static initializer of the class assertFalse(InitTracker.initialized); } @@ -81,7 +83,7 @@ public void testEnumSchemaDoesNotInitializeNamedClass() throws Exception { Schema schema = Schema.createEnum("EnumNotToBeInitialized", null, SchemaNameClassInitTest.class.getName(), Arrays.asList("A", "B")); - assertEquals(SchemaNameClassInitTest.class.getName() + "$EnumNotToBeInitialized", + assertEquals(SchemaNameClassInitTest.class.getName() + ".EnumNotToBeInitialized", AvroSchemaHelper.getFullName(schema)); assertFalse(InitTracker.enumInitialized); } @@ -91,7 +93,7 @@ public void testFixedSchemaDoesNotInitializeNamedClass() throws Exception { Schema schema = Schema.createFixed("FixedNotToBeInitialized", null, SchemaNameClassInitTest.class.getName(), 4); - assertEquals(SchemaNameClassInitTest.class.getName() + "$FixedNotToBeInitialized", + assertEquals(SchemaNameClassInitTest.class.getName() + ".FixedNotToBeInitialized", AvroSchemaHelper.getFullName(schema)); assertFalse(InitTracker.fixedInitialized); } @@ -111,40 +113,11 @@ public void testNoClassProbingForExcessivelyDottedNamespace() throws Exception assertEquals(namespace + ".Rec", AvroSchemaHelper.getFullName(schema)); } - @Test - public void testDotCountLimitBoundary() throws Exception - { - final int max = AvroSchemaHelper.MAX_NAMESPACE_DOTS; - assertEquals(32, max); - assertTrue(AvroSchemaHelper._shouldProbe("", "Rec")); - assertTrue(AvroSchemaHelper._shouldProbe("abc", "Rec")); - assertTrue(AvroSchemaHelper._shouldProbe(_dottedName(max - 1), "Rec")); - assertTrue(AvroSchemaHelper._shouldProbe(_dottedName(max), "Rec")); - assertFalse(AvroSchemaHelper._shouldProbe(_dottedName(max + 1), "Rec")); - assertFalse(AvroSchemaHelper._shouldProbe(_dottedName(max + 100), "Rec")); - } - - // Limit applies to length of full name: namespace, dot, name - @Test - public void testNameLengthLimitBoundary() throws Exception - { - final int max = AvroSchemaHelper.MAX_CLASS_NAME_LENGTH; - assertEquals(1024, max); - final String name = "Rec"; - final int maxNamespace = max - 1 - name.length(); - assertTrue(AvroSchemaHelper._shouldProbe(_longName(maxNamespace - 1), name)); - assertTrue(AvroSchemaHelper._shouldProbe(_longName(maxNamespace), name)); - assertFalse(AvroSchemaHelper._shouldProbe(_longName(maxNamespace + 1), name)); - assertFalse(AvroSchemaHelper._shouldProbe(_longName(100_000), name)); - // and long simple name counts too - assertFalse(AvroSchemaHelper._shouldProbe("a.b", _longName(max))); - } - // Over-long name with few dots: fallback to plain dotted name @Test public void testNoClassProbingForExcessivelyLongNamespace() throws Exception { - final String namespace = "a.b.c." + _longName(AvroSchemaHelper.MAX_CLASS_NAME_LENGTH); + final String namespace = "a.b.c." + _longName(2000); Schema schema = Schema.createRecord("Rec", null, namespace, false); assertEquals(namespace + ".Rec", AvroSchemaHelper.getFullName(schema)); } @@ -157,21 +130,23 @@ private static String _longName(int len) { return sb.toString(); } - private static String _dottedName(int dots) { - StringBuilder sb = new StringBuilder("a"); - for (int i = 0; i < dots; ++i) { - sb.append(".a"); - } - return sb.toString(); + // Legacy (Avro before 1.9) namespace with trailing dollar sign: name appended as-is + @Test + public void testLegacyDollarNamespace() throws Exception + { + Schema schema = Schema.createRecord("Inner", null, + SchemaNameClassInitTest.class.getName() + "$", false); + assertEquals(SchemaNameClassInitTest.class.getName() + "$Inner", + AvroSchemaHelper.getFullName(schema)); } - // But nesting within limit is still resolved + // [dataformats-binary#908]: no probing, so dotted name even for existing nested class @Test - public void testNestedClassStillResolvedWithinDotLimit() throws Exception + public void testNestedClassNameNotProbed() throws Exception { Schema schema = Schema.createRecord("Inner", null, SchemaNameClassInitTest.class.getName(), false); - assertEquals(SchemaNameClassInitTest.class.getName() + "$Inner", + assertEquals(SchemaNameClassInitTest.class.getName() + ".Inner", AvroSchemaHelper.getFullName(schema)); } } diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index d300fa127..968a67e5b 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -14,6 +14,14 @@ Active maintainers: === Releases === ------------------------------------------------------------------------ +2.24.0 (not yet released) + +#908: (avro) `AvroSchemaHelper.getFullName()` probes for nested classes with different + ClassLoader than databind uses to load them + (NOTE: schema names are no longer probed for; type ids exposed by `AvroParser.getTypeId()` + for nested classes now use `.` instead of `$` as separator, resolved into + classes by `AvroTypeIdResolver`) + 2.23.1 (not yet released) #906: (avro) Do not initialize class named by Avro schema when resolving its full name; From 1b862ebba810bb8d1a244f834995d3a40ca00c68 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 17 Nov 2026 20:48:41 -0800 Subject: [PATCH 2/8] Address review findings for #908 - 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 --- .../jackson/dataformat/avro/AvroModule.java | 3 + .../avro/AvroNestedClassTypeIdHandler.java | 44 ++++++ .../dataformat/avro/AvroTypeIdResolver.java | 115 ++++++++------ .../dataformat/avro/ser/AvroWriteContext.java | 23 +-- .../avro/NestedClassProbeLimits908Test.java | 147 ++++++++++++++++++ .../avro/NestedClassTypeId908Test.java | 92 ++++------- .../NestedClassTypeIdResolution908Test.java | 126 +++++++++++++++ 7 files changed, 423 insertions(+), 127 deletions(-) create mode 100644 avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java create mode 100644 avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java create mode 100644 avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java index e546ecaa7..b2e45a4eb 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java @@ -102,6 +102,9 @@ protected void _addDeserializers(SetupContext context) { SimpleDeserializers desers = new SimpleDeserializers(); desers.addDeserializer(Object.class, new AvroUntypedDeserializer()); context.addDeserializers(desers); + // 17-Nov-2026, tatu: [dataformats-binary#908] Type ids for nested classes + // need resolving for standard (non-Avro) type id resolvers too + context.addDeserializationProblemHandler(new AvroNestedClassTypeIdHandler()); } // since 2.9 diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java new file mode 100644 index 000000000..d0aa5d063 --- /dev/null +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java @@ -0,0 +1,44 @@ +package com.fasterxml.jackson.dataformat.avro; + +import java.io.IOException; + +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; +import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; +import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; + +/** + * Handler registered by {@link AvroModule} to let standard (non-Avro) type id + * resolvers -- like one used for {@code @JsonTypeInfo(use = Id.CLASS)} -- + * resolve native type ids of nested classes: Avro schemas use dot as the + * separator between enclosing and nested class ({@code package.Outer.Inner}) + * so such ids are not class names as-is. + * {@link AvroTypeIdResolver} handles this by itself. + * + * @since 2.24 + */ +class AvroNestedClassTypeIdHandler + extends DeserializationProblemHandler + implements java.io.Serializable +{ + private static final long serialVersionUID = 1L; + + @Override + public JavaType handleUnknownTypeId(DeserializationContext ctxt, + JavaType baseType, String subTypeId, TypeIdResolver idResolver, + String failureMsg) throws IOException + { + // Only for native type ids from Avro content; and not if probed already + if ((idResolver instanceof AvroTypeIdResolver) + || !(ctxt.getParser() instanceof AvroParser)) { + return null; + } + final PolymorphicTypeValidator ptv = ctxt.getConfig().getPolymorphicTypeValidator(); + final String resolved = AvroTypeIdResolver._findNestedTypeId(ctxt, baseType, subTypeId, ptv); + if ((resolved == null) || resolved.equals(subTypeId)) { + return null; + } + return ctxt.resolveAndValidateSubType(baseType, resolved, ptv); + } +} diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java index 810491c27..e558aa983 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java @@ -4,6 +4,8 @@ import java.util.*; import com.fasterxml.jackson.databind.DatabindContext; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; import com.fasterxml.jackson.databind.jsontype.NamedType; @@ -35,21 +37,18 @@ public class AvroTypeIdResolver extends ClassNameIdResolver // probing is done: well above length of any real class name final static int MAX_CLASS_NAME_LENGTH = 1024; - // Marker for type ids for which no class was found - private final static Object NO_CLASS = new Object(); - private final Map> _idTypes = new HashMap<>(); /** - * Results of probing for (possibly nested) classes by type id: either resolved - * {@link JavaType} or marker for "no class found". Needed since type ids of - * names that are not classes (common when reading untyped content) would - * otherwise be probed for on every value read. + * Type ids for which probing found a class, mapped to the id of that class: + * for example {@code package.Outer.Inner} to {@code package.Outer$Inner}. + * Only successful lookups are retained: whether class is found may depend on + * context ClassLoader at the time, so failure must not be remembered. * Per-resolver (and thereby per-mapper), unlike JVM-wide cache used before 2.24. * * @since 2.24 */ - private final LRUMap _probedTypes = new LRUMap<>(16, 200); + private final LRUMap _resolvedIds = new LRUMap<>(16, 200); public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, PolymorphicTypeValidator ptv, @@ -59,13 +58,11 @@ public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, if (subTypes != null) { for (NamedType namedType : subTypes) { //registerSubtype(namedType.getType(), namedType.getName()); - _idTypes.put(namedType.getName(), namedType.getType()); - } - // [dataformats-binary#908]: type ids from schema use dots for nested classes - // too: add aliases so that registered subtypes are found without probing - // (but never overriding exact matches) - for (NamedType namedType : subTypes) { final String name = namedType.getName(); + _idTypes.put(name, namedType.getType()); + // [dataformats-binary#908]: type ids from schema use dots for nested classes + // too: add aliases so that registered subtypes are found without probing + // (but never overriding exact matches) if ((name != null) && name.indexOf('$') >= 0) { _idTypes.putIfAbsent(name.replace('$', '.'), namedType.getType()); } @@ -94,11 +91,9 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti id = _idFrom(null, subType, _typeFactory); } else { // [dataformats-binary#908]: may need to probe for nested classes - JavaType type = _probeForType(id, ctxt); - if (type != null) { - return type; - } + id = _resolveNestedTypeId(id, ctxt); } + // NOTE: always resolve via super class, for checks it has return super._typeFromId(id, ctxt); } catch (InvalidTypeIdException | IllegalArgumentException e) { // AvroTypeDeserializer expects null if we can't map the type ID to a class; It will throw an appropriate error if we can't @@ -107,22 +102,51 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti } } + /** + * @return Id of the class that given type id refers to, if one found by probing + * for nested classes; {@code id} itself otherwise + */ + private String _resolveNestedTypeId(String id, DatabindContext ctxt) throws IOException + { + String resolved = _resolvedIds.get(id); + if (resolved != null) { + return resolved; + } + // If only registered subtypes are allowed, nothing to probe for (all + // of them have been checked already), and must not load other classes + if ((ctxt instanceof DeserializationContext) + && ((DeserializationContext) ctxt).isEnabled( + DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED)) { + return id; + } + resolved = _findNestedTypeId(ctxt, _baseType, id, _subTypeValidator); + if (resolved == null) { + return id; + } + _resolvedIds.put(id, resolved); + return resolved; + } + /** * Method that tries to find class that given type id (name from Avro schema) - * refers to: first as-is, then considering that trailing segments may be - * names of nested classes (based on {@code SpecificData.getClass()} of Apache Avro). + * refers to: first as-is, then considering that dots may be separators + * between enclosing and nested class, starting with the last one + * (based on {@code SpecificData.getClass()} of Apache Avro). * Lookups use same mechanism (and so same ClassLoaders) as databind does, - * and are subject to the configured {@link PolymorphicTypeValidator}. + * and are subject to given {@link PolymorphicTypeValidator}. *

* Also handles canonical generic type ids {@code AvroReaderFactory} produces for * {@code EnumSet} and {@code EnumMap} (like {@code java.util.EnumSet}), * for which the first type parameter is name from schema. * - * @return Type resolved, if any; {@code null} if no class was found + * @return Type id with name of class found, if any (may be {@code id} itself); + * {@code null} if no class was found * * @since 2.24 */ - protected JavaType _probeForType(String id, DatabindContext ctxt) throws IOException + static String _findNestedTypeId(DatabindContext ctxt, JavaType baseType, String id, + PolymorphicTypeValidator ptv) + throws IOException { // Name to probe is either the whole id, or the first type parameter final int ltIndex = id.indexOf('<'); @@ -138,50 +162,39 @@ protected JavaType _probeForType(String id, DatabindContext ctxt) throws IOExcep return null; } } - // Only probe names that could be dotted nested class names; and for + // Only probe names that could be those of nested classes; and for // which variants are worth trying (no real class has name very long // or nested very deep) final String name = id.substring(start, end); - if ((name.indexOf('.') < 0) || (name.indexOf('$') >= 0) || (name.indexOf('<') >= 0) - || !_shouldProbe(name)) { + if ((name.indexOf('.') < 0) || (name.indexOf('<') >= 0) || !_shouldProbe(name)) { return null; } - Object cached = _probedTypes.get(id); - if (cached != null) { - return (cached == NO_CLASS) ? null : (JavaType) cached; - } - JavaType type = null; final StringBuilder sb = new StringBuilder(id); int lastDot = end; while (true) { - type = _findType(sb.toString(), ctxt, ltIndex >= 0); + final String candidate = sb.toString(); + JavaType type; + try { + // returns `null` if no class found... + type = ctxt.resolveAndValidateSubType(baseType, candidate, ptv); + } catch (IllegalArgumentException e) { + // ... except that for generic types failure to find class (of type + // parameter) is signaled with exception by `TypeFactory` + if (ltIndex < 0) { + throw e; + } + type = null; + } if (type != null) { - break; + return candidate; } // Class does not exist - perhaps last dot is actually a nested class lastDot = sb.lastIndexOf(".", lastDot); if (lastDot < start) { - break; + return null; } sb.setCharAt(lastDot, '$'); } - _probedTypes.put(id, (type == null) ? NO_CLASS : type); - return type; - } - - private JavaType _findType(String id, DatabindContext ctxt, boolean generic) throws IOException - { - if (!generic) { - // returns `null` if no class found - return ctxt.resolveAndValidateSubType(_baseType, id, _subTypeValidator); - } - // But for generic types failure to find class (of type parameter) is - // signaled with exception by `TypeFactory` - try { - return ctxt.resolveAndValidateSubType(_baseType, id, _subTypeValidator); - } catch (IllegalArgumentException e) { - return null; - } } // Whether to probe for nested classes for given (possibly untrusted) schema diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java index a03ae9c6b..7b4260487 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java @@ -321,11 +321,11 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) { // - Number wrappers (Integer, ...) final String typeId = AvroSchemaHelper.getTypeId(raw); - final String altTypeId = _dottedTypeId(typeId); + final String dottedTypeId = _dottedTypeId(typeId); for (int i = 0, size = types.size(); i < size; ++i) { Schema schema = types.get(i); // Exact schema match? - if (_matchesTypeId(typeId, altTypeId, AvroSchemaHelper.getTypeId(schema))) { + if (_matchesTypeId(typeId, dottedTypeId, AvroSchemaHelper.getTypeId(schema))) { return i; } } @@ -371,11 +371,11 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) { } final String typeId = AvroSchemaHelper.getTypeId(raw); - final String altTypeId = _dottedTypeId(typeId); + final String dottedTypeId = _dottedTypeId(typeId); for (int i = 0, size = types.size(); i < size; ++i) { Schema schema = types.get(i); // Exact schema match? - if (_matchesTypeId(typeId, altTypeId, AvroSchemaHelper.getTypeId(schema))) { + if (_matchesTypeId(typeId, dottedTypeId, AvroSchemaHelper.getTypeId(schema))) { return schema; } } @@ -418,16 +418,19 @@ private static int _resolveStringIndex(Schema unionSchema, List types, } // [dataformats-binary#908]: schemas (as of Avro 1.9) use dot as separator for - // nested classes too, whereas class names use dollar sign: so need to match - // class name against both variants. Done by comparing names only, without - // trying to load class named by schema. + // nested classes too, whereas class names use dollar sign (and schemas may + // use mix of the two): so if no exact match, compare with separators unified. + // Done by comparing names only, without trying to load class named by schema. private static String _dottedTypeId(String typeId) { - return (typeId.indexOf('$') < 0) ? null : typeId.replace('$', '.'); + return (typeId.indexOf('$') < 0) ? typeId : typeId.replace('$', '.'); } - private static boolean _matchesTypeId(String typeId, String altTypeId, String schemaTypeId) { + private static boolean _matchesTypeId(String typeId, String dottedTypeId, String schemaTypeId) { + if (schemaTypeId == null) { + return false; + } return typeId.equals(schemaTypeId) - || ((altTypeId != null) && altTypeId.equals(schemaTypeId)); + || dottedTypeId.equals(_dottedTypeId(schemaTypeId)); } private static Schema _findNotNull(List types) diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java new file mode 100644 index 000000000..2566d00b9 --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java @@ -0,0 +1,147 @@ +package com.fasterxml.jackson.dataformat.avro; + +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.avro.Schema; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.*; + +// Limits on probing for nested classes named by (possibly untrusted) schema: +// each dot of name means one more class loading attempt. Originally added +// in [dataformats-binary#906]; probing moved to `AvroTypeIdResolver` +// in [dataformats-binary#908] +public class NestedClassProbeLimits908Test extends AvroTestBase +{ + static class ObjectBean { public Object value; } + + // ClassLoader that counts attempts to load classes with given name prefix + static class CountingClassLoader extends ClassLoader { + private final String _prefix; + final AtomicInteger count = new AtomicInteger(); + + CountingClassLoader(ClassLoader parent, String prefix) { + super(parent); + _prefix = prefix; + } + + @Override + protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + if (name.startsWith(_prefix)) { + count.incrementAndGet(); + } + return super.loadClass(name, resolve); + } + } + + private final AvroMapper MAPPER = newMapper(); + + @Test + public void testDotCountLimitBoundary() throws Exception + { + final int max = AvroTypeIdResolver.MAX_NAMESPACE_DOTS; + assertEquals(32, max); + assertTrue(AvroTypeIdResolver._shouldProbe("Rec")); + assertTrue(AvroTypeIdResolver._shouldProbe("abc.Rec")); + assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max - 1) + ".Rec")); + assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max) + ".Rec")); + assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 1) + ".Rec")); + assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 100) + ".Rec")); + } + + // Limit applies to length of full name: namespace, dot, name + @Test + public void testNameLengthLimitBoundary() throws Exception + { + final int max = AvroTypeIdResolver.MAX_CLASS_NAME_LENGTH; + assertEquals(1024, max); + final String name = ".Rec"; + final int maxNamespace = max - name.length(); + assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace - 1) + name)); + assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace) + name)); + assertFalse(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace + 1) + name)); + assertFalse(AvroTypeIdResolver._shouldProbe(_longName(100_000) + name)); + } + + // Sanity check that probing is done (and counted) for names within limits: + // one attempt per dot, at least + @Test + public void testProbingWithinLimits() throws Exception + { + final int dots = 10; + int count = _readAndCountLoads("probe908x." + _dottedName(dots - 1), "probe908x."); + assertTrue(count >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count); + } + + @Test + @Timeout(value = 10, unit = TimeUnit.SECONDS) + public void testNoProbingForExcessivelyDottedName() throws Exception + { + int count = _readAndCountLoads("probe908y." + _dottedName(20_000), "probe908y."); + _assertNotProbed(count); + } + + @Test + public void testNoProbingForExcessivelyLongName() throws Exception + { + int count = _readAndCountLoads("probe908z.a.b.c." + + _longName(AvroTypeIdResolver.MAX_CLASS_NAME_LENGTH), "probe908z."); + _assertNotProbed(count); + } + + // Without probing only lookups of name as-is are done: type id gets resolved + // a few times for a value, but number does not depend on number of dots + private void _assertNotProbed(int count) { + assertTrue(count <= 5, "Expected at most 5 class loading attempts, got: "+count); + } + + // Reads `ObjectBean` with value of record type with given namespace + // (that of non-existing class): returns number of class loading attempts + // via context ClassLoader, for names with given prefix + private int _readAndCountLoads(String namespace, String prefix) throws Exception + { + Schema rec = Schema.createRecord("Rec", null, namespace, false, + Arrays.asList(new Schema.Field("b", Schema.create(Schema.Type.INT)))); + Schema root = Schema.createRecord("ObjectBean", null, "probe908", false, + Arrays.asList(new Schema.Field("value", rec))); + + final CountingClassLoader loader = new CountingClassLoader( + getClass().getClassLoader(), prefix); + final Thread thread = Thread.currentThread(); + final ClassLoader oldLoader = thread.getContextClassLoader(); + thread.setContextClassLoader(loader); + try { + // int 42 (zigzag: 84) + ObjectBean result = MAPPER.readerFor(ObjectBean.class) + .with(new AvroSchema(root)) + .readValue(new byte[] { 84 }); + assertTrue(result.value instanceof Map, + "Expected Map, got: "+result.value.getClass().getName()); + assertEquals(42, ((Map) result.value).get("b")); + } finally { + thread.setContextClassLoader(oldLoader); + } + return loader.count.get(); + } + + private static String _longName(int len) { + StringBuilder sb = new StringBuilder(len); + while (sb.length() < len) { + sb.append('x'); + } + return sb.toString(); + } + + // Name with given number of dots + private static String _dottedName(int dots) { + StringBuilder sb = new StringBuilder("a"); + for (int i = 0; i < dots; ++i) { + sb.append(".a"); + } + return sb.toString(); + } +} diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java index ff00eb2d1..47284a2f2 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java @@ -72,12 +72,6 @@ private URLClassLoader isolatedLoader() { NestedClassTypeId908Test.class.getClassLoader()); } - /* - /********************************************************************** - /* Test methods, ClassLoader handling - /********************************************************************** - */ - // Sanity check for test setup itself @Test public void testIsolatedClassesNotVisibleToModule() throws Exception @@ -111,7 +105,7 @@ public void testWriteResolvesUnionForNestedClass() throws Exception public void testRoundTripWithContextClassLoader() throws Exception { try (URLClassLoader loader = isolatedLoader()) { - Object result = _roundTrip(loader, loader); + Object result = _roundTrip(newMapper(), loader, loader); assertEquals(NS + "$ImplB", result.getClass().getName()); assertSame(loader, result.getClass().getClassLoader()); assertEquals(42, result.getClass().getField("b").get(result)); @@ -123,7 +117,7 @@ public void testRoundTripWithContextClassLoader() throws Exception public void testReadWithoutContextClassLoader() throws Exception { try (URLClassLoader loader = isolatedLoader()) { - Object result = _roundTrip(loader, null); + Object result = _roundTrip(newMapper(), loader, null); assertTrue(result instanceof Map, "Expected Map, got: "+result.getClass().getName()); assertEquals(42, ((Map) result).get("b")); } @@ -137,22 +131,39 @@ public void testResolutionIsNotSharedAcrossClassLoaders() throws Exception try (URLClassLoader loader1 = isolatedLoader(); URLClassLoader loader2 = isolatedLoader()) { // first: lookup that fails - assertTrue(_roundTrip(loader1, null) instanceof Map); + assertTrue(_roundTrip(newMapper(), loader1, null) instanceof Map); - Object result1 = _roundTrip(loader1, loader1); + Object result1 = _roundTrip(newMapper(), loader1, loader1); assertSame(loader1, result1.getClass().getClassLoader()); - Object result2 = _roundTrip(loader2, loader2); + Object result2 = _roundTrip(newMapper(), loader2, loader2); assertSame(loader2, result2.getClass().getClassLoader()); // and failing lookup still fails - assertTrue(_roundTrip(loader1, null) instanceof Map); + assertTrue(_roundTrip(newMapper(), loader1, null) instanceof Map); + } + } + + // Failure to find class (here: due to context ClassLoader not being set) must + // not be remembered by mapper: lookup may succeed later on + @Test + public void testFailedLookupNotRetainedByMapper() throws Exception + { + final AvroMapper mapper = newMapper(); + try (URLClassLoader loader = isolatedLoader()) { + assertTrue(_roundTrip(mapper, loader, null) instanceof Map); + assertTrue(_roundTrip(mapper, loader, null) instanceof Map); + + Object result = _roundTrip(mapper, loader, loader); + assertEquals(NS + "$ImplB", result.getClass().getName()); + assertSame(loader, result.getClass().getClassLoader()); } } - // Writes `Bean` with `ImplB` value, reads it back (with new mapper) using - // given context ClassLoader (if any); returns value of `Bean.value` - private Object _roundTrip(ClassLoader beanLoader, ClassLoader contextLoader) throws Exception + // Writes `Bean` with `ImplB` value, reads it back using given context + // ClassLoader (if any); returns value of `Bean.value` + private Object _roundTrip(AvroMapper mapper, + ClassLoader beanLoader, ClassLoader contextLoader) throws Exception { final Class beanClass = beanLoader.loadClass(NS + "$Bean"); final Object bean = beanClass.getDeclaredConstructor().newInstance(); @@ -160,7 +171,6 @@ private Object _roundTrip(ClassLoader beanLoader, ClassLoader contextLoader) thr implB.getClass().getField("b").set(implB, 42); beanClass.getField("value").set(bean, implB); - final AvroMapper mapper = newMapper(); final AvroSchema schema = mapper.schemaFrom(SCHEMA_JSON); final byte[] avro = mapper.writer(schema).writeValueAsBytes(bean); // union index 1 (zigzag: 2), then int 42 (zigzag: 84) @@ -177,54 +187,4 @@ private Object _roundTrip(ClassLoader beanLoader, ClassLoader contextLoader) thr thread.setContextClassLoader(oldLoader); } } - - /* - /********************************************************************** - /* Test methods, probing limits - /********************************************************************** - */ - - @Test - public void testDotCountLimitBoundary() throws Exception - { - final int max = AvroTypeIdResolver.MAX_NAMESPACE_DOTS; - assertEquals(32, max); - assertTrue(AvroTypeIdResolver._shouldProbe("Rec")); - assertTrue(AvroTypeIdResolver._shouldProbe("abc.Rec")); - assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max - 1) + ".Rec")); - assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max) + ".Rec")); - assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 1) + ".Rec")); - assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 100) + ".Rec")); - } - - // Limit applies to length of full name: namespace, dot, name - @Test - public void testNameLengthLimitBoundary() throws Exception - { - final int max = AvroTypeIdResolver.MAX_CLASS_NAME_LENGTH; - assertEquals(1024, max); - final String name = ".Rec"; - final int maxNamespace = max - name.length(); - assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace - 1) + name)); - assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace) + name)); - assertFalse(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace + 1) + name)); - assertFalse(AvroTypeIdResolver._shouldProbe(_longName(100_000) + name)); - } - - private static String _longName(int len) { - StringBuilder sb = new StringBuilder(len); - while (sb.length() < len) { - sb.append('x'); - } - return sb.toString(); - } - - // Name with given number of dots - private static String _dottedName(int dots) { - StringBuilder sb = new StringBuilder("a"); - for (int i = 0; i < dots; ++i) { - sb.append(".a"); - } - return sb.toString(); - } } diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java new file mode 100644 index 000000000..b34573b25 --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java @@ -0,0 +1,126 @@ +package com.fasterxml.jackson.dataformat.avro; + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.DeserializationFeature; + +import static org.junit.jupiter.api.Assertions.*; + +// [dataformats-binary#908]: resolution of type ids of nested classes, for which +// schemas use dot as separator between enclosing and nested class; for cases +// other than ClassLoader handling (see `NestedClassTypeId908Test` for that) +public class NestedClassTypeIdResolution908Test extends AvroTestBase +{ + @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS) + static abstract class Base { } + + static class ImplA extends Base { public String a; } + static class ImplB extends Base { public int b; } + + static class Bean { public Base value; } + + static class ObjectBean { public Object value; } + + static class Mid { + static class Leaf { public int b; } + } + + private final static String NS = NestedClassTypeIdResolution908Test.class.getName(); + + // Schema with union of 2 records, with given namespaces (if any) and names + private static String _schemaJson(String name1, String name2) { + return "{\"type\":\"record\",\"name\":\"Bean\",\"namespace\":\"" + NS + "\"," + +"\"fields\":[{\"name\":\"value\",\"type\":[" + +"{\"type\":\"record\",\"name\":\"" + name1 + "\",\"fields\":[{\"name\":\"a\",\"type\":\"string\"}]}," + +"{\"type\":\"record\",\"name\":\"" + name2 + "\",\"fields\":[{\"name\":\"b\",\"type\":\"int\"}]}" + +"]}]}"; + } + + // union index 1 (zigzag: 2), then int 42 (zigzag: 84) + private final static byte[] AVRO_SECOND_42 = new byte[] { 2, 84 }; + + private final AvroMapper MAPPER = newMapper(); + + // Standard (non-Avro) type id resolver must be able to use native type ids too + @Test + public void testClassNameTypeInfoWithNestedClasses() throws Exception + { + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", "ImplB")); + // (writing of type ids via `@JsonTypeInfo` not supported, so hand-encoded) + Bean result = MAPPER.readerFor(Bean.class).with(schema).readValue(AVRO_SECOND_42); + assertNotNull(result.value); + assertEquals(ImplB.class, result.value.getClass()); + assertEquals(42, ((ImplB) result.value).b); + } + + @Test + public void testUntypedWithNestedClasses() throws Exception + { + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", "ImplB")); + ObjectBean result = _roundTripUntyped(schema, _implB(42)); + assertEquals(ImplB.class, result.value.getClass()); + assertEquals(42, ((ImplB) result.value).b); + } + + // Probing for nested classes must not bypass check for registered subtypes + @Test + public void testUnregisteredSubtypeNotProbedFor() throws Exception + { + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", "ImplB")); + final byte[] avro = MAPPER.writer(schema).writeValueAsBytes(_objectBean(_implB(42))); + ObjectBean result = MAPPER.readerFor(ObjectBean.class) + .with(schema) + .with(DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED) + .readValue(avro); + // not resolved into class: bound as "untyped" + assertTrue(result.value instanceof Map, + "Expected Map, got: "+result.value.getClass().getName()); + assertEquals(42, ((Map) result.value).get("b")); + } + + // Names that use both dollar sign and dot as separators: either explicit + // namespace, or ones by Avro before 1.9 for deeper nesting + @Test + public void testMixedSeparatorsInName() throws Exception + { + // Namespace from `Bean`, `NS`; so: "NS.ImplA", "NS$Mid.Leaf" + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", NS + "$Mid.Leaf")); + Mid.Leaf leaf = new Mid.Leaf(); + leaf.b = 42; + ObjectBean result = _roundTripUntyped(schema, leaf); + assertEquals(Mid.Leaf.class, result.value.getClass()); + assertEquals(42, ((Mid.Leaf) result.value).b); + } + + // Legacy (Avro before 1.9) namespace with trailing dollar sign + @Test + public void testLegacyDollarNamespace() throws Exception + { + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson(NS + "$.ImplA", NS + "$.ImplB")); + ObjectBean result = _roundTripUntyped(schema, _implB(42)); + assertEquals(ImplB.class, result.value.getClass()); + assertEquals(42, ((ImplB) result.value).b); + } + + private ObjectBean _roundTripUntyped(AvroSchema schema, Object value) throws Exception + { + final byte[] avro = MAPPER.writer(schema).writeValueAsBytes(_objectBean(value)); + assertArrayEquals(AVRO_SECOND_42, avro); + return MAPPER.readerFor(ObjectBean.class).with(schema).readValue(avro); + } + + private static ObjectBean _objectBean(Object value) { + ObjectBean bean = new ObjectBean(); + bean.value = value; + return bean; + } + + private static ImplB _implB(int b) { + ImplB impl = new ImplB(); + impl.b = b; + return impl; + } +} From 71825cbba2b484207ecf3ce140fc0902d3bc09f1 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 17 Nov 2026 20:52:54 -0800 Subject: [PATCH 3/8] Move nested class probing to `AvroParser.getTypeId()` (#908) 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 --- .../jackson/dataformat/avro/AvroModule.java | 3 - .../avro/AvroNestedClassTypeIdHandler.java | 44 ----- .../jackson/dataformat/avro/AvroParser.java | 27 ++- .../dataformat/avro/AvroTypeIdClassNames.java | 144 ++++++++++++++++ .../dataformat/avro/AvroTypeIdResolver.java | 162 +----------------- .../avro/schema/AvroSchemaHelper.java | 2 +- .../avro/NestedClassProbeLimits908Test.java | 28 +-- .../NestedClassTypeIdResolution908Test.java | 51 +++++- release-notes/VERSION-2.x | 6 +- 9 files changed, 244 insertions(+), 223 deletions(-) delete mode 100644 avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java create mode 100644 avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java index b2e45a4eb..e546ecaa7 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroModule.java @@ -102,9 +102,6 @@ protected void _addDeserializers(SetupContext context) { SimpleDeserializers desers = new SimpleDeserializers(); desers.addDeserializer(Object.class, new AvroUntypedDeserializer()); context.addDeserializers(desers); - // 17-Nov-2026, tatu: [dataformats-binary#908] Type ids for nested classes - // need resolving for standard (non-Avro) type id resolvers too - context.addDeserializationProblemHandler(new AvroNestedClassTypeIdHandler()); } // since 2.9 diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java deleted file mode 100644 index d0aa5d063..000000000 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroNestedClassTypeIdHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.fasterxml.jackson.dataformat.avro; - -import java.io.IOException; - -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; -import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; -import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; - -/** - * Handler registered by {@link AvroModule} to let standard (non-Avro) type id - * resolvers -- like one used for {@code @JsonTypeInfo(use = Id.CLASS)} -- - * resolve native type ids of nested classes: Avro schemas use dot as the - * separator between enclosing and nested class ({@code package.Outer.Inner}) - * so such ids are not class names as-is. - * {@link AvroTypeIdResolver} handles this by itself. - * - * @since 2.24 - */ -class AvroNestedClassTypeIdHandler - extends DeserializationProblemHandler - implements java.io.Serializable -{ - private static final long serialVersionUID = 1L; - - @Override - public JavaType handleUnknownTypeId(DeserializationContext ctxt, - JavaType baseType, String subTypeId, TypeIdResolver idResolver, - String failureMsg) throws IOException - { - // Only for native type ids from Avro content; and not if probed already - if ((idResolver instanceof AvroTypeIdResolver) - || !(ctxt.getParser() instanceof AvroParser)) { - return null; - } - final PolymorphicTypeValidator ptv = ctxt.getConfig().getPolymorphicTypeValidator(); - final String resolved = AvroTypeIdResolver._findNestedTypeId(ctxt, baseType, subTypeId, ptv); - if ((resolved == null) || resolved.equals(subTypeId)) { - return null; - } - return ctxt.resolveAndValidateSubType(baseType, resolved, ptv); - } -} diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java index aa0158196..255583815 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java @@ -94,6 +94,13 @@ public static int collectDefaults() protected AvroReadContext _avroContext; + /** + * Native type ids (from schema) seen, mapped to ids exposed by {@link #getTypeId()}. + * + * @since 2.24 + */ + private final java.util.Map _resolvedTypeIds = new java.util.HashMap<>(); + /* /********************************************************************** /* Life-cycle @@ -239,7 +246,25 @@ public boolean canReadTypeId() { @Override public Object getTypeId() throws IOException { - return _avroContext != null ? _avroContext.getTypeId() : null; + if (_avroContext == null) { + return null; + } + final String typeId = _avroContext.getTypeId(); + if (typeId == null) { + return null; + } + // [dataformats-binary#908]: names from schema use dot as separator for + // nested classes too: need to find the actual class name. Outcome (found + // or not) only retained here, for duration of parsing. + String resolved = _resolvedTypeIds.get(typeId); + if (resolved == null) { + resolved = AvroTypeIdClassNames.resolve(typeId); + if (resolved == null) { + resolved = typeId; + } + _resolvedTypeIds.put(typeId, resolved); + } + return resolved; } /* diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java new file mode 100644 index 000000000..d9c920768 --- /dev/null +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java @@ -0,0 +1,144 @@ +package com.fasterxml.jackson.dataformat.avro; + +import com.fasterxml.jackson.databind.util.LRUMap; + +/** + * Helper class for translating native type ids into names of Java classes: + * needed since names of record, enum and fixed schemas (as of Avro 1.9) use + * dot as the separator between enclosing and nested class + * ({@code package.Outer.Inner}), so need to probe to find the actual class + * ({@code package.Outer$Inner}). + *

+ * Probing is done (as per [dataformats-binary#908]) the way databind looks for + * classes: first using context ClassLoader (if any), then ClassLoader of this + * module; in both cases without initializing class found (name comes from + * possibly untrusted schema). + * + * @since 2.24 + */ +final class AvroTypeIdClassNames +{ + // Maximum number of dots in namespace for which nested-class probing is done + final static int MAX_NAMESPACE_DOTS = 32; + + // Maximum length of full name (namespace, dot, name) for which nested-class + // probing is done: well above length of any real class name + final static int MAX_CLASS_NAME_LENGTH = 1024; + + /** + * Type ids for which class was found, mapped to type id with name of that class. + * Only names are retained (no references to classes), and only for successful + * lookups: whether class is found may depend on context ClassLoader, so failure + * must not be remembered beyond a single parser. + */ + private final static LRUMap RESOLVED = new LRUMap<>(80, 800); + + private AvroTypeIdClassNames() { } + + /** + * @param typeId Type id from schema: class name (possibly using dots for nested + * classes), or canonical generic type {@code AvroReaderFactory} produces for + * {@code EnumSet} and {@code EnumMap} (like {@code java.util.EnumSet}), + * for which the first type parameter is name from schema. + * + * @return Type id with name of class found, if any (may be {@code typeId} itself); + * {@code null} if no class was found + */ + static String resolve(String typeId) + { + // Name to probe is either the whole id, or the first type parameter + final int ltIndex = typeId.indexOf('<'); + final int start, end; + if (ltIndex < 0) { + start = 0; + end = typeId.length(); + } else { + start = ltIndex + 1; + int ix = typeId.indexOf(',', start); + end = (ix < 0) ? typeId.indexOf('>', start) : ix; + if (end < 0) { // malformed, leave for databind to report + return null; + } + } + // Only probe names that could be those of nested classes; and for + // which variants are worth trying (no real class has name very long + // or nested very deep) + final String name = typeId.substring(start, end); + if ((name.indexOf('.') < 0) || (name.indexOf('<') >= 0) || !_shouldProbe(name)) { + return null; + } + String resolved = RESOLVED.get(typeId); + if (resolved != null) { + return resolved; + } + final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader moduleLoader = AvroTypeIdClassNames.class.getClassLoader(); + + // 23-Aug-2024:[dataformats-binary#167] Based on SpecificData::getClass + // from Apache Avro. Initially assume that namespace is a Java package + final StringBuilder sb = new StringBuilder(name); + int lastDot = sb.length(); + while (true) { + if (_classExists(sb.toString(), contextLoader, moduleLoader)) { + break; + } + // Class does not exist - perhaps last dot is actually a nested class + lastDot = sb.lastIndexOf(".", lastDot); + if (lastDot < 0) { + return null; + } + sb.setCharAt(lastDot, '$'); + } + resolved = (ltIndex < 0) ? sb.toString() + : typeId.substring(0, start) + sb + typeId.substring(end); + // avoid retaining separate copy of the common case + if (resolved.equals(typeId)) { + resolved = typeId; + } + RESOLVED.put(typeId, resolved); + return resolved; + } + + // NOTE: `LinkageError`s (including `NoClassDefFoundError` for class that exists + // but cannot be loaded, e.g. due to missing superclass) are intentionally NOT + // caught: class may well exist, and fallback name would mask the actual problem + private static boolean _classExists(String className, + ClassLoader contextLoader, ClassLoader moduleLoader) + { + // Only need to know class exists, so must not initialize it + if (contextLoader != null) { + try { + Class.forName(className, false, contextLoader); + return true; + } catch (ClassNotFoundException e) { } + if (contextLoader == moduleLoader) { + return false; + } + } + try { + Class.forName(className, false, moduleLoader); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + + // Whether to probe for nested classes for given (possibly untrusted) schema + // name: each dot means one more class loading attempt, so limit both number + // of dots and length. + // Package-visible for tests + static boolean _shouldProbe(String className) { + if (className.length() > MAX_CLASS_NAME_LENGTH) { + return false; + } + // One more dot than in namespace: separator between namespace and name + final int maxDots = MAX_NAMESPACE_DOTS + 1; + int count = 0; + for (int ix = className.indexOf('.'); ix >= 0; ix = className.indexOf('.', ix + 1)) { + if (++count > maxDots) { + return false; + } + } + return true; + } +} diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java index e558aa983..2dac4c936 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdResolver.java @@ -4,24 +4,16 @@ import java.util.*; import com.fasterxml.jackson.databind.DatabindContext; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; import com.fasterxml.jackson.databind.jsontype.NamedType; import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; import com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver; import com.fasterxml.jackson.databind.type.TypeFactory; -import com.fasterxml.jackson.databind.util.LRUMap; /** * {@link com.fasterxml.jackson.databind.jsontype.TypeIdResolver} for Avro type IDs embedded in schemas. Avro generally uses class names, * but we want to also support named subtypes so that developers can easily remap the embedded type IDs to a different runtime class. - *

- * Since 2.24 also handles resolution of names of nested classes: schemas (as of Avro 1.9) use dot as - * the separator between enclosing and nested class (package.Outer.Inner) so resolver needs to probe - * to find the actual class (package.Outer$Inner). This is done using the same class lookup as - * databind uses for loading the class (see [dataformats-binary#908]). * * @since 2.9 */ @@ -29,26 +21,11 @@ public class AvroTypeIdResolver extends ClassNameIdResolver { private static final long serialVersionUID = 1L; - // Maximum number of dots in namespace for which nested-class probing is done - // (moved from {@code AvroSchemaHelper} in 2.24) - final static int MAX_NAMESPACE_DOTS = 32; - - // Maximum length of full name (namespace, dot, name) for which nested-class - // probing is done: well above length of any real class name - final static int MAX_CLASS_NAME_LENGTH = 1024; - private final Map> _idTypes = new HashMap<>(); - /** - * Type ids for which probing found a class, mapped to the id of that class: - * for example {@code package.Outer.Inner} to {@code package.Outer$Inner}. - * Only successful lookups are retained: whether class is found may depend on - * context ClassLoader at the time, so failure must not be remembered. - * Per-resolver (and thereby per-mapper), unlike JVM-wide cache used before 2.24. - * - * @since 2.24 - */ - private final LRUMap _resolvedIds = new LRUMap<>(16, 200); + // 04-Jan-2021, tatu: Not sure why this is being kept around, not used as of + // 2.12; commenting out may be removed from 2.13 +// private final Map, String> _typeIds = new HashMap<>(); public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, PolymorphicTypeValidator ptv, @@ -58,14 +35,7 @@ public AvroTypeIdResolver(JavaType baseType, TypeFactory typeFactory, if (subTypes != null) { for (NamedType namedType : subTypes) { //registerSubtype(namedType.getType(), namedType.getName()); - final String name = namedType.getName(); - _idTypes.put(name, namedType.getType()); - // [dataformats-binary#908]: type ids from schema use dots for nested classes - // too: add aliases so that registered subtypes are found without probing - // (but never overriding exact matches) - if ((name != null) && name.indexOf('$') >= 0) { - _idTypes.putIfAbsent(name.replace('$', '.'), namedType.getType()); - } + _idTypes.put(namedType.getName(), namedType.getType()); } } } @@ -86,14 +56,10 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti } // check if there's a specific type we should be using for this ID Class subType = _idTypes.get(id); + if (subType != null) { + id = _idFrom(null, subType, _typeFactory); + } try { - if (subType != null) { - id = _idFrom(null, subType, _typeFactory); - } else { - // [dataformats-binary#908]: may need to probe for nested classes - id = _resolveNestedTypeId(id, ctxt); - } - // NOTE: always resolve via super class, for checks it has return super._typeFromId(id, ctxt); } catch (InvalidTypeIdException | IllegalArgumentException e) { // AvroTypeDeserializer expects null if we can't map the type ID to a class; It will throw an appropriate error if we can't @@ -101,118 +67,4 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti return null; } } - - /** - * @return Id of the class that given type id refers to, if one found by probing - * for nested classes; {@code id} itself otherwise - */ - private String _resolveNestedTypeId(String id, DatabindContext ctxt) throws IOException - { - String resolved = _resolvedIds.get(id); - if (resolved != null) { - return resolved; - } - // If only registered subtypes are allowed, nothing to probe for (all - // of them have been checked already), and must not load other classes - if ((ctxt instanceof DeserializationContext) - && ((DeserializationContext) ctxt).isEnabled( - DeserializationFeature.FAIL_ON_SUBTYPE_CLASS_NOT_REGISTERED)) { - return id; - } - resolved = _findNestedTypeId(ctxt, _baseType, id, _subTypeValidator); - if (resolved == null) { - return id; - } - _resolvedIds.put(id, resolved); - return resolved; - } - - /** - * Method that tries to find class that given type id (name from Avro schema) - * refers to: first as-is, then considering that dots may be separators - * between enclosing and nested class, starting with the last one - * (based on {@code SpecificData.getClass()} of Apache Avro). - * Lookups use same mechanism (and so same ClassLoaders) as databind does, - * and are subject to given {@link PolymorphicTypeValidator}. - *

- * Also handles canonical generic type ids {@code AvroReaderFactory} produces for - * {@code EnumSet} and {@code EnumMap} (like {@code java.util.EnumSet}), - * for which the first type parameter is name from schema. - * - * @return Type id with name of class found, if any (may be {@code id} itself); - * {@code null} if no class was found - * - * @since 2.24 - */ - static String _findNestedTypeId(DatabindContext ctxt, JavaType baseType, String id, - PolymorphicTypeValidator ptv) - throws IOException - { - // Name to probe is either the whole id, or the first type parameter - final int ltIndex = id.indexOf('<'); - final int start, end; - if (ltIndex < 0) { - start = 0; - end = id.length(); - } else { - start = ltIndex + 1; - int ix = id.indexOf(',', start); - end = (ix < 0) ? id.indexOf('>', start) : ix; - if (end < 0) { // malformed, leave for databind to report - return null; - } - } - // Only probe names that could be those of nested classes; and for - // which variants are worth trying (no real class has name very long - // or nested very deep) - final String name = id.substring(start, end); - if ((name.indexOf('.') < 0) || (name.indexOf('<') >= 0) || !_shouldProbe(name)) { - return null; - } - final StringBuilder sb = new StringBuilder(id); - int lastDot = end; - while (true) { - final String candidate = sb.toString(); - JavaType type; - try { - // returns `null` if no class found... - type = ctxt.resolveAndValidateSubType(baseType, candidate, ptv); - } catch (IllegalArgumentException e) { - // ... except that for generic types failure to find class (of type - // parameter) is signaled with exception by `TypeFactory` - if (ltIndex < 0) { - throw e; - } - type = null; - } - if (type != null) { - return candidate; - } - // Class does not exist - perhaps last dot is actually a nested class - lastDot = sb.lastIndexOf(".", lastDot); - if (lastDot < start) { - return null; - } - sb.setCharAt(lastDot, '$'); - } - } - - // Whether to probe for nested classes for given (possibly untrusted) schema - // name: each dot means one more class loading attempt, so limit both number - // of dots and length. - // Package-visible for tests - static boolean _shouldProbe(String className) { - if (className.length() > MAX_CLASS_NAME_LENGTH) { - return false; - } - // One more dot than in namespace: separator between namespace and name - final int maxDots = MAX_NAMESPACE_DOTS + 1; - int count = 0; - for (int ix = className.indexOf('.'); ix >= 0; ix = className.indexOf('.', ix + 1)) { - if (++count > maxDots) { - return false; - } - } - return true; - } } diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java index 072cab2d1..854b3db6d 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java @@ -362,7 +362,7 @@ public static String getTypeId(Schema schema) { * this method no longer probes for existence of classes to figure out * whether a namespace refers to a package or an enclosing class * (see [dataformats-binary#908]). Resolution of name into an actual class - * is done by {@code AvroTypeIdResolver}, when (and only if) class is needed. + * is done by {@code AvroParser.getTypeId()}, when (and only if) type id is needed. */ public static String getFullName(Schema schema) { final Schema.Type type = schema.getType(); diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java index 2566d00b9..099595b1a 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java @@ -13,7 +13,7 @@ // Limits on probing for nested classes named by (possibly untrusted) schema: // each dot of name means one more class loading attempt. Originally added -// in [dataformats-binary#906]; probing moved to `AvroTypeIdResolver` +// in [dataformats-binary#906]; probing moved to `AvroParser.getTypeId()` // in [dataformats-binary#908] public class NestedClassProbeLimits908Test extends AvroTestBase { @@ -43,28 +43,28 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE @Test public void testDotCountLimitBoundary() throws Exception { - final int max = AvroTypeIdResolver.MAX_NAMESPACE_DOTS; + final int max = AvroTypeIdClassNames.MAX_NAMESPACE_DOTS; assertEquals(32, max); - assertTrue(AvroTypeIdResolver._shouldProbe("Rec")); - assertTrue(AvroTypeIdResolver._shouldProbe("abc.Rec")); - assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max - 1) + ".Rec")); - assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(max) + ".Rec")); - assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 1) + ".Rec")); - assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(max + 100) + ".Rec")); + assertTrue(AvroTypeIdClassNames._shouldProbe("Rec")); + assertTrue(AvroTypeIdClassNames._shouldProbe("abc.Rec")); + assertTrue(AvroTypeIdClassNames._shouldProbe(_dottedName(max - 1) + ".Rec")); + assertTrue(AvroTypeIdClassNames._shouldProbe(_dottedName(max) + ".Rec")); + assertFalse(AvroTypeIdClassNames._shouldProbe(_dottedName(max + 1) + ".Rec")); + assertFalse(AvroTypeIdClassNames._shouldProbe(_dottedName(max + 100) + ".Rec")); } // Limit applies to length of full name: namespace, dot, name @Test public void testNameLengthLimitBoundary() throws Exception { - final int max = AvroTypeIdResolver.MAX_CLASS_NAME_LENGTH; + final int max = AvroTypeIdClassNames.MAX_CLASS_NAME_LENGTH; assertEquals(1024, max); final String name = ".Rec"; final int maxNamespace = max - name.length(); - assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace - 1) + name)); - assertTrue(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace) + name)); - assertFalse(AvroTypeIdResolver._shouldProbe(_longName(maxNamespace + 1) + name)); - assertFalse(AvroTypeIdResolver._shouldProbe(_longName(100_000) + name)); + assertTrue(AvroTypeIdClassNames._shouldProbe(_longName(maxNamespace - 1) + name)); + assertTrue(AvroTypeIdClassNames._shouldProbe(_longName(maxNamespace) + name)); + assertFalse(AvroTypeIdClassNames._shouldProbe(_longName(maxNamespace + 1) + name)); + assertFalse(AvroTypeIdClassNames._shouldProbe(_longName(100_000) + name)); } // Sanity check that probing is done (and counted) for names within limits: @@ -89,7 +89,7 @@ public void testNoProbingForExcessivelyDottedName() throws Exception public void testNoProbingForExcessivelyLongName() throws Exception { int count = _readAndCountLoads("probe908z.a.b.c." - + _longName(AvroTypeIdResolver.MAX_CLASS_NAME_LENGTH), "probe908z."); + + _longName(AvroTypeIdClassNames.MAX_CLASS_NAME_LENGTH), "probe908z."); _assertNotProbed(count); } diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java index b34573b25..610b1ccf4 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.DeserializationFeature; import static org.junit.jupiter.api.Assertions.*; @@ -28,6 +30,19 @@ static class Mid { static class Leaf { public int b; } } + static class InitTracker { + static volatile boolean initialized; + } + + // Never referenced by test code other than by name + static class NotToBeInitialized { + public int b; + + static { + InitTracker.initialized = true; + } + } + private final static String NS = NestedClassTypeIdResolution908Test.class.getName(); // Schema with union of 2 records, with given namespaces (if any) and names @@ -44,6 +59,38 @@ private static String _schemaJson(String name1, String name2) { private final AvroMapper MAPPER = newMapper(); + // Type id exposed by parser is name of the actual (nested) class; found + // without initializing the class (name comes from possibly untrusted schema) + @Test + public void testStreamingTypeIdForNestedClass() throws Exception + { + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", "NotToBeInitialized")); + try (JsonParser p = MAPPER.getFactory().createParser(AVRO_SECOND_42)) { + p.setSchema(schema); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertEquals(NS + "$NotToBeInitialized", p.getTypeId()); + // and same if asked again + assertEquals(NS + "$NotToBeInitialized", p.getTypeId()); + } + assertFalse(InitTracker.initialized); + } + + // No class to find: name as schema has it + @Test + public void testStreamingTypeIdForUnknownClass() throws Exception + { + final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", "NoSuchClass")); + try (JsonParser p = MAPPER.getFactory().createParser(AVRO_SECOND_42)) { + p.setSchema(schema); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertEquals(NS + ".NoSuchClass", p.getTypeId()); + } + } + // Standard (non-Avro) type id resolver must be able to use native type ids too @Test public void testClassNameTypeInfoWithNestedClasses() throws Exception @@ -65,9 +112,9 @@ public void testUntypedWithNestedClasses() throws Exception assertEquals(42, ((ImplB) result.value).b); } - // Probing for nested classes must not bypass check for registered subtypes + // Check for registered subtypes must apply to nested classes too @Test - public void testUnregisteredSubtypeNotProbedFor() throws Exception + public void testUnregisteredSubtypeNotAllowed() throws Exception { final AvroSchema schema = MAPPER.schemaFrom(_schemaJson("ImplA", "ImplB")); final byte[] avro = MAPPER.writer(schema).writeValueAsBytes(_objectBean(_implB(42))); diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 968a67e5b..454e0d4b4 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -18,9 +18,9 @@ Active maintainers: #908: (avro) `AvroSchemaHelper.getFullName()` probes for nested classes with different ClassLoader than databind uses to load them - (NOTE: schema names are no longer probed for; type ids exposed by `AvroParser.getTypeId()` - for nested classes now use `.` instead of `$` as separator, resolved into - classes by `AvroTypeIdResolver`) + (NOTE: `AvroSchemaHelper.getFullName()` no longer probes for classes, returns name + as schema has it; probing now done by `AvroParser.getTypeId()`, using context + ClassLoader first) 2.23.1 (not yet released) From 126f37ad8a2c5032ebb00ff3436dfcfcbdbf3f28 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 17 Nov 2026 20:54:48 -0800 Subject: [PATCH 4/8] Simplify nested class name resolution for #908 - `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 --- .../jackson/dataformat/avro/AvroParser.java | 15 +-- .../dataformat/avro/AvroTypeIdClassNames.java | 96 +++++++++---------- .../dataformat/avro/ser/AvroWriteContext.java | 28 ++---- 3 files changed, 54 insertions(+), 85 deletions(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java index 255583815..fcad0ea14 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java @@ -246,25 +246,14 @@ public boolean canReadTypeId() { @Override public Object getTypeId() throws IOException { - if (_avroContext == null) { - return null; - } - final String typeId = _avroContext.getTypeId(); + final String typeId = (_avroContext == null) ? null : _avroContext.getTypeId(); if (typeId == null) { return null; } // [dataformats-binary#908]: names from schema use dot as separator for // nested classes too: need to find the actual class name. Outcome (found // or not) only retained here, for duration of parsing. - String resolved = _resolvedTypeIds.get(typeId); - if (resolved == null) { - resolved = AvroTypeIdClassNames.resolve(typeId); - if (resolved == null) { - resolved = typeId; - } - _resolvedTypeIds.put(typeId, resolved); - } - return resolved; + return _resolvedTypeIds.computeIfAbsent(typeId, AvroTypeIdClassNames::resolve); } /* diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java index d9c920768..936bbdaaf 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java @@ -26,12 +26,12 @@ final class AvroTypeIdClassNames final static int MAX_CLASS_NAME_LENGTH = 1024; /** - * Type ids for which class was found, mapped to type id with name of that class. + * Names from schema for which class was found, mapped to name of that class. * Only names are retained (no references to classes), and only for successful * lookups: whether class is found may depend on context ClassLoader, so failure * must not be remembered beyond a single parser. */ - private final static LRUMap RESOLVED = new LRUMap<>(80, 800); + private final static LRUMap CLASS_NAMES = new LRUMap<>(80, 800); private AvroTypeIdClassNames() { } @@ -41,82 +41,72 @@ private AvroTypeIdClassNames() { } * {@code EnumSet} and {@code EnumMap} (like {@code java.util.EnumSet}), * for which the first type parameter is name from schema. * - * @return Type id with name of class found, if any (may be {@code typeId} itself); - * {@code null} if no class was found + * @return Type id with name of class found, if any; {@code typeId} itself otherwise */ static String resolve(String typeId) { - // Name to probe is either the whole id, or the first type parameter - final int ltIndex = typeId.indexOf('<'); - final int start, end; - if (ltIndex < 0) { - start = 0; - end = typeId.length(); - } else { - start = ltIndex + 1; - int ix = typeId.indexOf(',', start); - end = (ix < 0) ? typeId.indexOf('>', start) : ix; + final int start = typeId.indexOf('<') + 1; + if (start == 0) { + return _resolveClassName(typeId); + } + int end = typeId.indexOf(',', start); + if (end < 0) { + end = typeId.indexOf('>', start); if (end < 0) { // malformed, leave for databind to report - return null; + return typeId; } } - // Only probe names that could be those of nested classes; and for - // which variants are worth trying (no real class has name very long - // or nested very deep) - final String name = typeId.substring(start, end); - if ((name.indexOf('.') < 0) || (name.indexOf('<') >= 0) || !_shouldProbe(name)) { - return null; + return typeId.substring(0, start) + + _resolveClassName(typeId.substring(start, end)) + + typeId.substring(end); + } + + private static String _resolveClassName(String name) + { + // Nothing to probe without namespace; and no real class has name + // very long or nested very deep + if ((name.indexOf('.') < 0) || !_shouldProbe(name)) { + return name; } - String resolved = RESOLVED.get(typeId); - if (resolved != null) { - return resolved; + String className = CLASS_NAMES.get(name); + if (className != null) { + return className; } - final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); - final ClassLoader moduleLoader = AvroTypeIdClassNames.class.getClassLoader(); - // 23-Aug-2024:[dataformats-binary#167] Based on SpecificData::getClass // from Apache Avro. Initially assume that namespace is a Java package final StringBuilder sb = new StringBuilder(name); int lastDot = sb.length(); - while (true) { - if (_classExists(sb.toString(), contextLoader, moduleLoader)) { - break; - } + while (!_classExists(sb.toString())) { // Class does not exist - perhaps last dot is actually a nested class lastDot = sb.lastIndexOf(".", lastDot); if (lastDot < 0) { - return null; + return name; } sb.setCharAt(lastDot, '$'); } - resolved = (ltIndex < 0) ? sb.toString() - : typeId.substring(0, start) + sb + typeId.substring(end); - // avoid retaining separate copy of the common case - if (resolved.equals(typeId)) { - resolved = typeId; - } - RESOLVED.put(typeId, resolved); - return resolved; + className = sb.toString(); + CLASS_NAMES.put(name, className); + return className; + } + + // Same lookup order as databind has: context ClassLoader (if any) first + private static boolean _classExists(String className) + { + final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader moduleLoader = AvroTypeIdClassNames.class.getClassLoader(); + return ((contextLoader != null) && (contextLoader != moduleLoader) + && _classExists(className, contextLoader)) + || _classExists(className, moduleLoader); } // NOTE: `LinkageError`s (including `NoClassDefFoundError` for class that exists // but cannot be loaded, e.g. due to missing superclass) are intentionally NOT // caught: class may well exist, and fallback name would mask the actual problem - private static boolean _classExists(String className, - ClassLoader contextLoader, ClassLoader moduleLoader) + private static boolean _classExists(String className, ClassLoader loader) { - // Only need to know class exists, so must not initialize it - if (contextLoader != null) { - try { - Class.forName(className, false, contextLoader); - return true; - } catch (ClassNotFoundException e) { } - if (contextLoader == moduleLoader) { - return false; - } - } try { - Class.forName(className, false, moduleLoader); + // Only need to know class exists, so must not initialize it + Class.forName(className, false, loader); return true; } catch (ClassNotFoundException e) { return false; diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java index 7b4260487..72bb75f9a 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java @@ -320,12 +320,11 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) { // - ByteBuffer // - Number wrappers (Integer, ...) - final String typeId = AvroSchemaHelper.getTypeId(raw); - final String dottedTypeId = _dottedTypeId(typeId); + final String typeId = _dottedTypeId(AvroSchemaHelper.getTypeId(raw)); for (int i = 0, size = types.size(); i < size; ++i) { Schema schema = types.get(i); - // Exact schema match? - if (_matchesTypeId(typeId, dottedTypeId, AvroSchemaHelper.getTypeId(schema))) { + // Schema for the class? + if (typeId.equals(_dottedTypeId(AvroSchemaHelper.getTypeId(schema)))) { return i; } } @@ -370,12 +369,11 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) { return types.get(_resolveMapIndex(unionSchema, types, datum)); } - final String typeId = AvroSchemaHelper.getTypeId(raw); - final String dottedTypeId = _dottedTypeId(typeId); + final String typeId = _dottedTypeId(AvroSchemaHelper.getTypeId(raw)); for (int i = 0, size = types.size(); i < size; ++i) { Schema schema = types.get(i); - // Exact schema match? - if (_matchesTypeId(typeId, dottedTypeId, AvroSchemaHelper.getTypeId(schema))) { + // Schema for the class? + if (typeId.equals(_dottedTypeId(AvroSchemaHelper.getTypeId(schema)))) { return schema; } } @@ -419,18 +417,10 @@ private static int _resolveStringIndex(Schema unionSchema, List types, // [dataformats-binary#908]: schemas (as of Avro 1.9) use dot as separator for // nested classes too, whereas class names use dollar sign (and schemas may - // use mix of the two): so if no exact match, compare with separators unified. - // Done by comparing names only, without trying to load class named by schema. + // use mix of the two): so compare names with separators unified. + // No attempt made to load class named by schema. private static String _dottedTypeId(String typeId) { - return (typeId.indexOf('$') < 0) ? typeId : typeId.replace('$', '.'); - } - - private static boolean _matchesTypeId(String typeId, String dottedTypeId, String schemaTypeId) { - if (schemaTypeId == null) { - return false; - } - return typeId.equals(schemaTypeId) - || dottedTypeId.equals(_dottedTypeId(schemaTypeId)); + return (typeId == null) ? null : typeId.replace('$', '.'); } private static Schema _findNotNull(List types) From ba0007298e7e555147b216774d5ee8bcc1536f7d Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Tue, 17 Nov 2026 20:58:24 -0800 Subject: [PATCH 5/8] Cache resolved type ids in `AvroSchema` instead of parser/static cache (#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 --- .../jackson/dataformat/avro/AvroParser.java | 19 ++++------ .../jackson/dataformat/avro/AvroSchema.java | 35 ++++++++++++++++++ .../dataformat/avro/AvroTypeIdClassNames.java | 37 ++++++------------- .../avro/NestedClassTypeId908Test.java | 20 ++++++---- 4 files changed, 66 insertions(+), 45 deletions(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java index fcad0ea14..d317668a2 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroParser.java @@ -94,13 +94,6 @@ public static int collectDefaults() protected AvroReadContext _avroContext; - /** - * Native type ids (from schema) seen, mapped to ids exposed by {@link #getTypeId()}. - * - * @since 2.24 - */ - private final java.util.Map _resolvedTypeIds = new java.util.HashMap<>(); - /* /********************************************************************** /* Life-cycle @@ -231,6 +224,9 @@ public void setSchema(FormatSchema schema) } catch (IOException e) { throw new IllegalArgumentException(e); } + // 17-Nov-2026, tatu: was never assigned before 2.24; needed for + // resolving type ids [dataformats-binary#908] + _rootSchema = (AvroSchema) schema; } else { throw new IllegalArgumentException("Can not use FormatSchema of type " +schema.getClass().getName()); @@ -247,13 +243,12 @@ public boolean canReadTypeId() { @Override public Object getTypeId() throws IOException { final String typeId = (_avroContext == null) ? null : _avroContext.getTypeId(); - if (typeId == null) { - return null; + if ((typeId == null) || (_rootSchema == null)) { + return typeId; } // [dataformats-binary#908]: names from schema use dot as separator for - // nested classes too: need to find the actual class name. Outcome (found - // or not) only retained here, for duration of parsing. - return _resolvedTypeIds.computeIfAbsent(typeId, AvroTypeIdClassNames::resolve); + // nested classes too: need to find name of the actual class + return _rootSchema.resolveTypeId(typeId); } /* diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java index 76bf01fe2..011edfe3e 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; import org.apache.avro.Schema; @@ -37,6 +38,15 @@ public class AvroSchema implements FormatSchema */ protected final AtomicReference _reader = new AtomicReference<>(); + /** + * Type ids (as used by {@link AvroStructureReader}s of this schema) for which + * Java class was found, mapped to type id with name of that class: see + * {@link #resolveTypeId}. Bounded by number of types schema has. + * + * @since 2.24 + */ + private final ConcurrentHashMap _resolvedTypeIds = new ConcurrentHashMap<>(); + public AvroSchema(Schema asch) { _writerSchema = asch; @@ -159,6 +169,31 @@ public String getSchemaType() { /********************************************************************** */ + /** + * Method for translating type id from this schema into one with name of the + * actual Java class: names of record, enum and fixed schemas use dot as the + * separator between enclosing and nested class ({@code package.Outer.Inner}) + * so need to probe to find the class ({@code package.Outer$Inner}). + * Only successful lookups are retained: whether class is found may depend on + * context ClassLoader, so failure must not be remembered. + * + * @return Type id with name of class found, if any; {@code typeId} itself otherwise + * + * @since 2.24 + */ + String resolveTypeId(String typeId) + { + String resolved = _resolvedTypeIds.get(typeId); + if (resolved == null) { + resolved = AvroTypeIdClassNames.resolve(typeId); + if (resolved == null) { + return typeId; + } + _resolvedTypeIds.put(typeId, resolved); + } + return resolved; + } + public AvroStructureReader getReader() throws IOException { AvroStructureReader r = _reader.get(); diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java index 936bbdaaf..761ef4c7f 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java @@ -1,7 +1,5 @@ package com.fasterxml.jackson.dataformat.avro; -import com.fasterxml.jackson.databind.util.LRUMap; - /** * Helper class for translating native type ids into names of Java classes: * needed since names of record, enum and fixed schemas (as of Avro 1.9) use @@ -25,14 +23,6 @@ final class AvroTypeIdClassNames // probing is done: well above length of any real class name final static int MAX_CLASS_NAME_LENGTH = 1024; - /** - * Names from schema for which class was found, mapped to name of that class. - * Only names are retained (no references to classes), and only for successful - * lookups: whether class is found may depend on context ClassLoader, so failure - * must not be remembered beyond a single parser. - */ - private final static LRUMap CLASS_NAMES = new LRUMap<>(80, 800); - private AvroTypeIdClassNames() { } /** @@ -41,36 +31,33 @@ private AvroTypeIdClassNames() { } * {@code EnumSet} and {@code EnumMap} (like {@code java.util.EnumSet}), * for which the first type parameter is name from schema. * - * @return Type id with name of class found, if any; {@code typeId} itself otherwise + * @return Type id with name of class found, if any (may be {@code typeId} itself); + * {@code null} if no class was found */ static String resolve(String typeId) { final int start = typeId.indexOf('<') + 1; if (start == 0) { - return _resolveClassName(typeId); + return _findClassName(typeId); } int end = typeId.indexOf(',', start); if (end < 0) { end = typeId.indexOf('>', start); if (end < 0) { // malformed, leave for databind to report - return typeId; + return null; } } - return typeId.substring(0, start) - + _resolveClassName(typeId.substring(start, end)) - + typeId.substring(end); + final String className = _findClassName(typeId.substring(start, end)); + return (className == null) ? null + : typeId.substring(0, start) + className + typeId.substring(end); } - private static String _resolveClassName(String name) + private static String _findClassName(String name) { // Nothing to probe without namespace; and no real class has name // very long or nested very deep if ((name.indexOf('.') < 0) || !_shouldProbe(name)) { - return name; - } - String className = CLASS_NAMES.get(name); - if (className != null) { - return className; + return null; } // 23-Aug-2024:[dataformats-binary#167] Based on SpecificData::getClass // from Apache Avro. Initially assume that namespace is a Java package @@ -80,13 +67,11 @@ private static String _resolveClassName(String name) // Class does not exist - perhaps last dot is actually a nested class lastDot = sb.lastIndexOf(".", lastDot); if (lastDot < 0) { - return name; + return null; } sb.setCharAt(lastDot, '$'); } - className = sb.toString(); - CLASS_NAMES.put(name, className); - return className; + return sb.toString(); } // Same lookup order as databind has: context ClassLoader (if any) first diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java index 47284a2f2..66d747001 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java @@ -145,24 +145,31 @@ public void testResolutionIsNotSharedAcrossClassLoaders() throws Exception } // Failure to find class (here: due to context ClassLoader not being set) must - // not be remembered by mapper: lookup may succeed later on + // not be remembered by mapper or schema: lookup may succeed later on @Test - public void testFailedLookupNotRetainedByMapper() throws Exception + public void testFailedLookupNotRetained() throws Exception { final AvroMapper mapper = newMapper(); + final AvroSchema schema = mapper.schemaFrom(SCHEMA_JSON); try (URLClassLoader loader = isolatedLoader()) { - assertTrue(_roundTrip(mapper, loader, null) instanceof Map); - assertTrue(_roundTrip(mapper, loader, null) instanceof Map); + assertTrue(_roundTrip(mapper, schema, loader, null) instanceof Map); + assertTrue(_roundTrip(mapper, schema, loader, null) instanceof Map); - Object result = _roundTrip(mapper, loader, loader); + Object result = _roundTrip(mapper, schema, loader, loader); assertEquals(NS + "$ImplB", result.getClass().getName()); assertSame(loader, result.getClass().getClassLoader()); } } + private Object _roundTrip(AvroMapper mapper, + ClassLoader beanLoader, ClassLoader contextLoader) throws Exception + { + return _roundTrip(mapper, mapper.schemaFrom(SCHEMA_JSON), beanLoader, contextLoader); + } + // Writes `Bean` with `ImplB` value, reads it back using given context // ClassLoader (if any); returns value of `Bean.value` - private Object _roundTrip(AvroMapper mapper, + private Object _roundTrip(AvroMapper mapper, AvroSchema schema, ClassLoader beanLoader, ClassLoader contextLoader) throws Exception { final Class beanClass = beanLoader.loadClass(NS + "$Bean"); @@ -171,7 +178,6 @@ private Object _roundTrip(AvroMapper mapper, implB.getClass().getField("b").set(implB, 42); beanClass.getField("value").set(bean, implB); - final AvroSchema schema = mapper.schemaFrom(SCHEMA_JSON); final byte[] avro = mapper.writer(schema).writeValueAsBytes(bean); // union index 1 (zigzag: 2), then int 42 (zigzag: 84) assertArrayEquals(new byte[] { 2, 84 }, avro); From 09a58780d22bee78d20e4c70d6c05f549d15806e Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 18 Nov 2026 20:10:48 -0800 Subject: [PATCH 6/8] Avoid repeated `getTypeId()` calls in `AvroUntypedDeserializer` (#908) 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 --- .../avro/deser/AvroUntypedDeserializer.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/AvroUntypedDeserializer.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/AvroUntypedDeserializer.java index 8c7fa00d4..8f6626f9b 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/AvroUntypedDeserializer.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/deser/AvroUntypedDeserializer.java @@ -58,11 +58,15 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException { // Use type deserializer if we have type information, even for scalar values - if (p.canReadTypeId() && p.getTypeId() != null && typeDeserializer != null) { - TypeIdResolver resolver = typeDeserializer.getTypeIdResolver(); - // Make sure that we actually can resolve the type ID, otherwise we'll end up in a recursive loop - if (resolver != null && resolver.typeFromId(ctxt, p.getTypeId().toString()) != null) { - return typeDeserializer.deserializeTypedFromAny(p, ctxt); + if (typeDeserializer != null && p.canReadTypeId()) { + // NOTE: only get type id once, access may be costly [dataformats-binary#908] + final Object typeId = p.getTypeId(); + if (typeId != null) { + TypeIdResolver resolver = typeDeserializer.getTypeIdResolver(); + // Make sure that we actually can resolve the type ID, otherwise we'll end up in a recursive loop + if (resolver != null && resolver.typeFromId(ctxt, typeId.toString()) != null) { + return typeDeserializer.deserializeTypedFromAny(p, ctxt); + } } } return super.deserializeWithType(p, ctxt, typeDeserializer); @@ -94,10 +98,12 @@ protected Object mapObject(JsonParser p, DeserializationContext ctxt) throws IOE } KeyDeserializer deserializer = null; - if (p.getTypeId() != null) { - if (_typeDeserializer != null) { + if (_typeDeserializer != null) { + // NOTE: only get type id once, access may be costly [dataformats-binary#908] + final Object keyTypeId = p.getTypeId(); + if (keyTypeId != null) { TypeIdResolver idResolver = _typeDeserializer.getTypeIdResolver(); - JavaType keyType = idResolver.typeFromId(ctxt, p.getTypeId().toString()); + JavaType keyType = idResolver.typeFromId(ctxt, keyTypeId.toString()); if (keyType != null) { deserializer = ctxt.findKeyDeserializer(keyType, null); } From efc7b53d997e6318bc0f075b60def69f19c6d198 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 18 Nov 2026 20:15:33 -0800 Subject: [PATCH 7/8] Retain failed type id lookups in `AvroSchema`, per ClassLoader (#908) 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 --- .../jackson/dataformat/avro/AvroSchema.java | 77 +++++++++++++++++-- .../dataformat/avro/AvroTypeIdClassNames.java | 12 +++ .../avro/NestedClassProbeLimits908Test.java | 65 ++++++++++++++-- 3 files changed, 141 insertions(+), 13 deletions(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java index 011edfe3e..6ff1f5abc 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java @@ -1,6 +1,7 @@ package com.fasterxml.jackson.dataformat.avro; import java.io.IOException; +import java.lang.ref.WeakReference; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; @@ -47,6 +48,14 @@ public class AvroSchema implements FormatSchema */ private final ConcurrentHashMap _resolvedTypeIds = new ConcurrentHashMap<>(); + /** + * Type ids for which no Java class was found, for the ClassLoader most recently + * used for lookups (if any): see {@link #resolveTypeId}. + * + * @since 2.24 + */ + private volatile UnresolvedTypeIds _unresolvedTypeIds; + public AvroSchema(Schema asch) { _writerSchema = asch; @@ -174,8 +183,12 @@ public String getSchemaType() { * actual Java class: names of record, enum and fixed schemas use dot as the * separator between enclosing and nested class ({@code package.Outer.Inner}) * so need to probe to find the class ({@code package.Outer$Inner}). - * Only successful lookups are retained: whether class is found may depend on - * context ClassLoader, so failure must not be remembered. + *

+ * Outcomes of lookups are retained, as probing is costly (especially when there + * is no class to find, common when binding as "untyped"). But since whether + * class is found may depend on context ClassLoader, failures are only + * remembered for the ClassLoader that was used: lookup with another one + * is done from scratch. * * @return Type id with name of class found, if any; {@code typeId} itself otherwise * @@ -184,14 +197,62 @@ public String getSchemaType() { String resolveTypeId(String typeId) { String resolved = _resolvedTypeIds.get(typeId); - if (resolved == null) { - resolved = AvroTypeIdClassNames.resolve(typeId); - if (resolved == null) { - return typeId; - } + if (resolved != null) { + return resolved; + } + final ClassLoader loader = AvroTypeIdClassNames.lookupClassLoader(); + UnresolvedTypeIds unresolved = _unresolvedTypeIds; + if ((unresolved != null) && !unresolved.isFor(loader)) { + unresolved = null; + } + if ((unresolved != null) && unresolved.contains(typeId)) { + return typeId; + } + resolved = AvroTypeIdClassNames.resolve(typeId); + if (resolved != null) { _resolvedTypeIds.put(typeId, resolved); + return resolved; + } + // Without ClassLoader to associate failure with, cannot retain + if (loader != null) { + if (unresolved == null) { + // Benign race: at worst some lookups are repeated + _unresolvedTypeIds = unresolved = new UnresolvedTypeIds(loader); + } + unresolved.add(typeId); + } + return typeId; + } + + /** + * Set of type ids for which no class was found using specific ClassLoader. + * ClassLoader is only weakly referenced (and only type ids, not classes, + * retained) to avoid preventing its garbage collection. Size bounded + * by number of types schema has. + */ + private final static class UnresolvedTypeIds + { + private final WeakReference _loader; + + private final Set _typeIds = ConcurrentHashMap.newKeySet(); + + UnresolvedTypeIds(ClassLoader loader) { + _loader = new WeakReference<>(loader); + } + + // NOTE: `loader` may be null, but must not then match (reference + // may have been cleared) + boolean isFor(ClassLoader loader) { + return (loader != null) && (_loader.get() == loader); + } + + boolean contains(String typeId) { + return _typeIds.contains(typeId); + } + + void add(String typeId) { + _typeIds.add(typeId); } - return resolved; } public AvroStructureReader getReader() throws IOException diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java index 761ef4c7f..abd38d25a 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java @@ -74,6 +74,18 @@ private static String _findClassName(String name) return sb.toString(); } + /** + * @return ClassLoader that determines outcome of {@link #resolve}, for the + * current thread: context ClassLoader, if any (ClassLoader of this module + * is used as fallback, but is always the same); otherwise that of this module. + */ + static ClassLoader lookupClassLoader() + { + final ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); + return (contextLoader != null) ? contextLoader + : AvroTypeIdClassNames.class.getClassLoader(); + } + // Same lookup order as databind has: context ClassLoader (if any) first private static boolean _classExists(String className) { diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java index 099595b1a..47959e3dc 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java @@ -77,6 +77,51 @@ public void testProbingWithinLimits() throws Exception assertTrue(count >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count); } + // Failure to find class is remembered for schema, so that probing is not + // repeated for every value (and document)... + @Test + public void testFailedProbingNotRepeated() throws Exception + { + final int dots = 10; + final AvroSchema schema = _schema("probe908v." + _dottedName(dots - 1)); + final CountingClassLoader loader = new CountingClassLoader( + getClass().getClassLoader(), "probe908v."); + _read(schema, loader); + final int first = loader.count.get(); + assertTrue(first >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+first); + + _read(schema, loader); + final int second = loader.count.get(); + _assertNotProbed(second - first); + _read(schema, loader); + _assertNotProbed(loader.count.get() - second); + } + + // ... but only for the ClassLoader used: with another one class might be found + @Test + public void testFailedProbingRepeatedForDifferentClassLoader() throws Exception + { + final int dots = 10; + final AvroSchema schema = _schema("probe908w." + _dottedName(dots - 1)); + final CountingClassLoader loader1 = new CountingClassLoader( + getClass().getClassLoader(), "probe908w."); + _read(schema, loader1); + final int count1 = loader1.count.get(); + assertTrue(count1 >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count1); + + final CountingClassLoader loader2 = new CountingClassLoader( + getClass().getClassLoader(), "probe908w."); + _read(schema, loader2); + final int count2 = loader2.count.get(); + assertTrue(count2 >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count2); + assertEquals(count1, loader1.count.get()); + + // and back to the first one: probed again, as only the latest is retained + _read(schema, loader1); + final int count1b = loader1.count.get() - count1; + assertTrue(count1b >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count1b); + } + @Test @Timeout(value = 10, unit = TimeUnit.SECONDS) public void testNoProbingForExcessivelyDottedName() throws Exception @@ -103,21 +148,32 @@ private void _assertNotProbed(int count) { // (that of non-existing class): returns number of class loading attempts // via context ClassLoader, for names with given prefix private int _readAndCountLoads(String namespace, String prefix) throws Exception + { + final CountingClassLoader loader = new CountingClassLoader( + getClass().getClassLoader(), prefix); + _read(_schema(namespace), loader); + return loader.count.get(); + } + + private AvroSchema _schema(String namespace) { Schema rec = Schema.createRecord("Rec", null, namespace, false, Arrays.asList(new Schema.Field("b", Schema.create(Schema.Type.INT)))); Schema root = Schema.createRecord("ObjectBean", null, "probe908", false, Arrays.asList(new Schema.Field("value", rec))); + return new AvroSchema(root); + } - final CountingClassLoader loader = new CountingClassLoader( - getClass().getClassLoader(), prefix); + // Reads `ObjectBean` using given schema and context ClassLoader + private void _read(AvroSchema schema, ClassLoader contextLoader) throws Exception + { final Thread thread = Thread.currentThread(); final ClassLoader oldLoader = thread.getContextClassLoader(); - thread.setContextClassLoader(loader); + thread.setContextClassLoader(contextLoader); try { // int 42 (zigzag: 84) ObjectBean result = MAPPER.readerFor(ObjectBean.class) - .with(new AvroSchema(root)) + .with(schema) .readValue(new byte[] { 84 }); assertTrue(result.value instanceof Map, "Expected Map, got: "+result.value.getClass().getName()); @@ -125,7 +181,6 @@ private int _readAndCountLoads(String namespace, String prefix) throws Exception } finally { thread.setContextClassLoader(oldLoader); } - return loader.count.get(); } private static String _longName(int len) { From 5ff5fbf7e39d7143e5a29cfd88cfd79a1068012e Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 18 Nov 2026 21:37:12 -0800 Subject: [PATCH 8/8] Address review findings for type id resolution (#908) - `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 --- .../jackson/dataformat/avro/AvroSchema.java | 102 +++++++++--------- .../dataformat/avro/AvroTypeDeserializer.java | 6 +- .../avro/schema/AvroSchemaHelper.java | 6 ++ .../avro/NestedClassProbeLimits908Test.java | 7 +- .../avro/NestedClassTypeId908Test.java | 37 +++++++ release-notes/VERSION-2.x | 8 +- 6 files changed, 108 insertions(+), 58 deletions(-) diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java index 6ff1f5abc..9d879ca06 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroSchema.java @@ -2,7 +2,9 @@ import java.io.IOException; import java.lang.ref.WeakReference; +import java.util.Map; import java.util.Set; +import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; @@ -40,21 +42,22 @@ public class AvroSchema implements FormatSchema protected final AtomicReference _reader = new AtomicReference<>(); /** - * Type ids (as used by {@link AvroStructureReader}s of this schema) for which - * Java class was found, mapped to type id with name of that class: see - * {@link #resolveTypeId}. Bounded by number of types schema has. + * Outcomes of resolving type ids (as used by {@link AvroStructureReader}s of + * this schema) into names of Java classes, separately for each ClassLoader + * used for lookups (weakly referenced): see {@link #resolveTypeId}. + * Access must be synchronized. * * @since 2.24 */ - private final ConcurrentHashMap _resolvedTypeIds = new ConcurrentHashMap<>(); + private final Map _typeIdsByLoader = new WeakHashMap<>(4); /** - * Type ids for which no Java class was found, for the ClassLoader most recently - * used for lookups (if any): see {@link #resolveTypeId}. + * Most recently used entry of {@link #_typeIdsByLoader}, to avoid + * synchronized access in the common case of a single ClassLoader. * * @since 2.24 */ - private volatile UnresolvedTypeIds _unresolvedTypeIds; + private volatile ResolvedTypeIds _recentTypeIds; public AvroSchema(Schema asch) { @@ -184,11 +187,11 @@ public String getSchemaType() { * separator between enclosing and nested class ({@code package.Outer.Inner}) * so need to probe to find the class ({@code package.Outer$Inner}). *

- * Outcomes of lookups are retained, as probing is costly (especially when there - * is no class to find, common when binding as "untyped"). But since whether - * class is found may depend on context ClassLoader, failures are only - * remembered for the ClassLoader that was used: lookup with another one - * is done from scratch. + * Outcomes of lookups (both class found and not found) are retained, as + * probing is costly; especially when there is no class to find, common when + * binding as "untyped". But since whether class is found may depend on context + * ClassLoader, outcomes are only used for the ClassLoader they were + * produced with: lookup with another one is done from scratch. * * @return Type id with name of class found, if any; {@code typeId} itself otherwise * @@ -196,62 +199,61 @@ public String getSchemaType() { */ String resolveTypeId(String typeId) { - String resolved = _resolvedTypeIds.get(typeId); - if (resolved != null) { - return resolved; - } final ClassLoader loader = AvroTypeIdClassNames.lookupClassLoader(); - UnresolvedTypeIds unresolved = _unresolvedTypeIds; - if ((unresolved != null) && !unresolved.isFor(loader)) { - unresolved = null; - } - if ((unresolved != null) && unresolved.contains(typeId)) { - return typeId; - } - resolved = AvroTypeIdClassNames.resolve(typeId); - if (resolved != null) { - _resolvedTypeIds.put(typeId, resolved); - return resolved; + // Without ClassLoader to associate outcome with, cannot retain + if (loader == null) { + final String resolved = AvroTypeIdClassNames.resolve(typeId); + return (resolved == null) ? typeId : resolved; } - // Without ClassLoader to associate failure with, cannot retain - if (loader != null) { - if (unresolved == null) { - // Benign race: at worst some lookups are repeated - _unresolvedTypeIds = unresolved = new UnresolvedTypeIds(loader); + ResolvedTypeIds typeIds = _recentTypeIds; + if ((typeIds == null) || !typeIds.isFor(loader)) { + synchronized (_typeIdsByLoader) { + typeIds = _typeIdsByLoader.get(loader); + if (typeIds == null) { + typeIds = new ResolvedTypeIds(loader); + _typeIdsByLoader.put(loader, typeIds); + } } - unresolved.add(typeId); + _recentTypeIds = typeIds; } - return typeId; + return typeIds.resolve(typeId); } /** - * Set of type ids for which no class was found using specific ClassLoader. - * ClassLoader is only weakly referenced (and only type ids, not classes, - * retained) to avoid preventing its garbage collection. Size bounded - * by number of types schema has. + * Outcomes of resolving type ids using specific context ClassLoader: type ids + * mapped to ones with name of the class found, or to themselves if none + * was (or if type id is not one to probe for). + * ClassLoader is only weakly referenced, and only type ids (not classes) + * retained, to avoid preventing its garbage collection. + * Size bounded by number of types schema has. */ - private final static class UnresolvedTypeIds + private final static class ResolvedTypeIds { private final WeakReference _loader; - private final Set _typeIds = ConcurrentHashMap.newKeySet(); + private final ConcurrentHashMap _resolved = new ConcurrentHashMap<>(); - UnresolvedTypeIds(ClassLoader loader) { + ResolvedTypeIds(ClassLoader loader) { _loader = new WeakReference<>(loader); } - // NOTE: `loader` may be null, but must not then match (reference - // may have been cleared) + // NOTE: `loader` must not be null (reference may have been cleared) boolean isFor(ClassLoader loader) { - return (loader != null) && (_loader.get() == loader); + return _loader.get() == loader; } - boolean contains(String typeId) { - return _typeIds.contains(typeId); - } - - void add(String typeId) { - _typeIds.add(typeId); + // NOTE: must be called with ClassLoader this instance is for + // as the context ClassLoader (or without one, if for that of module) + String resolve(String typeId) { + String resolved = _resolved.get(typeId); + if (resolved == null) { + resolved = AvroTypeIdClassNames.resolve(typeId); + if (resolved == null) { + resolved = typeId; + } + _resolved.put(typeId, resolved); + } + return resolved; } } diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeDeserializer.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeDeserializer.java index dbb74fae0..6d114a5c9 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeDeserializer.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeDeserializer.java @@ -54,7 +54,9 @@ public Object deserializeTypedFromScalar(JsonParser p, DeserializationContext ct @Override public Object deserializeTypedFromAny(JsonParser p, DeserializationContext ctxt) throws IOException { - if (p.getTypeId() == null && getDefaultImpl() == null) { + // NOTE: only get type id once, access may be costly [dataformats-binary#908] + final Object typeId = p.getTypeId(); + if (typeId == null && getDefaultImpl() == null) { JsonDeserializer deser = _findDeserializer(ctxt, AvroSchemaHelper.getTypeId(_baseType)); if (deser == null) { ctxt.reportInputMismatch(_baseType, "No (native) type id found when one was expected for polymorphic type handling"); @@ -62,7 +64,7 @@ public Object deserializeTypedFromAny(JsonParser p, DeserializationContext ctxt) } return deser.deserialize(p, ctxt); } - return _deserializeWithNativeTypeId(p, ctxt, p.getTypeId()); + return _deserializeWithNativeTypeId(p, ctxt, typeId); } @Override diff --git a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java index 854b3db6d..e2eb62afa 100644 --- a/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/schema/AvroSchemaHelper.java @@ -340,6 +340,12 @@ public static String getTypeId(Class type) { /** * Returns the type ID for this schema, or {@code null} if none is present. + *

+ * NOTE: since 2.24 type id of record, enum and fixed schemas is name exactly + * as schema has it (see {@link #getFullName}), using dot as the separator + * for nested classes too ({@code package.Outer.Inner}): so it does not + * necessarily equal {@link #getTypeId(Class)} of matching class + * ({@code package.Outer$Inner}), nor can be used for loading the class as-is. */ public static String getTypeId(Schema schema) { switch (schema.getType()) { diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java index 47959e3dc..f850a04cc 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java @@ -116,10 +116,11 @@ public void testFailedProbingRepeatedForDifferentClassLoader() throws Exception assertTrue(count2 >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count2); assertEquals(count1, loader1.count.get()); - // and back to the first one: probed again, as only the latest is retained + // and outcomes are retained for both, when alternating _read(schema, loader1); - final int count1b = loader1.count.get() - count1; - assertTrue(count1b >= dots + 1, "Expected at least "+(dots+1)+" attempts, got: "+count1b); + _assertNotProbed(loader1.count.get() - count1); + _read(schema, loader2); + _assertNotProbed(loader2.count.get() - count2); } @Test diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java index 66d747001..18d0012eb 100644 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java @@ -16,6 +16,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.dataformat.avro.ser.AvroWriteContext; import static org.junit.jupiter.api.Assertions.*; @@ -161,6 +163,41 @@ public void testFailedLookupNotRetained() throws Exception } } + // Finding class with one ClassLoader must not be remembered for others + // either, for shared schema: type id must not depend on order of access + @Test + public void testSuccessfulLookupNotSharedAcrossClassLoaders() throws Exception + { + final AvroMapper mapper = newMapper(); + final AvroSchema schema = mapper.schemaFrom(SCHEMA_JSON); + try (URLClassLoader loader = isolatedLoader()) { + assertEquals(NS + "$ImplB", _streamingTypeId(mapper, schema, loader)); + assertEquals(NS + ".ImplB", _streamingTypeId(mapper, schema, null)); + assertEquals(NS + "$ImplB", _streamingTypeId(mapper, schema, loader)); + assertEquals(NS + ".ImplB", _streamingTypeId(mapper, schema, null)); + } + } + + // Returns type id parser exposes for `ImplB` value of `Bean`, using given + // context ClassLoader (if any) + private Object _streamingTypeId(AvroMapper mapper, AvroSchema schema, + ClassLoader contextLoader) throws Exception + { + final Thread thread = Thread.currentThread(); + final ClassLoader oldLoader = thread.getContextClassLoader(); + thread.setContextClassLoader(contextLoader); + // union index 1 (zigzag: 2), then int 42 (zigzag: 84) + try (JsonParser p = mapper.getFactory().createParser(new byte[] { 2, 84 })) { + p.setSchema(schema); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertToken(JsonToken.FIELD_NAME, p.nextToken()); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + return p.getTypeId(); + } finally { + thread.setContextClassLoader(oldLoader); + } + } + private Object _roundTrip(AvroMapper mapper, ClassLoader beanLoader, ClassLoader contextLoader) throws Exception { diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 454e0d4b4..2131f40cb 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -18,9 +18,11 @@ Active maintainers: #908: (avro) `AvroSchemaHelper.getFullName()` probes for nested classes with different ClassLoader than databind uses to load them - (NOTE: `AvroSchemaHelper.getFullName()` no longer probes for classes, returns name - as schema has it; probing now done by `AvroParser.getTypeId()`, using context - ClassLoader first) + (NOTE: `AvroSchemaHelper.getFullName()` and `AvroSchemaHelper.getTypeId(Schema)` + no longer probe for classes, return name as schema has it (`package.Outer.Inner` + for nested classes, not `package.Outer$Inner`); probing now done by + `AvroParser.getTypeId()`, using context ClassLoader first. + Also: `AvroParser.getSchema()` now returns schema set, instead of `null`) 2.23.1 (not yet released)