-
Notifications
You must be signed in to change notification settings - Fork 678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Way to define fallback class when _class not found #3250
Comments
I suggest calling |
So I can subclass and override @Override
public <T> TypeInformation<? extends T> readType(final Bson source, final TypeInformation<T> basicType) {
final TypeInformation<? extends T> typeInformation = super.readType(source, basicType);
final Class<? extends T> rawType = typeInformation.getType();
final Class<?> overrideType = forceOverrideTypeMap.get(rawType);
if (overrideType != null) {
final Class<?> documentsTargetType = overrideType;
boolean isMoreConcreteCustomType = (rawType == null)
|| (rawType.isAssignableFrom(documentsTargetType) && !rawType.equals(documentsTargetType));
if (!isMoreConcreteCustomType) {
return basicType;
}
TypeInformation<?> targetType = TypeInformation.of(documentsTargetType);
return basicType.specialize(targetType);
}
return typeInformation;
} and then I can plug that in... works. just seems very verbose and clunky. @Configuration
public class CustomMongoConfig {
private final MappingMongoConverter mappingMongoConverter;
public CustomMongoConfig(final MappingMongoConverter mappingMongoConverter) {
this.mappingMongoConverter = mappingMongoConverter;
}
@PostConstruct
void initCustomMongoTypeMapper() {
final CustomMongoConfig typeMapper = new CustomMongoConfig(
DefaultMongoTypeMapper.DEFAULT_TYPE_KEY,
mappingMongoConverter.getMappingContext(),
mappingMongoConverter::getWriteTarget
);
mappingMongoConverter.setTypeMapper(typeMapper);
}
} |
Code in the comment above doesn't follow my suggestion of using We're aiming to provide functionality for a broad audience. If certain use-cases require special handling and those aren't super-common, then we are keep our API as-is. We could introduce |
oh sorry, I looked at above originaly, and it would work, but means we are looking up from source twice so seemed less efficient than checking the return type |
The accessor uses some caching, so duplicate lookups shouldn't be expensive. There are various optimizations, e. g. you could check for |
Ok we got that working, you're right less complexity. private static final Map<Class<?>, TypeInformation<?>> OVERRIDE_TYPE_MAP = new HashMap<>();
...
@Override
public <T> TypeInformation<? extends T> readType(final Bson source, final TypeInformation<T> basicType) {
final TypeInformation<? extends T> actualTypeToUse = getActualTypeToUse(source, basicType);
return super.readType(source, actualTypeToUse);
}
private <T> TypeInformation<? extends T> getActualTypeToUse(Bson source, TypeInformation<T> basicType) {
if (!isClassDefinedInSource(source)) {
final TypeInformation<?> overrideTypeToUse = OVERRIDE_TYPE_MAP.get(basicType.getType());
if (overrideTypeToUse != null) {
return basicType.specialize(overrideTypeToUse);
}
}
return basicType;
}
private boolean isClassDefinedInSource(final Bson source) {
final TypeInformation<?> typeFromSource = super.readType(source);
return typeFromSource != null;
} |
Alright, thanks for sharing. I'm closing the ticket here as there's nothing left to do for us. |
For a while we've had a class used in a lot of our mongo collections,
FieldChange
, which just recorded when a field changed. This is used in collections e.g.We can see in mongo that
_class
has not been written e.g. forExampleClass
in mongo:We now are trying to enrich our model, so
FieldChange
becomes an abstract class, and we have many variations, but what wasFieldChange
is nowSimpleFieldChange
.But now we cannot load the existing data from mongo
We've been investigating specifying details via annotations we assumed would work, e.g.
And a few variations on the fields as well as classes, but nothing seemed to work. We finally debugged, and can see that Mongo loads up items in the collection using
DefaultMongoTypeMapper
, which extendsDefaultTypeMapper
.can see this has method that starts:
effectively what would be nice is if
getDefaultedTypeToBeUsed(source)
had some way to then lookup bybasicType
if nothing in source was found for the mapping. No simple way to hook into this though, sincegetDefaultedTypeToBeUsed
only takes source.Would be nice if annotations on the
basicType
could be inspected for default implementation, this would seem to fit in with expectations.Otherwise if their was a pluggable way to define default mappings, would seem could be put into above block, e.g.
We're so unsure how to do this, so we're now looking to run a migration on all our data to add
_class
information, but feels like their should be a simpler way to configure this in code.The text was updated successfully, but these errors were encountered: