Skip to content

Commit f0507f3

Browse files
committed
#211 - Convert complex type.
1 parent 29fb53c commit f0507f3

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

implementation/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,18 @@
120120
<dependency>
121121
<groupId>io.smallrye.converters</groupId>
122122
<artifactId>smallrye-converters</artifactId>
123-
<version>1.0.0-SNAPSHOT</version>
123+
<version>1.0.0</version>
124+
<scope>test</scope>
125+
</dependency>
126+
<dependency>
127+
<groupId>jakarta.json.bind</groupId>
128+
<artifactId>jakarta.json.bind-api</artifactId>
129+
<scope>test</scope>
130+
</dependency>
131+
<dependency>
132+
<groupId>org.eclipse</groupId>
133+
<artifactId>yasson</artifactId>
134+
<version>1.0.6</version>
124135
<scope>test</scope>
125136
</dependency>
126137
</dependencies>

implementation/src/test/java/io/smallrye/jwt/auth/cdi/ClaimConverterTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.smallrye.jwt.auth.cdi;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
45

56
import java.lang.annotation.Annotation;
67
import java.lang.reflect.Member;
@@ -22,6 +23,7 @@
2223
import javax.enterprise.inject.spi.PassivationCapable;
2324
import javax.enterprise.util.AnnotationLiteral;
2425
import javax.inject.Inject;
26+
import javax.json.bind.JsonbBuilder;
2527

2628
import org.eclipse.microprofile.jwt.Claim;
2729
import org.eclipse.microprofile.jwt.ClaimValue;
@@ -34,6 +36,7 @@
3436
import org.junit.Test;
3537

3638
import io.smallrye.converters.SmallRyeConvertersBuilder;
39+
import io.smallrye.converters.api.Converter;
3740
import io.smallrye.converters.api.Converters;
3841
import io.smallrye.jwt.KeyUtils;
3942
import io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal;
@@ -61,6 +64,7 @@ public class ClaimConverterTest {
6164
.addBeans(new ClaimInjectionBean<>(Double.class))
6265
.addBeans(new ClaimInjectionBean<>(Boolean.class))
6366
.addBeans(new ClaimInjectionBean<>(Character.class))
67+
.addBeans(new ClaimInjectionBean<>(Address.class))
6468
.inject(this)
6569
.build();
6670

@@ -89,6 +93,14 @@ public void convertRawWrapperTypes() {
8993
assertEquals(true, raw.getBooleanClaim());
9094
}
9195

96+
@Test
97+
public void convertComplexType() {
98+
final Address address = raw.getAddress();
99+
assertNotNull(address);
100+
assertEquals("street", address.getStreet());
101+
assertEquals(1000, address.getCode().intValue());
102+
}
103+
92104
@Produces
93105
@RequestScoped
94106
private static JsonWebToken jwt() throws Exception {
@@ -126,6 +138,9 @@ private static class RawConverterBean {
126138
@Inject
127139
@Claim("long")
128140
private Long longClaim;
141+
@Inject
142+
@Claim("address")
143+
private Address address;
129144

130145
String getName() {
131146
return name;
@@ -158,6 +173,10 @@ public Boolean getBooleanClaim() {
158173
public Long getLongClaim() {
159174
return longClaim;
160175
}
176+
177+
public Address getAddress() {
178+
return address;
179+
}
161180
}
162181

163182
@RequestScoped
@@ -177,7 +196,11 @@ private static class ClaimInjectionBean<T> implements Bean<T>, PassivationCapabl
177196

178197
public ClaimInjectionBean(final Class klass) {
179198
this.klass = klass;
180-
this.converters = new SmallRyeConvertersBuilder().build();
199+
this.converters = new SmallRyeConvertersBuilder()
200+
.withConverter(Address.class, 100,
201+
// Jsonb does not support JsonObject to POJO conversion. You need to call toString on it.
202+
(Converter<Address>) value -> JsonbBuilder.create().fromJson(value, Address.class))
203+
.build();
181204
}
182205

183206
@Override
@@ -321,4 +344,25 @@ public Claims standard() {
321344
return INSTANCE.standard();
322345
}
323346
}
347+
348+
public static class Address {
349+
private String street;
350+
private Integer code;
351+
352+
public String getStreet() {
353+
return street;
354+
}
355+
356+
public void setStreet(final String street) {
357+
this.street = street;
358+
}
359+
360+
public Integer getCode() {
361+
return code;
362+
}
363+
364+
public void setCode(final Integer code) {
365+
this.code = code;
366+
}
367+
}
324368
}

implementation/src/test/resources/token-converter.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@
1616
"float": 99.9,
1717
"double": 99.99,
1818
"boolean": "true",
19-
"char": "y"
19+
"char": "y",
20+
21+
"address": {
22+
"street": "street",
23+
"code": 1000
24+
}
2025
}

0 commit comments

Comments
 (0)