Skip to content

Commit 45abe47

Browse files
4.x: Fix DbClient JSON mapping
- DbColumn.get() overrides Value.get() using as() instead of as().get() thus it returns an instance of Value instead of the raw value. - Add a unit test in dbclient/jsonp Fixes helidon-io#7842
1 parent 54dade6 commit 45abe47

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

dbclient/dbclient/src/main/java/io/helidon/dbclient/DbColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public interface DbColumn extends Value<Object> {
6060
*/
6161
@Override
6262
default Object get() {
63-
return as(javaType());
63+
return as(javaType()).get();
6464
}
6565

6666
/**

dbclient/jsonp/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
<artifactId>helidon-common-features-api</artifactId>
4444
<optional>true</optional>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-api</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.hamcrest</groupId>
53+
<artifactId>hamcrest-all</artifactId>
54+
<scope>test</scope>
55+
</dependency>
4656
</dependencies>
4757

4858
<build>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.dbclient.jsonp;
17+
18+
import io.helidon.common.mapper.MapperManager;
19+
import io.helidon.dbclient.DbColumn;
20+
import io.helidon.dbclient.DbColumnBase;
21+
import io.helidon.dbclient.DbMapperManager;
22+
import io.helidon.dbclient.DbRow;
23+
import io.helidon.dbclient.DbRowBase;
24+
25+
import jakarta.json.JsonObject;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.hamcrest.Matchers.is;
30+
31+
/**
32+
* Tests {@link io.helidon.dbclient.jsonp.JsonProcessingMapper}.
33+
*/
34+
class JsonProcessingMapperTest {
35+
36+
private static final DbMapperManager DB_MAPPER_MANAGER = DbMapperManager.builder()
37+
.addMapperProvider(new JsonProcessingMapperProvider())
38+
.build();
39+
40+
@Test
41+
void testRowAsJsonObject() {
42+
DbRow row = row(column("foo", "bar"), column("bob", "alice"));
43+
JsonObject jsonObject = row.as(JsonObject.class);
44+
assertThat(jsonObject.getString("foo"), is("bar"));
45+
}
46+
47+
private static DbRow row(DbColumnBase... columns) {
48+
return new DbRowBase(columns, DB_MAPPER_MANAGER) {
49+
@Override
50+
public DbColumn column(String name) {
51+
return super.column(name);
52+
}
53+
};
54+
}
55+
56+
private static DbColumnBase column(String name, Object value) {
57+
return new DbColumnBase(value, MapperManager.create()) {
58+
59+
@Override
60+
public Class<?> javaType() {
61+
return value.getClass();
62+
}
63+
64+
@Override
65+
public String dbType() {
66+
return "test";
67+
}
68+
69+
@Override
70+
public String name() {
71+
return name;
72+
}
73+
};
74+
}
75+
}

0 commit comments

Comments
 (0)