Skip to content

Commit 6529baa

Browse files
committed
Fix the mapping bug
* Fix the mapping bug related to enum database types which are located in the schema not in the search path. * Add additional logging and integration tests for the use case when the schema is not in the search path. * Cleanup the Java 8 code
1 parent b36942f commit 6529baa

File tree

20 files changed

+347
-95
lines changed

20 files changed

+347
-95
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
RESET role;
2+
3+
CREATE SCHEMA ztest_schema3
4+
AUTHORIZATION zalando_api_owner;
5+
6+
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA ztest_schema3 REVOKE EXECUTE ON FUNCTIONS FROM public;
7+
ALTER DEFAULT PRIVILEGES FOR ROLE zalando_api_owner IN SCHEMA ztest_schema3 REVOKE EXECUTE ON FUNCTIONS FROM public;
8+
ALTER DEFAULT PRIVILEGES IN SCHEMA ztest_schema3 REVOKE EXECUTE ON FUNCTIONS FROM public;
9+
10+
ALTER DEFAULT PRIVILEGES FOR ROLE zalando_api_owner IN SCHEMA ztest_schema3 GRANT EXECUTE ON FUNCTIONS TO zalando_api_executor;
11+
12+
GRANT USAGE ON SCHEMA ztest_schema3 TO zalando_api_usage;
13+
14+
SET ROLE TO zalando_api_owner;
15+
16+
SET search_path TO ztest_schema3, public;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TYPE color AS ENUM (
2+
'RED',
3+
'BLUE',
4+
'GREEN',
5+
'ORANGE'
6+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TYPE CAR AS
2+
(
3+
id BIGINT,
4+
brand TEXT,
5+
color ztest_schema3.COLOR
6+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TYPE CUSTOMER_ADDRESS AS
2+
(
3+
street VARCHAR,
4+
house_number INT
5+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TYPE CUSTOMER AS
2+
(
3+
id BIGINT,
4+
name TEXT,
5+
address ztest_schema3.CUSTOMER_ADDRESS
6+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE OR REPLACE FUNCTION get_all_cars()
2+
RETURNS SETOF ztest_schema3.CAR AS
3+
$body$
4+
BEGIN
5+
RETURN QUERY SELECT t.id, t.brand,
6+
t.color
7+
FROM (VALUES (1::BIGINT, 'Toyota'::TEXT, 'RED'::ztest_schema3.COLOR),
8+
(2::BIGINT, 'Mercedes'::TEXT, 'BLUE'::ztest_schema3.COLOR))
9+
AS t (id, brand, color);
10+
END;
11+
$body$
12+
LANGUAGE plpgsql
13+
SECURITY DEFINER
14+
COST 100;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE OR REPLACE FUNCTION get_all_customers()
2+
RETURNS SETOF ztest_schema3.CUSTOMER AS
3+
$body$
4+
BEGIN
5+
RETURN QUERY SELECT t.id, t.name,
6+
t.address
7+
FROM (VALUES (1::BIGINT, 'John'::TEXT, ROW('First street', 15)::ztest_schema3.CUSTOMER_ADDRESS))
8+
AS t (id, name, address);
9+
END;
10+
$body$
11+
LANGUAGE plpgsql
12+
SECURITY DEFINER
13+
COST 100;

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959

6060
<properties>
6161
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
62-
<maven.compiler.source>1.8</maven.compiler.source>
63-
<maven.compiler.target>1.8</maven.compiler.target>
62+
<maven.compiler.source>11</maven.compiler.source>
63+
<maven.compiler.target>11</maven.compiler.target>
6464
<spring.version>5.2.12.RELEASE</spring.version>
6565
<postgresql.version>42.2.14</postgresql.version>
6666
</properties>
@@ -434,6 +434,7 @@
434434
CREATE EXTENSION hstore;
435435
DROP SCHEMA IF EXISTS ztest_schema1 CASCADE;
436436
DROP SCHEMA IF EXISTS ztest_schema2 CASCADE;
437+
DROP SCHEMA IF EXISTS ztest_schema3 CASCADE;
437438
</sqlCommand>
438439
<onError>continue</onError>
439440
</configuration>

src/main/java/org/zalando/typemapper/core/TypeMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public ITEM mapRow(final ResultSet set, final int count) throws SQLException {
7171
}
7272

7373
private ResultTree extractResultTree(final ResultSet set) throws SQLException {
74-
LOG.trace("Extracting result tree");
7574

7675
// cast to obtain more information from the result set.
7776
final PgResultSet pgSet = set.unwrap(PgResultSet.class);
@@ -100,7 +99,7 @@ private ResultTree extractResultTree(final ResultSet set) throws SQLException {
10099
int j = 1;
101100
for (final String fieldValue : fieldValues) {
102101
final DbTypeField fieldDef = function.getFieldByPos(j);
103-
DbResultNode currentNode = null;
102+
DbResultNode currentNode;
104103
if (fieldDef.getType().equals("USER-DEFINED")) {
105104
currentNode = new ObjectResultNode(fieldValue, fieldDef.getName(), fieldDef.getTypeName(),
106105
fieldDef.getTypeId(), pgSet.getStatement().getConnection());
@@ -141,7 +140,7 @@ private ResultTree extractResultTree(final ResultSet set) throws SQLException {
141140
tree.addChild(node);
142141
}
143142

144-
LOG.trace("Extracted ResultTree: {}", tree);
143+
LOG.debug("Extracted ResultTree: {}", tree);
145144

146145
return tree;
147146
}

src/main/java/org/zalando/typemapper/core/fieldMapper/ObjectFieldMapper.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
package org.zalando.typemapper.core.fieldMapper;
22

3-
import static org.zalando.typemapper.postgres.PgTypeHelper.getDatabaseFieldDescriptor;
4-
5-
import java.lang.reflect.InvocationTargetException;
6-
7-
import java.util.List;
8-
93
import org.slf4j.Logger;
104
import org.slf4j.LoggerFactory;
11-
12-
135
import org.zalando.typemapper.core.DatabaseFieldDescriptor;
146
import org.zalando.typemapper.core.Mapping;
157
import org.zalando.typemapper.core.result.ArrayResultNode;
@@ -18,6 +10,11 @@
1810
import org.zalando.typemapper.core.result.ObjectResultNode;
1911
import org.zalando.typemapper.exception.NotsupportedTypeException;
2012

13+
import java.lang.reflect.InvocationTargetException;
14+
import java.util.List;
15+
16+
import static org.zalando.typemapper.postgres.PgTypeHelper.*;
17+
2118
public class ObjectFieldMapper {
2219

2320
private static final Logger LOG = LoggerFactory.getLogger(ObjectFieldMapper.class);
@@ -34,9 +31,10 @@ private static boolean isRowWithAllFieldsNull(final ObjectResultNode node) {
3431
return true;
3532
}
3633

37-
public static final Object mapFromDbObjectNode(final Class classz, final ObjectResultNode node,
38-
final Mapping mapping) throws InstantiationException, IllegalAccessException, IllegalArgumentException,
39-
InvocationTargetException, NotsupportedTypeException, NoSuchMethodException, SecurityException {
34+
public static Object mapFromDbObjectNode(final Class classz, final ObjectResultNode node,
35+
final Mapping mapping) throws InstantiationException,
36+
IllegalAccessException, IllegalArgumentException,
37+
InvocationTargetException, NotsupportedTypeException, NoSuchMethodException, SecurityException {
4038
final DatabaseFieldDescriptor descriptor = getDatabaseFieldDescriptor(mapping.getField());
4139
final Object value;
4240

@@ -53,18 +51,26 @@ public static final Object mapFromDbObjectNode(final Class classz, final ObjectR
5351
}
5452

5553
@SuppressWarnings("unchecked")
56-
public static final Object mapField(@SuppressWarnings("rawtypes") final Class clazz, final ObjectResultNode node)
57-
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
54+
public static Object mapField(@SuppressWarnings("rawtypes") final Class clazz, final ObjectResultNode node)
55+
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
5856
NotsupportedTypeException, SecurityException, NoSuchMethodException {
5957
if (node.getChildren() == null) {
6058
return null;
6159
}
6260

63-
Object result = null;
61+
Object result;
6462

6563
// instantiate enums by string value
6664
if (clazz.isEnum()) {
67-
final DbResultNode currentNode = node.getChildByName(node.getType());
65+
final DbResultNode currentNode = node.getChildByName(node.getName());
66+
if (currentNode == null) {
67+
final String error =
68+
String.format(
69+
"Can not map the enum class \"%s\" to the database field \"%s\" of type \"%s\". Enum value is not found.",
70+
clazz.getName(),
71+
node.getName(), node.getType());
72+
throw new IllegalStateException(error);
73+
}
6874
result = Enum.valueOf(clazz, currentNode.getValue());
6975
} else {
7076
result = clazz.getDeclaredConstructor().newInstance();
@@ -87,16 +93,15 @@ public static final Object mapField(@SuppressWarnings("rawtypes") final Class cl
8793
try {
8894
if (DbResultNodeType.SIMPLE.equals(currentNode.getNodeType())) {
8995
mapping.map(result,
90-
mapping.getFieldMapper().mapField(currentNode.getValue(), mapping.getFieldClass()));
96+
mapping.getFieldMapper().mapField(currentNode.getValue(), mapping.getFieldClass()));
9197
} else if (DbResultNodeType.OBJECT.equals(currentNode.getNodeType())) {
9298
mapping.map(result, mapFromDbObjectNode(clazz, (ObjectResultNode) currentNode, mapping));
9399
} else if (DbResultNodeType.ARRAY.equals(currentNode.getNodeType())) {
94100
mapping.map(result,
95-
ArrayFieldMapper.mapField(mapping.getField(), (ArrayResultNode) currentNode));
101+
ArrayFieldMapper.mapField(mapping.getField(), (ArrayResultNode) currentNode));
96102
}
97103
} catch (final Exception e) {
98-
LOG.error("Failed to map property {} of class {}",
99-
new Object[] {mapping.getName(), clazz.getSimpleName(), e});
104+
LOG.error("Failed to map property {} of class {}", mapping.getName(), clazz.getSimpleName(), e);
100105
}
101106
}
102107
}

0 commit comments

Comments
 (0)