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..b99e6f860 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 @@ -5,11 +5,13 @@ import com.fasterxml.jackson.databind.DatabindContext; import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonMappingException; 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, @@ -21,8 +23,25 @@ public class AvroTypeIdResolver extends ClassNameIdResolver { private static final long serialVersionUID = 1L; + // Maximum number of dots in type id for which nested-class probing is done: + // each one means one more class loading attempt for (possibly untrusted) id + final static int MAX_PROBE_DOTS = 32; + + // Maximum length of type id for which nested-class probing is done: + // well above length of any real class name + final static int MAX_PROBE_LENGTH = 1024; + private final Map> _idTypes = new HashMap<>(); + /** + * Cache of type ids (full names from Avro schema, with dots for nested classes) resolved + * into class names; ids for which no class is found are mapped to themselves. + * Since resolver is constructed for a mapper, so is this cache. + * + * @since 2.24 + */ + private final LRUMap _resolvedIds = new LRUMap<>(16, 100); + // 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<>(); @@ -56,6 +75,11 @@ 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) { + // [dataformats-binary#908]: id from schema uses dots for nested classes too + id = _resolveNestedClassName(id, ctxt); + subType = _idTypes.get(id); + } if (subType != null) { id = _idFrom(null, subType, _typeFactory); } @@ -67,4 +91,100 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti return null; } } + + /** + * Type ids for records, enums and fixed types are full names from Avro schema, in which + * (since Apache Avro 1.9) nested classes are separated by dots, not dollar signs: this + * method finds the name of the actual class, trying given id first, then replacing dots + * with dollar signs, one by one, starting from the last one. + * Classes are looked up same way as when resolving type itself (no class initialization, + * same {@link ClassLoader}s, subtype validation), unlike with earlier probing by + * {@code AvroSchemaHelper}. + * + * @return Name of the class to use, if one found; {@code id} if not + * + * @since 2.24 + */ + protected String _resolveNestedClassName(String id, DatabindContext ctxt) + { + if (!_shouldProbe(id)) { + return id; + } + String resolved = _resolvedIds.get(id); + if (resolved == null) { + // Canonical id of generic type (see `AvroReaderFactory`, for `EnumSet`)? + // Type parameters may be full names from schema as well + if (id.indexOf('<') > 0) { + resolved = _resolveGenericClassNames(id, ctxt); + } else { + resolved = _resolveClassName(id, _baseType, ctxt); + } + _resolvedIds.put(id, resolved); + } + return resolved; + } + + private String _resolveGenericClassNames(String id, DatabindContext ctxt) + { + final JavaType paramBaseType = ctxt.constructType(Object.class); + final StringBuilder sb = new StringBuilder(id.length()); + boolean first = true; + int start = 0; + for (int i = 0, len = id.length(); i < len; ++i) { + final char c = id.charAt(i); + if (c == '<' || c == '>' || c == ',') { + if (i > start) { + sb.append(_resolveClassName(id.substring(start, i), + first ? _baseType : paramBaseType, ctxt)); + first = false; + } + sb.append(c); + start = i + 1; + } + } + return sb.append(id, start, id.length()).toString(); + } + + private String _resolveClassName(String id, JavaType baseType, DatabindContext ctxt) + { + final StringBuilder sb = new StringBuilder(id); + int lastDot = sb.length(); + String candidate = id; + while (true) { + // Registered subtypes are known without class lookups + if (_idTypes.containsKey(candidate)) { + return candidate; + } + try { + if (ctxt.resolveAndValidateSubType(baseType, candidate, _subTypeValidator) != null) { + return candidate; + } + } catch (JsonMappingException e) { + // Class exists but is not valid (not allowed, not a subtype): no point in + // trying others; caller gets to handle the problem + return candidate; + } + lastDot = sb.lastIndexOf(".", lastDot); + if (lastDot == -1) { // no more dots, nothing found + return id; + } + sb.setCharAt(lastDot, '$'); + candidate = sb.toString(); + } + } + + // Package-visible for tests + static boolean _shouldProbe(String id) { + if (id.length() > MAX_PROBE_LENGTH) { + return false; + } + int count = 0; + for (int ix = id.indexOf('.'); ix >= 0; ix = id.indexOf('.', ix + 1)) { + if (++count > MAX_PROBE_DOTS) { + return false; + } + } + // No dots, no nested-class alternatives + return (count > 0); + } } 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..1d0676c04 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,15 @@ 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 that it properly handles namespaces + * that use dollar sign for nested classes (package.name.ClassName$NestedClassName instead of + * package.name.ClassName$.NestedClassName). + *

+ * NOTE: since 2.24 this method no longer probes for existence of nested classes (which required loading + * classes named by possibly untrusted schema, using this module's {@link ClassLoader}): for namespaces that + * use dots for nested classes (Apache Avro 1.9 and later) plain dotted name + * (package.name.ClassName.NestedClassName) is returned. Resolution into actual nested class + * is done by {@code AvroTypeIdResolver} when deserializing, and by {@link #typeIdMatches} when serializing. */ public static String getFullName(Schema schema) { final Schema.Type type = schema.getType(); @@ -380,16 +382,64 @@ 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); + // [dataformats-binary#908]: no probing for nested classes + return namespace + '.' + name; default: return type.getName(); } } + /** + * Method for checking whether given Java class has the type id of given schema: + * for named types (records, enums, fixed) this means that class name matches full name + * of the schema, allowing for nested classes being separated by either dollar sign + * (as in class name) or dot (as in namespaces of Apache Avro 1.9 and later). + * Does not load any classes. + * + * @since 2.24 + */ + public static boolean typeIdMatches(Class type, Schema schema) { + final String typeId = getTypeId(type); + switch (schema.getType()) { + case RECORD: + case ENUM: + case FIXED: + break; + default: + return typeId.equals(schema.getProp(AVRO_SCHEMA_PROP_CLASS)); + } + final String name = schema.getName(); + final String namespace = schema.getNamespace(); + if (namespace == null || namespace.isEmpty()) { + return typeId.equals(name); + } + final int nsLen = namespace.length(); + // Namespace either ends with '$', or is separated from name by '.' ('$' in class name) + int nameStart = nsLen; + if (namespace.charAt(nsLen-1) != '$') { + ++nameStart; + } + if ((typeId.length() != nameStart + name.length()) || !typeId.endsWith(name)) { + return false; + } + if (nameStart > nsLen) { + final char sep = typeId.charAt(nsLen); + if (sep != '.' && sep != '$') { + return false; + } + } + for (int i = 0; i < nsLen; ++i) { + final char c = typeId.charAt(i); + final char nsc = namespace.charAt(i); + // Class name may have '$' where namespace has '.' (but not vice versa) + if ((c != nsc) && (c != '$' || nsc != '.')) { + return false; + } + } + return true; + } + public static JsonNode nullNode() { return DEFAULT_VALUE_MAPPER.nullNode(); } @@ -442,112 +492,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..b5bca3e6a 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,10 @@ public static int resolveUnionIndex(Schema unionSchema, Object datum) { // - ByteBuffer // - Number wrappers (Integer, ...) - String typeId = AvroSchemaHelper.getTypeId(datum.getClass()); 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 (AvroSchemaHelper.typeIdMatches(raw, schema)) { return i; } } @@ -369,11 +368,10 @@ public static Schema resolveUnionType(Schema unionSchema, Object datum) { return types.get(_resolveMapIndex(unionSchema, types, datum)); } - String typeId = AvroSchemaHelper.getTypeId(datum.getClass()); 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 (AvroSchemaHelper.typeIdMatches(raw, schema)) { return schema; } } 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..e53e854e2 --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/NestedClassTypeId908Test.java @@ -0,0 +1,154 @@ +package com.fasterxml.jackson.dataformat.avro; + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +// [dataformats-binary#908]: nested classes named by schema (with dots, as with +// Avro 1.9+) are resolved when deserializing, by type id resolver (using databind's +// class lookup), instead of by probing when schema is processed +public class NestedClassTypeId908Test extends AvroTestBase +{ + static class Leaf { + public String name; + + protected Leaf() { } + public Leaf(String n) { name = n; } + + // Nested class, 2 levels deep + static class SubLeaf { + public int id; + + protected SubLeaf() { } + public SubLeaf(int i) { id = i; } + } + } + + static class ObjectWrapper { + public Object value; + + protected ObjectWrapper() { } + public ObjectWrapper(Object v) { value = v; } + } + + private final static String TEST_CLASS_NAME = NestedClassTypeId908Test.class.getName(); + + private final AvroMapper MAPPER = newMapper(); + + private AvroSchema _wrapperSchema(String namespace, String name, String fieldDef) throws Exception { + return MAPPER.schemaFrom("{\"type\":\"record\",\"name\":\"ObjectWrapper\",\"fields\":[" + +"{\"name\":\"value\",\"type\":{\"type\":\"record\",\"name\":\"" + name + "\"," + +"\"namespace\":\"" + namespace + "\"," + +"\"fields\":[" + fieldDef + "]}}]}"); + } + + @Test + public void testNestedClassResolvedFromDottedNamespace() throws Exception + { + AvroSchema schema = _wrapperSchema(TEST_CLASS_NAME, "Leaf", + "{\"name\":\"name\",\"type\":\"string\"}"); + byte[] avro = MAPPER.writer(schema).writeValueAsBytes(new ObjectWrapper(new Leaf("abc"))); + // twice, to cover cached resolution too + for (int i = 0; i < 2; ++i) { + ObjectWrapper result = MAPPER.readerFor(ObjectWrapper.class) + .with(schema) + .readValue(avro); + assertEquals(Leaf.class, result.value.getClass()); + assertEquals("abc", ((Leaf) result.value).name); + } + } + + @Test + public void testDeeperNestedClassResolvedFromDottedNamespace() throws Exception + { + AvroSchema schema = _wrapperSchema(TEST_CLASS_NAME + ".Leaf", "SubLeaf", + "{\"name\":\"id\",\"type\":\"int\"}"); + byte[] avro = MAPPER.writer(schema).writeValueAsBytes(new ObjectWrapper(new Leaf.SubLeaf(42))); + ObjectWrapper result = MAPPER.readerFor(ObjectWrapper.class) + .with(schema) + .readValue(avro); + assertEquals(Leaf.SubLeaf.class, result.value.getClass()); + assertEquals(42, ((Leaf.SubLeaf) result.value).id); + } + + // Older style with dollar signs in namespace still works + @Test + public void testNestedClassResolvedFromDollarNamespace() throws Exception + { + AvroSchema schema = _wrapperSchema(TEST_CLASS_NAME + "$", "Leaf", + "{\"name\":\"name\",\"type\":\"string\"}"); + byte[] avro = MAPPER.writer(schema).writeValueAsBytes(new ObjectWrapper(new Leaf("abc"))); + ObjectWrapper result = MAPPER.readerFor(ObjectWrapper.class) + .with(schema) + .readValue(avro); + assertEquals(Leaf.class, result.value.getClass()); + } + + // Name that matches no class: "natural" type (Map), as before + @Test + public void testUnknownClassNameReadAsMap() throws Exception + { + AvroSchema schema = _wrapperSchema(TEST_CLASS_NAME, "NoSuchClass", + "{\"name\":\"name\",\"type\":\"string\"}"); + for (int i = 0; i < 2; ++i) { + ObjectWrapper result = MAPPER.readerFor(ObjectWrapper.class) + .with(schema) + .readValue(new byte[] { 6, 'a', 'b', 'c' }); + assertInstanceOf(Map.class, result.value); + assertEquals("abc", ((Map) result.value).get("name")); + } + } + + @Test + public void testProbeLimits() throws Exception + { + // no dots, nothing to probe + assertFalse(AvroTypeIdResolver._shouldProbe("")); + assertFalse(AvroTypeIdResolver._shouldProbe("Name")); + + final int maxDots = AvroTypeIdResolver.MAX_PROBE_DOTS; + assertEquals(32, maxDots); + assertTrue(AvroTypeIdResolver._shouldProbe("a.B")); + assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(maxDots - 1))); + assertTrue(AvroTypeIdResolver._shouldProbe(_dottedName(maxDots))); + assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(maxDots + 1))); + assertFalse(AvroTypeIdResolver._shouldProbe(_dottedName(maxDots + 100))); + + final int maxLen = AvroTypeIdResolver.MAX_PROBE_LENGTH; + assertEquals(1024, maxLen); + assertTrue(AvroTypeIdResolver._shouldProbe("a." + _longName(maxLen - 3))); + assertTrue(AvroTypeIdResolver._shouldProbe("a." + _longName(maxLen - 2))); + assertFalse(AvroTypeIdResolver._shouldProbe("a." + _longName(maxLen - 1))); + assertFalse(AvroTypeIdResolver._shouldProbe("a." + _longName(100_000))); + } + + // Names beyond limits are not probed, read as "natural" type + @Test + public void testExcessivelyDottedNameReadAsMap() throws Exception + { + AvroSchema schema = _wrapperSchema(_dottedName(40), "Leaf", + "{\"name\":\"name\",\"type\":\"string\"}"); + ObjectWrapper result = MAPPER.readerFor(ObjectWrapper.class) + .with(schema) + .readValue(new byte[] { 6, 'a', 'b', 'c' }); + assertInstanceOf(Map.class, result.value); + } + + private static String _dottedName(int dots) { + StringBuilder sb = new StringBuilder("a"); + for (int i = 0; i < dots; ++i) { + sb.append(".a"); + } + return sb.toString(); + } + + private static String _longName(int len) { + StringBuilder sb = new StringBuilder(len); + while (sb.length() < len) { + sb.append('x'); + } + return sb.toString(); + } +} diff --git a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaFullNameTest.java b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaFullNameTest.java new file mode 100644 index 000000000..fcec1c9cb --- /dev/null +++ b/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaFullNameTest.java @@ -0,0 +1,204 @@ +package com.fasterxml.jackson.dataformat.avro.schema; + +import java.util.Arrays; + +import org.apache.avro.Schema; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.dataformat.avro.AvroMapper; +import com.fasterxml.jackson.dataformat.avro.AvroSchema; +import com.fasterxml.jackson.dataformat.avro.AvroTestBase; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +// [dataformats-binary#906], [dataformats-binary#908]: resolving full name (type id) +// of a record, enum or fixed schema must not load, let alone initialize, the class +// named by the (possibly untrusted) schema; matching against a known class works +// without class loading +public class SchemaFullNameTest extends AvroTestBase +{ + static class InitTracker { + static volatile boolean initialized; + static volatile boolean enumInitialized; + static volatile boolean fixedInitialized; + } + + // Never referenced by test code: only named by the schema below + static class NotToBeInitialized { + static { + InitTracker.initialized = true; + } + } + + // Likewise only named by ENUM / FIXED schemas + enum EnumNotToBeInitialized { + A, B; + + static { + InitTracker.enumInitialized = true; + } + } + + static class FixedNotToBeInitialized { + static { + InitTracker.fixedInitialized = true; + } + } + + static class Inner { + public String x; + + static class Deeper { } + } + + static class Outer { + public Inner value; + } + + private final static String TEST_CLASS_NAME = SchemaFullNameTest.class.getName(); + + private final AvroMapper MAPPER = newMapper(); + + @Test + public void testReadDoesNotInitializeClassNamedBySchema() throws Exception + { + AvroSchema schema = MAPPER.schemaFrom("{\"type\":\"record\",\"name\":\"Outer\",\"fields\":[" + +"{\"name\":\"value\",\"type\":{\"type\":\"record\",\"name\":\"NotToBeInitialized\"," + +"\"namespace\":\"" + TEST_CLASS_NAME + "\"," + +"\"fields\":[{\"name\":\"x\",\"type\":\"string\"}]}}]}"); + Outer result = MAPPER.readerFor(Outer.class) + .with(schema) + .readValue(new byte[] { 2, 'a' }); + assertEquals("a", result.value.x); + + // Name is not resolved to that of nested class + assertEquals(TEST_CLASS_NAME + ".NotToBeInitialized", + AvroSchemaHelper.getFullName(schema.getAvroSchema().getField("value").schema())); + assertFalse(InitTracker.initialized); + } + + @Test + public void testEnumSchemaDoesNotInitializeNamedClass() throws Exception + { + Schema schema = Schema.createEnum("EnumNotToBeInitialized", null, + TEST_CLASS_NAME, Arrays.asList("A", "B")); + assertEquals(TEST_CLASS_NAME + ".EnumNotToBeInitialized", + AvroSchemaHelper.getFullName(schema)); + assertFalse(InitTracker.enumInitialized); + } + + @Test + public void testFixedSchemaDoesNotInitializeNamedClass() throws Exception + { + Schema schema = Schema.createFixed("FixedNotToBeInitialized", null, + TEST_CLASS_NAME, 4); + assertEquals(TEST_CLASS_NAME + ".FixedNotToBeInitialized", + AvroSchemaHelper.getFullName(schema)); + assertFalse(InitTracker.fixedInitialized); + } + + // Namespace that uses dollar sign for nested classes (Avro before 1.9) + @Test + public void testFullNameWithDollarNamespace() throws Exception + { + Schema schema = Schema.createRecord("Inner", null, TEST_CLASS_NAME + "$", false); + assertEquals(TEST_CLASS_NAME + "$Inner", AvroSchemaHelper.getFullName(schema)); + + assertEquals("Inner", + AvroSchemaHelper.getFullName(Schema.createRecord("Inner", null, null, false))); + } + + @Test + public void testTypeIdMatchesForNestedClasses() throws Exception + { + // Dots for nested classes (Avro 1.9+) + assertTrue(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, TEST_CLASS_NAME, false))); + assertTrue(AvroSchemaHelper.typeIdMatches(Inner.Deeper.class, + Schema.createRecord("Deeper", null, TEST_CLASS_NAME + ".Inner", false))); + // Dollar signs (older Avro), fully or partially + assertTrue(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, TEST_CLASS_NAME + "$", false))); + assertTrue(AvroSchemaHelper.typeIdMatches(Inner.Deeper.class, + Schema.createRecord("Deeper", null, TEST_CLASS_NAME + "$Inner$", false))); + assertTrue(AvroSchemaHelper.typeIdMatches(Inner.Deeper.class, + Schema.createRecord("Deeper", null, TEST_CLASS_NAME + "$Inner", false))); + // Enum, fixed too + assertTrue(AvroSchemaHelper.typeIdMatches(EnumNotToBeInitialized.class, + Schema.createEnum("EnumNotToBeInitialized", null, + TEST_CLASS_NAME, Arrays.asList("A", "B")))); + assertFalse(InitTracker.enumInitialized); + } + + @Test + public void testTypeIdMatchesForTopLevelClasses() throws Exception + { + final String pkg = SchemaFullNameTest.class.getPackage().getName(); + assertTrue(AvroSchemaHelper.typeIdMatches(SchemaFullNameTest.class, + Schema.createRecord("SchemaFullNameTest", null, pkg, false))); + // class in default package would have no namespace + assertFalse(AvroSchemaHelper.typeIdMatches(SchemaFullNameTest.class, + Schema.createRecord("SchemaFullNameTest", null, null, false))); + } + + @Test + public void testTypeIdMatchesMismatches() throws Exception + { + final String pkg = SchemaFullNameTest.class.getPackage().getName(); + // wrong name, same length + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Outer", null, TEST_CLASS_NAME, false))); + // wrong namespace, same length + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, + pkg + ".SchemaFullNameTesX", false))); + // name is suffix only + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("ner", null, TEST_CLASS_NAME + ".In", false))); + // namespace separator where class name has none + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, + pkg + ".SchemaFullName.est", false))); + // dollar sign in namespace does not match dot (package separator) in class name + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, + pkg.replace('.', '$') + ".SchemaFullNameTest", false))); + // shorter / longer + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, pkg, false))); + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, + Schema.createRecord("Inner", null, TEST_CLASS_NAME + ".Inner", false))); + } + + // Non-named types match by "java-class" property + @Test + public void testTypeIdMatchesForNonNamedTypes() throws Exception + { + Schema schema = Schema.create(Schema.Type.STRING); + assertFalse(AvroSchemaHelper.typeIdMatches(String.class, schema)); + schema.addProp(AvroSchemaHelper.AVRO_SCHEMA_PROP_CLASS, String.class.getName()); + assertTrue(AvroSchemaHelper.typeIdMatches(String.class, schema)); + assertFalse(AvroSchemaHelper.typeIdMatches(Integer.class, schema)); + + // primitives use name of wrapper + Schema intSchema = Schema.create(Schema.Type.INT); + intSchema.addProp(AvroSchemaHelper.AVRO_SCHEMA_PROP_CLASS, Integer.class.getName()); + assertTrue(AvroSchemaHelper.typeIdMatches(int.class, intSchema)); + } + + // Excessively long / dotted names are not a problem as nothing gets probed + @Test + public void testFullNameForExcessivelyDottedNamespace() throws Exception + { + StringBuilder sb = new StringBuilder("a"); + for (int i = 0; i < 20_000; ++i) { + sb.append(".a"); + } + final String namespace = sb.toString(); + Schema schema = Schema.createRecord("Rec", null, namespace, false); + assertEquals(namespace + ".Rec", AvroSchemaHelper.getFullName(schema)); + assertFalse(AvroSchemaHelper.typeIdMatches(Inner.class, schema)); + } +} 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 deleted file mode 100644 index 2016030ba..000000000 --- a/avro/src/test/java/com/fasterxml/jackson/dataformat/avro/schema/SchemaNameClassInitTest.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.fasterxml.jackson.dataformat.avro.schema; - -import java.util.Arrays; -import java.util.concurrent.TimeUnit; - -import org.apache.avro.Schema; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; - -import com.fasterxml.jackson.dataformat.avro.AvroMapper; -import com.fasterxml.jackson.dataformat.avro.AvroSchema; -import com.fasterxml.jackson.dataformat.avro.AvroTestBase; - -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 -public class SchemaNameClassInitTest extends AvroTestBase -{ - static class InitTracker { - static volatile boolean initialized; - static volatile boolean enumInitialized; - static volatile boolean fixedInitialized; - } - - // Never referenced by test code: only named by the schema below - static class NotToBeInitialized { - static { - InitTracker.initialized = true; - } - } - - // Likewise only named by ENUM / FIXED schemas - enum EnumNotToBeInitialized { - A, B; - - static { - InitTracker.enumInitialized = true; - } - } - - static class FixedNotToBeInitialized { - static { - InitTracker.fixedInitialized = true; - } - } - - static class Inner { - public String x; - } - - static class Outer { - public Inner value; - } - - private final AvroMapper MAPPER = newMapper(); - - @Test - public void testReadDoesNotInitializeClassNamedBySchema() throws Exception - { - AvroSchema schema = MAPPER.schemaFrom("{\"type\":\"record\",\"name\":\"Outer\",\"fields\":[" - +"{\"name\":\"value\",\"type\":{\"type\":\"record\",\"name\":\"NotToBeInitialized\"," - +"\"namespace\":\"" + SchemaNameClassInitTest.class.getName() + "\"," - +"\"fields\":[{\"name\":\"x\",\"type\":\"string\"}]}}]}"); - Outer result = MAPPER.readerFor(Outer.class) - .with(schema) - .readValue(new byte[] { 2, 'a' }); - assertEquals("a", result.value.x); - - // Name is still resolved to the nested class... - assertEquals(SchemaNameClassInitTest.class.getName() + "$NotToBeInitialized", - AvroSchemaHelper.getFullName(schema.getAvroSchema().getField("value").schema())); - // ... but without running its static initializer - assertFalse(InitTracker.initialized); - } - - @Test - public void testEnumSchemaDoesNotInitializeNamedClass() throws Exception - { - Schema schema = Schema.createEnum("EnumNotToBeInitialized", null, - SchemaNameClassInitTest.class.getName(), Arrays.asList("A", "B")); - assertEquals(SchemaNameClassInitTest.class.getName() + "$EnumNotToBeInitialized", - AvroSchemaHelper.getFullName(schema)); - assertFalse(InitTracker.enumInitialized); - } - - @Test - public void testFixedSchemaDoesNotInitializeNamedClass() throws Exception - { - Schema schema = Schema.createFixed("FixedNotToBeInitialized", null, - SchemaNameClassInitTest.class.getName(), 4); - assertEquals(SchemaNameClassInitTest.class.getName() + "$FixedNotToBeInitialized", - AvroSchemaHelper.getFullName(schema)); - assertFalse(InitTracker.fixedInitialized); - } - - // Namespace with more dots than any real class name must not be probed - // for nested classes (one class loading attempt per dot) - @Test - @Timeout(value = 10, unit = TimeUnit.SECONDS) - public void testNoClassProbingForExcessivelyDottedNamespace() throws Exception - { - StringBuilder sb = new StringBuilder("a"); - for (int i = 0; i < 20_000; ++i) { - sb.append(".a"); - } - final String namespace = sb.toString(); - Schema schema = Schema.createRecord("Rec", null, namespace, false); - 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); - Schema schema = Schema.createRecord("Rec", null, namespace, false); - assertEquals(namespace + ".Rec", AvroSchemaHelper.getFullName(schema)); - } - - private static String _longName(int len) { - StringBuilder sb = new StringBuilder(len); - while (sb.length() < len) { - sb.append('x'); - } - 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(); - } - - // But nesting within limit is still resolved - @Test - public void testNestedClassStillResolvedWithinDotLimit() throws Exception - { - Schema schema = Schema.createRecord("Inner", null, - SchemaNameClassInitTest.class.getName(), false); - assertEquals(SchemaNameClassInitTest.class.getName() + "$Inner", - AvroSchemaHelper.getFullName(schema)); - } -} diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index d300fa127..1aba950f3 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: nested classes now resolved by + `AvroTypeIdResolver` on deserialization (no class loading when processing schema); + `JsonParser.getTypeId()` exposes dotted name from schema + (fix by @cowtowncoder, w/ Claude code) + 2.23.1 (not yet released) #906: (avro) Do not initialize class named by Avro schema when resolving its full name;