11package io .smallrye .jwt .auth .cdi ;
22
33import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotNull ;
45
56import java .lang .annotation .Annotation ;
67import java .lang .reflect .Member ;
2223import javax .enterprise .inject .spi .PassivationCapable ;
2324import javax .enterprise .util .AnnotationLiteral ;
2425import javax .inject .Inject ;
26+ import javax .json .bind .JsonbBuilder ;
2527
2628import org .eclipse .microprofile .jwt .Claim ;
2729import org .eclipse .microprofile .jwt .ClaimValue ;
3436import org .junit .Test ;
3537
3638import io .smallrye .converters .SmallRyeConvertersBuilder ;
39+ import io .smallrye .converters .api .Converter ;
3740import io .smallrye .converters .api .Converters ;
3841import io .smallrye .jwt .KeyUtils ;
3942import 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}
0 commit comments