Skip to content
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

DTO joins support issues #2761

Draft
wants to merge 10 commits into
base: 4.10.x
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ default PersistentPropertyPath getPropertyPath(@NonNull String[] propertyPath) {
@NonNull
Optional<NamingStrategy> findNamingStrategy();

/**
* Obtains a PersistentProperty representing id or version property by name.
*
* @param name The name of the id or version property
* @return The PersistentProperty used as id or version or null if it doesn't exist
*/
PersistentProperty getIdOrVersionPropertyByName(String name);
radovanradic marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a new persistent entity representation of the given type. The type
* must be annotated with {@link io.micronaut.core.annotation.Introspected}. This method will create a new instance on demand and does not cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micronaut.core.beans.BeanProperty;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.Relation;
import io.micronaut.data.annotation.Transient;
Expand Down Expand Up @@ -366,6 +367,26 @@ public PersistentEntity getParentEntity() {
return null;
}

@Override
public RuntimePersistentProperty<T> getIdOrVersionPropertyByName(String name) {
if (ArrayUtils.isNotEmpty(identity)) {
RuntimePersistentProperty<T> persistentProp = Arrays.stream(identity)
radovanradic marked this conversation as resolved.
Show resolved Hide resolved
.filter(p -> p.getName().equals(name))
.findFirst()
.orElse(null);

if (persistentProp != null) {
return persistentProp;
}
}

if (version != null && version.getName().equals(name)) {
return version;
}

return null;
}

private boolean isEmbedded(BeanProperty bp) {
return bp.enumValue(Relation.class, Relation.Kind.class).orElse(null) == Relation.Kind.EMBEDDED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.micronaut.data.model.runtime
import io.micronaut.data.annotation.AutoPopulated
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.data.annotation.Version
import spock.lang.Specification

class RuntimePersistentEntitySpec extends Specification {
Expand All @@ -12,6 +13,10 @@ class RuntimePersistentEntitySpec extends Specification {
def rtpe = new RuntimePersistentEntity(Test)
expect:
rtpe.getPersistentPropertyNames().contains('id')
rtpe.getIdOrVersionPropertyByName('id') != null
rtpe.getIdOrVersionPropertyByName('name') == null
rtpe.getIdOrVersionPropertyByName('version') != null
rtpe.getIdOrVersionPropertyByName('name') == null
}

}
Expand All @@ -23,4 +28,7 @@ class Test {
UUID id

String name
}

@Version
Long version
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,7 @@ public SourcePersistentProperty getIdentityByName(String name) {
return (SourcePersistentProperty) super.getIdentityByName(name);
}

/**
* Obtains a PersistentProperty representing id or version property by name.
*
* @param name The name of the id or version property
* @return The PersistentProperty used as id or version or null if it doesn't exist
*/
@Override
public SourcePersistentProperty getIdOrVersionPropertyByName(String name) {
if (ArrayUtils.isNotEmpty(ids)) {
SourcePersistentProperty persistentProp = Arrays.stream(ids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,12 @@ private boolean isDtoType(ClassElement repositoryElement, ClassElement classElem
/**
* Find DTO properties.
*
* @param matchContext The method match context
* @param entity The entity
* @param returnType The result
* @return DTO properties
*/
protected List<SourcePersistentProperty> getDtoProjectionProperties(SourcePersistentEntity entity,
protected List<SourcePersistentProperty> getDtoProjectionProperties(MethodMatchContext matchContext, SourcePersistentEntity entity,
ClassElement returnType) {
return returnType.getBeanProperties().stream()
.filter(dtoProperty -> {
Expand Down Expand Up @@ -825,9 +826,17 @@ protected List<SourcePersistentProperty> getDtoProjectionProperties(SourcePersis
// Convert anything to a string or an object
return pp;
}
if (!TypeUtils.areTypesCompatible(dtoPropertyType, pp.getType())) {
boolean compatibleTypes = TypeUtils.areTypesCompatible(dtoPropertyType, pp.getType());
if (compatibleTypes) {
return pp;
}

// Check if these are compatible non-simple field types (kind of nested DTOs)
List<SourcePersistentProperty> props = getDtoProjectionProperties(matchContext, new SourcePersistentEntity(pp.getType(), matchContext::getEntity), dtoPropertyType);
if (props.isEmpty()) {
throw new MatchFailedException("Property [" + propertyName + "] of type [" + dtoPropertyType.getName() + "] is not compatible with equivalent property of type [" + pp.getType().getName() + "] declared in entity: " + entity.getName());
}

return pp;
}).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected MethodMatchInfo build(MethodMatchContext matchContext) {
);

if (result.isDto() && !result.isRuntimeDtoConversion()) {
List<SourcePersistentProperty> dtoProjectionProperties = getDtoProjectionProperties(matchContext.getRootEntity(), resultType);
List<SourcePersistentProperty> dtoProjectionProperties = getDtoProjectionProperties(matchContext, matchContext.getRootEntity(), resultType);
if (!dtoProjectionProperties.isEmpty()) {
List<Selection<?>> selectionList = dtoProjectionProperties.stream()
.map(p -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected MethodMatchInfo build(MethodMatchContext matchContext) {
);

if (result.isDto() && !result.isRuntimeDtoConversion()) {
List<SourcePersistentProperty> dtoProjectionProperties = getDtoProjectionProperties(matchContext.getRootEntity(), resultType);
List<SourcePersistentProperty> dtoProjectionProperties = getDtoProjectionProperties(matchContext, matchContext.getRootEntity(), resultType);
if (!dtoProjectionProperties.isEmpty()) {
Root<?> root = query.getRoots().iterator().next();
List<Selection<?>> selectionList = dtoProjectionProperties.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected MethodMatchInfo build(MethodMatchContext matchContext) {
);

if (result.isDto() && !result.isRuntimeDtoConversion()) {
List<SourcePersistentProperty> dtoProjectionProperties = getDtoProjectionProperties(matchContext.getRootEntity(), resultType);
List<SourcePersistentProperty> dtoProjectionProperties = getDtoProjectionProperties(matchContext, matchContext.getRootEntity(), resultType);
if (!dtoProjectionProperties.isEmpty()) {
List<Selection<?>> selectionList = dtoProjectionProperties.stream()
.map(p -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;
import java.util.UUID;
import java.util.function.BiFunction;

/**
Expand All @@ -71,6 +72,7 @@ public final class SqlResultEntityTypeMapper<RS, R> implements SqlTypeMapper<RS,
private final SqlJsonColumnReader<RS> jsonColumnReader;
private final DataConversionService conversionService;
private final BiFunction<RuntimePersistentEntity<Object>, Object, Object> eventListener;
private final boolean isDto;
private boolean callNext = true;

/**
Expand All @@ -86,8 +88,9 @@ public SqlResultEntityTypeMapper(
String prefix,
@NonNull RuntimePersistentEntity<R> entity,
@NonNull ResultReader<RS, String> resultReader,
@Nullable SqlJsonColumnReader<RS> jsonColumnReader, DataConversionService conversionService) {
this(entity, resultReader, Collections.emptySet(), prefix, jsonColumnReader, conversionService, null);
@Nullable SqlJsonColumnReader<RS> jsonColumnReader,
DataConversionService conversionService) {
this(entity, resultReader, Collections.emptySet(), prefix, jsonColumnReader, conversionService, null, false);
}

/**
Expand All @@ -104,7 +107,7 @@ public SqlResultEntityTypeMapper(
@NonNull ResultReader<RS, String> resultReader,
@Nullable Set<JoinPath> joinPaths,
@Nullable SqlJsonColumnReader<RS> jsonColumnReader, DataConversionService conversionService) {
this(entity, resultReader, joinPaths, null, jsonColumnReader, conversionService, null);
this(entity, resultReader, joinPaths, null, jsonColumnReader, conversionService, null, false);
}

/**
Expand All @@ -116,14 +119,17 @@ public SqlResultEntityTypeMapper(
* @param jsonColumnReader The json column reader
* @param loadListener The event listener
* @param conversionService The conversion service
* @param isDto Whether reading/mapping DTO projection
*/
public SqlResultEntityTypeMapper(
@NonNull RuntimePersistentEntity<R> entity,
@NonNull ResultReader<RS, String> resultReader,
@Nullable Set<JoinPath> joinPaths,
@Nullable SqlJsonColumnReader<RS> jsonColumnReader,
@Nullable BiFunction<RuntimePersistentEntity<Object>, Object, Object> loadListener, DataConversionService conversionService) {
this(entity, resultReader, joinPaths, null, jsonColumnReader, conversionService, loadListener);
@Nullable BiFunction<RuntimePersistentEntity<Object>, Object, Object> loadListener,
DataConversionService conversionService,
boolean isDto) {
this(entity, resultReader, joinPaths, null, jsonColumnReader, conversionService, loadListener, isDto);
}

/**
Expand All @@ -136,14 +142,17 @@ public SqlResultEntityTypeMapper(
* @param jsonColumnReader The json column reader
* @param eventListener The event listener used for trigger post load if configured
* @param conversionService The conversion service
* @param isDto Whether reading/mapping DTO projection
*/
private SqlResultEntityTypeMapper(
@NonNull RuntimePersistentEntity<R> entity,
@NonNull ResultReader<RS, String> resultReader,
@Nullable Set<JoinPath> joinPaths,
String startingPrefix,
@Nullable SqlJsonColumnReader<RS> jsonColumnReader,
DataConversionService conversionService, @Nullable BiFunction<RuntimePersistentEntity<Object>, Object, Object> eventListener) {
DataConversionService conversionService,
@Nullable BiFunction<RuntimePersistentEntity<Object>, Object, Object> eventListener,
boolean isDto) {
this.conversionService = conversionService;
ArgumentUtils.requireNonNull("entity", entity);
ArgumentUtils.requireNonNull("resultReader", resultReader);
Expand All @@ -160,6 +169,7 @@ private SqlResultEntityTypeMapper(
this.joinPaths = Collections.emptyMap();
}
this.startingPrefix = startingPrefix;
this.isDto = isDto;
}

@Override
Expand Down Expand Up @@ -622,6 +632,11 @@ private <K> K triggerPostLoad(RuntimePersistentEntity<?> persistentEntity, K ent
private <K> Object readEntityId(RS rs, MappingContext<K> ctx) {
RuntimePersistentProperty<K> identity = ctx.persistentEntity.getIdentity();
if (identity == null) {
// DTO might not have ID mapped, and in this case to maintain relation
// we set random UUID as id to be able to read and make relation
if (isDto) {
return UUID.randomUUID();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this will work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is a test that covers that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, don't know if could break some user case, all tests we have are passing.

}
return null;
}
if (identity instanceof Embedded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,8 @@ protected <E, R> SqlTypeMapper<RS, R> createMapper(SqlStoredQuery<E, R> prepared
joinFetchPaths,
sqlJsonColumnMapperProvider.getJsonColumnReader(preparedQuery, rsType),
loadListener,
conversionService);
conversionService,
false);
} else {
RuntimePersistentEntity<R> resultPersistentEntity = getEntity(resultType);
Collection<BeanProperty<R, Object>> beanProperties = resultPersistentEntity.getIntrospection().getBeanProperties();
Expand All @@ -738,7 +739,14 @@ protected <E, R> SqlTypeMapper<RS, R> createMapper(SqlStoredQuery<E, R> prepared
return p;
}
RuntimePersistentProperty<E> entityProperty = persistentEntity.getPropertyByName(p.getName());
if (entityProperty == null || !ReflectionUtils.getWrapperType(entityProperty.getType()).equals(ReflectionUtils.getWrapperType(p.getType()))) {
if (entityProperty == null) {
return p;
}
Class<?> dtoPropertyType = ReflectionUtils.getWrapperType(p.getType());
Class<?> entityPropertyType = ReflectionUtils.getWrapperType(entityProperty.getType());
if (!dtoPropertyType.equals(entityPropertyType)
&& !TypeUtils.areTypesCompatible(dtoPropertyType, entityPropertyType)
&& !TypeUtils.isDtoCompatibleWithEntity(dtoPropertyType, entityPropertyType)) {
return p;
}
return new BeanPropertyWithAnnotationMetadata<>(
Expand All @@ -753,7 +761,8 @@ protected <E, R> SqlTypeMapper<RS, R> createMapper(SqlStoredQuery<E, R> prepared
joinFetchPaths,
sqlJsonColumnMapperProvider.getJsonColumnReader(preparedQuery, rsType),
null,
conversionService);
conversionService,
true);
}
}
return new SqlTypeMapper<>() {
Expand Down
Loading
Loading