forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce support for ContextResolver<ObjectMapper> in server part of…
… RESTEasy Reactive Relates to: quarkusio#26152
- Loading branch information
Showing
3 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...c/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/ContextResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_INDEX; | ||
import static io.restassured.RestAssured.with; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.ext.ContextResolver; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.http.ContentType; | ||
|
||
public class ContextResolverTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(EnumsResource.class, Type.class, TypeContextResolver.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void shouldUseCustomObjectMapper() { | ||
with().accept(ContentType.JSON).get("/enums/type/foo") | ||
.then().statusCode(200).body(equalTo("0")); | ||
with().accept(ContentType.JSON).get("/enums/type/bar") | ||
.then().statusCode(200).body(equalTo("1")); | ||
} | ||
|
||
@Test | ||
public void shouldUseDefaultObjectMapper() { | ||
with().accept(ContentType.JSON).get("/enums/color/red") | ||
.then().statusCode(200).body(equalTo("\"RED\"")); | ||
with().accept(ContentType.JSON).get("/enums/color/black") | ||
.then().statusCode(200).body(equalTo("\"BLACK\"")); | ||
} | ||
|
||
@Path("enums") | ||
public static class EnumsResource { | ||
|
||
@Path("type/foo") | ||
@GET | ||
public Type foo() { | ||
return Type.FOO; | ||
} | ||
|
||
@Path("type/bar") | ||
@GET | ||
public Type bar() { | ||
return Type.BAR; | ||
} | ||
|
||
@Path("color/red") | ||
@GET | ||
public Color red() { | ||
return Color.RED; | ||
} | ||
|
||
@Path("color/black") | ||
@GET | ||
public Color black() { | ||
return Color.BLACK; | ||
} | ||
} | ||
|
||
public enum Type { | ||
FOO, | ||
BAR | ||
} | ||
|
||
public enum Color { | ||
RED, | ||
BLACK | ||
} | ||
|
||
@Provider | ||
public static class TypeContextResolver implements ContextResolver<ObjectMapper> { | ||
@Override | ||
public ObjectMapper getContext(Class<?> type) { | ||
if (!type.isAssignableFrom(Type.class)) { | ||
return null; | ||
} | ||
ObjectMapper result = new ObjectMapper(); | ||
result.enable(WRITE_ENUMS_USING_INDEX); | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters