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..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 @@ -224,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()); @@ -239,7 +242,13 @@ public boolean canReadTypeId() { @Override public Object getTypeId() throws IOException { - return _avroContext != null ? _avroContext.getTypeId() : null; + final String typeId = (_avroContext == null) ? null : _avroContext.getTypeId(); + if ((typeId == null) || (_rootSchema == null)) { + return typeId; + } + // [dataformats-binary#908]: names from schema use dot as separator for + // 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..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 @@ -1,7 +1,11 @@ package com.fasterxml.jackson.dataformat.avro; 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; import org.apache.avro.Schema; @@ -37,6 +41,24 @@ public class AvroSchema implements FormatSchema */ protected final AtomicReference _reader = new AtomicReference<>(); + /** + * 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 Map _typeIdsByLoader = new WeakHashMap<>(4); + + /** + * Most recently used entry of {@link #_typeIdsByLoader}, to avoid + * synchronized access in the common case of a single ClassLoader. + * + * @since 2.24 + */ + private volatile ResolvedTypeIds _recentTypeIds; + public AvroSchema(Schema asch) { _writerSchema = asch; @@ -159,6 +181,82 @@ 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}). + *

+ * 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 + * + * @since 2.24 + */ + String resolveTypeId(String typeId) + { + final ClassLoader loader = AvroTypeIdClassNames.lookupClassLoader(); + // Without ClassLoader to associate outcome with, cannot retain + if (loader == null) { + final String resolved = AvroTypeIdClassNames.resolve(typeId); + return (resolved == null) ? typeId : resolved; + } + 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); + } + } + _recentTypeIds = typeIds; + } + return typeIds.resolve(typeId); + } + + /** + * 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 ResolvedTypeIds + { + private final WeakReference _loader; + + private final ConcurrentHashMap _resolved = new ConcurrentHashMap<>(); + + ResolvedTypeIds(ClassLoader loader) { + _loader = new WeakReference<>(loader); + } + + // NOTE: `loader` must not be null (reference may have been cleared) + boolean isFor(ClassLoader loader) { + return _loader.get() == loader; + } + + // 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; + } + } + public AvroStructureReader getReader() throws IOException { AvroStructureReader r = _reader.get(); 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/AvroTypeIdClassNames.java b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java new file mode 100644 index 000000000..abd38d25a --- /dev/null +++ b/avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroTypeIdClassNames.java @@ -0,0 +1,131 @@ +package com.fasterxml.jackson.dataformat.avro; + +/** + * 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; + + 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) + { + final int start = typeId.indexOf('<') + 1; + if (start == 0) { + 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 null; + } + } + final String className = _findClassName(typeId.substring(start, end)); + return (className == null) ? null + : typeId.substring(0, start) + className + typeId.substring(end); + } + + 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 null; + } + // 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 (!_classExists(sb.toString())) { + // Class does not exist - perhaps last dot is actually a nested class + lastDot = sb.lastIndexOf(".", lastDot); + if (lastDot < 0) { + return null; + } + sb.setCharAt(lastDot, '$'); + } + 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) + { + 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 loader) + { + try { + // Only need to know class exists, so must not initialize it + Class.forName(className, false, loader); + 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/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); } 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..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 @@ -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 @@ -341,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()) { @@ -354,12 +359,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 AvroParser.getTypeId()}, when (and only if) type id is needed. */ public static String getFullName(Schema schema) { final Schema.Type type = schema.getType(); @@ -380,10 +389,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 +451,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..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,11 +320,11 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) { // - ByteBuffer // - Number wrappers (Integer, ...) - String typeId = AvroSchemaHelper.getTypeId(datum.getClass()); + 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 (typeId.equals(AvroSchemaHelper.getTypeId(schema))) { + // Schema for the class? + if (typeId.equals(_dottedTypeId(AvroSchemaHelper.getTypeId(schema)))) { return i; } } @@ -369,11 +369,11 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) { return types.get(_resolveMapIndex(unionSchema, types, datum)); } - String typeId = AvroSchemaHelper.getTypeId(datum.getClass()); + 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 (typeId.equals(AvroSchemaHelper.getTypeId(schema))) { + // Schema for the class? + if (typeId.equals(_dottedTypeId(AvroSchemaHelper.getTypeId(schema)))) { return schema; } } @@ -415,6 +415,14 @@ 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 (and schemas may + // 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 == null) ? null : typeId.replace('$', '.'); + } + private static Schema _findNotNull(List types) { if (types.size() == 2) { 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..f850a04cc --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassProbeLimits908Test.java @@ -0,0 +1,203 @@ +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 `AvroParser.getTypeId()` +// 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 = AvroTypeIdClassNames.MAX_NAMESPACE_DOTS; + assertEquals(32, max); + 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 = AvroTypeIdClassNames.MAX_CLASS_NAME_LENGTH; + assertEquals(1024, max); + final String name = ".Rec"; + final int maxNamespace = max - name.length(); + 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: + // 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); + } + + // 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 outcomes are retained for both, when alternating + _read(schema, loader1); + _assertNotProbed(loader1.count.get() - count1); + _read(schema, loader2); + _assertNotProbed(loader2.count.get() - count2); + } + + @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(AvroTypeIdClassNames.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 + { + 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); + } + + // 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(contextLoader); + try { + // int 42 (zigzag: 84) + ObjectBean result = MAPPER.readerFor(ObjectBean.class) + .with(schema) + .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); + } + } + + 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 new file mode 100644 index 000000000..18d0012eb --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java @@ -0,0 +1,233 @@ +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.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +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()); + } + + // 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(newMapper(), 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(newMapper(), 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(newMapper(), loader1, null) instanceof Map); + + Object result1 = _roundTrip(newMapper(), loader1, loader1); + assertSame(loader1, result1.getClass().getClassLoader()); + + Object result2 = _roundTrip(newMapper(), loader2, loader2); + assertSame(loader2, result2.getClass().getClassLoader()); + + // and failing lookup still fails + 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 or schema: lookup may succeed later on + @Test + public void testFailedLookupNotRetained() throws Exception + { + final AvroMapper mapper = newMapper(); + final AvroSchema schema = mapper.schemaFrom(SCHEMA_JSON); + try (URLClassLoader loader = isolatedLoader()) { + assertTrue(_roundTrip(mapper, schema, loader, null) instanceof Map); + assertTrue(_roundTrip(mapper, schema, loader, null) instanceof Map); + + Object result = _roundTrip(mapper, schema, loader, loader); + assertEquals(NS + "$ImplB", result.getClass().getName()); + assertSame(loader, result.getClass().getClassLoader()); + } + } + + // 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 + { + 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, AvroSchema schema, + 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 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); + } + } +} 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..610b1ccf4 --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeIdResolution908Test.java @@ -0,0 +1,173 @@ +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.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +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; } + } + + 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 + 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(); + + // 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 + { + 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); + } + + // Check for registered subtypes must apply to nested classes too + @Test + public void testUnregisteredSubtypeNotAllowed() 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; + } +} 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..2131f40cb 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -14,6 +14,16 @@ 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: `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) #906: (avro) Do not initialize class named by Avro schema when resolving its full name;