You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This StackOverflow question got me curious about how to control conversion logic using an annotation.
I started digging around ConversionService and MappingMongoConverter code looking for a solution. I eventually landed on ConditionalGenericConverter, which I thought would let me solve conditional conversion using an annotation. The code to convert a Long to a Date based on a @Foo annotation would have looked like this...
public class ConditionalLongToDateConverter implements ConditionalGenericConverter {
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return sourceType.hasAnnotation(Foo.class);
}
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Long.class, Date.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
return new Date((Long) source);
}
}
which would have allowed the persistence of a POJO that looked like
public class MyDocument {
@Id
private String id;
@Foo
private Long timestamp; // gets persisted as a JS Date
}
I assumed the sourceTypeTypeDescriptor for property timestamp would contain the @Foo annotation, because of this constructor:
/**
* Create a new type descriptor from a {@link Property}.
* <p>Use this constructor when a source or target conversion point is a
* property on a Java class.
* @param property the property
*/
public TypeDescriptor(Property property) {
Assert.notNull(property, "Property must not be null");
this.resolvableType = ResolvableType.forMethodParameter(property.getMethodParameter());
this.type = this.resolvableType.resolve(property.getType());
this.annotations = nullSafeAnnotations(property.getAnnotations());
}
I registered the converter using CustomConversions from an AbstractMongoConfiguration subclass and thought I was done. But it all broke.
The problem is that MappingMongoConverter never really gets its hands on a TypeDescriptor for the properties to convert, and calls the GenericConversionService in a way that effectively discards the metadata of those properties. This happens specifically in getPotentiallyConvertedSimpleWrite(), but the code higher up the call stack doesn't help because it doesn't give that method TypeDescriptor arguments to work with either.
Is this intentional? I don't know how rare this use case is, but it seems like there's a lot of capability in Spring Core's conversion code that could be exposed to support smarter conversion if TypeDescriptors are propagated better
Affects: 1.4.1 (Codd SR1)
Issue Links:
DATACMNS-1036 Add API for property specific converters
DATAMONGO-2328 Add missing type converters for native target type conversion
4 votes, 5 watchers
The text was updated successfully, but these errors were encountered:
Emerson Farrugia opened DATAMONGO-889 and commented
This StackOverflow question got me curious about how to control conversion logic using an annotation.
I started digging around
ConversionService
andMappingMongoConverter
code looking for a solution. I eventually landed onConditionalGenericConverter
, which I thought would let me solve conditional conversion using an annotation. The code to convert aLong
to aDate
based on a@Foo
annotation would have looked like this...which would have allowed the persistence of a POJO that looked like
I assumed the
sourceType
TypeDescriptor
for propertytimestamp
would contain the@Foo
annotation, because of this constructor:I registered the converter using
CustomConversions
from anAbstractMongoConfiguration
subclass and thought I was done. But it all broke.The problem is that
MappingMongoConverter
never really gets its hands on aTypeDescriptor
for the properties to convert, and calls theGenericConversionService
in a way that effectively discards the metadata of those properties. This happens specifically ingetPotentiallyConvertedSimpleWrite()
, but the code higher up the call stack doesn't help because it doesn't give that methodTypeDescriptor
arguments to work with either.Is this intentional? I don't know how rare this use case is, but it seems like there's a lot of capability in Spring Core's conversion code that could be exposed to support smarter conversion if
TypeDescriptors
are propagated betterAffects: 1.4.1 (Codd SR1)
Issue Links:
DATACMNS-1036 Add API for property specific converters
DATAMONGO-2328 Add missing type converters for native target type conversion
4 votes, 5 watchers
The text was updated successfully, but these errors were encountered: