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

Support generic query type mappings in TypeMapping (fixes #2561) #2562

Merged
merged 1 commit into from Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -33,6 +33,8 @@
*/
public abstract class TypeMappings {

private final Map<Type, Type> genericQueryTypes = new HashMap<Type, Type>();

private final Map<String, Type> queryTypes = new HashMap<String, Type>();

private final Map<TypeCategory, Type> exprTypes
Expand All @@ -57,7 +59,9 @@ public Type getExprType(Type type, EntityType model, boolean raw) {
}

public Type getExprType(Type type, EntityType model, boolean raw, boolean rawParameters, boolean extend) {
if (queryTypes.containsKey(type.getFullName())) {
if (genericQueryTypes.containsKey(type)) {
return genericQueryTypes.get(type);
} else if (queryTypes.containsKey(type.getFullName())) {
return queryTypes.get(type.getFullName());
} else {
return getQueryType(exprTypes, type, model, raw, rawParameters, extend);
Expand All @@ -69,7 +73,9 @@ public Type getPathType(Type type, EntityType model, boolean raw) {
}

public Type getPathType(Type type, EntityType model, boolean raw, boolean rawParameters, boolean extend) {
if (queryTypes.containsKey(type.getFullName())) {
if (genericQueryTypes.containsKey(type)) {
return genericQueryTypes.get(type);
} else if (queryTypes.containsKey(type.getFullName())) {
return queryTypes.get(type.getFullName());
} else {
return getQueryType(pathTypes, type, model, raw, rawParameters, extend);
Expand Down Expand Up @@ -121,9 +127,11 @@ public void register(TypeCategory category,

public void register(Type type, Type queryType) {
queryTypes.put(type.getFullName(), queryType);
genericQueryTypes.put(type, queryType);
}

public boolean isRegistered(Type type) {
return queryTypes.containsKey(type.getFullName());
}

}
Expand Up @@ -16,11 +16,15 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.mysema.codegen.model.SimpleType;
import org.junit.Test;

import com.mysema.codegen.model.ClassType;
import com.mysema.codegen.model.Type;

import java.util.Collections;
import java.util.List;

public class TypeMappingsTest {

static class Entity { }
Expand All @@ -41,7 +45,23 @@ public void isRegistered() {
TypeMappings typeMappings = new JavaTypeMappings();
typeMappings.register(new ClassType(Double[].class), new ClassType(Point.class));
assertTrue(typeMappings.isRegistered(new ClassType(Double[].class)));
}

@Test
public void testGenericTypeRegistration() {
SimpleType rawListType = new SimpleType(List.class.getName());
SimpleType integerListType = new SimpleType(rawListType, Collections.<Type> singletonList(new SimpleType(Integer.class.getName())));
SimpleType longListType = new SimpleType(rawListType, Collections.<Type> singletonList(new SimpleType(Long.class.getName())));

SimpleType integerListTypeExpression = new SimpleType("integerListTypeExpression");
SimpleType longListTypeExpression = new SimpleType("longListTypeExpression");

TypeMappings typeMappings = new JavaTypeMappings();
typeMappings.register(integerListType, integerListTypeExpression);
typeMappings.register(longListType, longListTypeExpression);

assertEquals(integerListTypeExpression, typeMappings.getExprType(integerListType, null, false));
assertEquals(longListTypeExpression, typeMappings.getExprType(longListType, null, false));
}

}