Skip to content

Commit 6510ef4

Browse files
committed
#29 - Fix Annotated Fields - extract FieldReader.trimAnnotations() and add unit and component tests
1 parent c6ffe7e commit 6510ef4

File tree

5 files changed

+86
-11
lines changed

5 files changed

+86
-11
lines changed

blackbox-test/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
<dependencies>
2222

23+
<!-- for testing fields with third party annotations -->
24+
<dependency>
25+
<groupId>javax.validation</groupId>
26+
<artifactId>validation-api</artifactId>
27+
<version>2.0.1.Final</version>
28+
</dependency>
29+
2330
<dependency>
2431
<groupId>io.avaje</groupId>
2532
<artifactId>avaje-jsonb</artifactId>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.example.customer;
2+
3+
import io.avaje.jsonb.Json;
4+
5+
import javax.validation.constraints.NotNull;
6+
import javax.validation.constraints.Size;
7+
8+
@Json
9+
public class WithAnnotations {
10+
11+
@Size(max = 50)
12+
final String one;
13+
14+
@NotNull @Size(min = 5, max = 10)
15+
final String two;
16+
17+
public WithAnnotations(String one, String two) {
18+
this.one = one;
19+
this.two = two;
20+
}
21+
22+
public String one() {
23+
return one;
24+
}
25+
26+
public String two() {
27+
return two;
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example.customer;
2+
3+
import io.avaje.jsonb.Jsonb;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class WithAnnotationsTest {
9+
10+
Jsonb jsonb = Jsonb.builder().build();
11+
12+
@Test
13+
void toJsonFromJson() {
14+
var bean = new WithAnnotations("foo","bar");
15+
String asJson = jsonb.toJson(bean);
16+
assertThat(asJson).isEqualTo("{\"one\":\"foo\",\"two\":\"bar\"}");
17+
18+
WithAnnotations fromJson = jsonb.type(WithAnnotations.class).fromJson(asJson);
19+
20+
assertThat(fromJson.one()).isEqualTo("foo");
21+
assertThat(fromJson.two()).isEqualTo("bar");
22+
}
23+
24+
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/FieldReader.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,8 @@ class FieldReader {
3333
addSubType(subType);
3434
this.fieldName = element.getSimpleName().toString();
3535
this.propertyName = PropertyReader.name(namingConvention, fieldName, element);
36-
3736
this.publicField = element.getModifiers().contains(Modifier.PUBLIC);
38-
39-
final String type = element.asType().toString();
40-
if (type.contains("@")) {
41-
final String[] split = type.split(" ");
42-
43-
this.rawType = type.substring(0, type.indexOf("@")) + split[split.length - 1];
44-
} else {
45-
this.rawType = type;
46-
}
37+
this.rawType = trimAnnotations(element.asType().toString());
4738

4839
final PropertyIgnoreReader ignoreReader = new PropertyIgnoreReader(element);
4940
this.unmapped = ignoreReader.unmapped();
@@ -73,6 +64,14 @@ class FieldReader {
7364
}
7465
}
7566

67+
static String trimAnnotations(String type) {
68+
int pos = type.indexOf("@");
69+
if (pos == -1) {
70+
return type;
71+
}
72+
return type.substring(0, pos) + type.substring(type.lastIndexOf(' ') + 1);
73+
}
74+
7675
void position(int pos) {
7776
position = pos;
7877
}
@@ -160,7 +159,7 @@ void writeDebug(Append writer) {
160159
}
161160
if (!deserialize) {
162161
writer.append(" ignoreDeserialize");
163-
}else if (constructorParam) {
162+
} else if (constructorParam) {
164163
writer.append(" constructor");
165164
} else if (setter != null) {
166165
writer.append(" setter:%s ", setter);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.avaje.jsonb.generator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class FieldReaderTest {
8+
9+
@Test
10+
void trimAnnotations() {
11+
assertEquals("java.lang.String", FieldReader.trimAnnotations("java.lang.String"));
12+
assertEquals("java.lang.String", FieldReader.trimAnnotations("java.lang.@javax.validation.constraints.Email String"));
13+
assertEquals("java.lang.String", FieldReader.trimAnnotations("java.lang.@javax.validation.constraints.NotNull,@javax.validation.constraints.Size(min=2, max=150) String"));
14+
assertEquals("java.lang.String", FieldReader.trimAnnotations("java.lang.@javax.validation.constraints.Email,@javax.validation.constraints.Size(max=100) String"));
15+
}
16+
}

0 commit comments

Comments
 (0)