Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,6 +41,24 @@ public class AvroSchema implements FormatSchema
*/
protected final AtomicReference<AvroStructureReader> _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<ClassLoader, ResolvedTypeIds> _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;
Expand Down Expand Up @@ -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}).
*<p>
* 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<ClassLoader> _loader;

private final ConcurrentHashMap<String, String> _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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ 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<Object> 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");
return null;
}
return deser.deserialize(p, ctxt);
}
return _deserializeWithNativeTypeId(p, ctxt, p.getTypeId());
return _deserializeWithNativeTypeId(p, ctxt, typeId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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}).
*<p>
* 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<package.Outer.Inner>}),
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
Loading