Skip to content

Commit

Permalink
#211 - Convert complex type.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Apr 8, 2020
1 parent 29fb53c commit f0507f3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
13 changes: 12 additions & 1 deletion implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,18 @@
<dependency>
<groupId>io.smallrye.converters</groupId>
<artifactId>smallrye-converters</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.6</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.smallrye.jwt.auth.cdi;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
Expand All @@ -22,6 +23,7 @@
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;
import javax.json.bind.JsonbBuilder;

import org.eclipse.microprofile.jwt.Claim;
import org.eclipse.microprofile.jwt.ClaimValue;
Expand All @@ -34,6 +36,7 @@
import org.junit.Test;

import io.smallrye.converters.SmallRyeConvertersBuilder;
import io.smallrye.converters.api.Converter;
import io.smallrye.converters.api.Converters;
import io.smallrye.jwt.KeyUtils;
import io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal;
Expand Down Expand Up @@ -61,6 +64,7 @@ public class ClaimConverterTest {
.addBeans(new ClaimInjectionBean<>(Double.class))
.addBeans(new ClaimInjectionBean<>(Boolean.class))
.addBeans(new ClaimInjectionBean<>(Character.class))
.addBeans(new ClaimInjectionBean<>(Address.class))
.inject(this)
.build();

Expand Down Expand Up @@ -89,6 +93,14 @@ public void convertRawWrapperTypes() {
assertEquals(true, raw.getBooleanClaim());
}

@Test
public void convertComplexType() {
final Address address = raw.getAddress();
assertNotNull(address);
assertEquals("street", address.getStreet());
assertEquals(1000, address.getCode().intValue());
}

@Produces
@RequestScoped
private static JsonWebToken jwt() throws Exception {
Expand Down Expand Up @@ -126,6 +138,9 @@ private static class RawConverterBean {
@Inject
@Claim("long")
private Long longClaim;
@Inject
@Claim("address")
private Address address;

String getName() {
return name;
Expand Down Expand Up @@ -158,6 +173,10 @@ public Boolean getBooleanClaim() {
public Long getLongClaim() {
return longClaim;
}

public Address getAddress() {
return address;
}
}

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

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

@Override
Expand Down Expand Up @@ -321,4 +344,25 @@ public Claims standard() {
return INSTANCE.standard();
}
}

public static class Address {
private String street;
private Integer code;

public String getStreet() {
return street;
}

public void setStreet(final String street) {
this.street = street;
}

public Integer getCode() {
return code;
}

public void setCode(final Integer code) {
this.code = code;
}
}
}
7 changes: 6 additions & 1 deletion implementation/src/test/resources/token-converter.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
"float": 99.9,
"double": 99.99,
"boolean": "true",
"char": "y"
"char": "y",

"address": {
"street": "street",
"code": 1000
}
}

0 comments on commit f0507f3

Please sign in to comment.