Skip to content

Commit

Permalink
Merge pull request quarkusio#14101 from gsmet/resteasy-parttype
Browse files Browse the repository at this point in the history
Collect providers required for RESTEasy's @PartType annotations
  • Loading branch information
gsmet authored Jan 5, 2021
2 parents 0f70dec + cb9e4d4 commit 75ee1d5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ JaxrsProvidersToRegisterBuildItem setupProviders(BuildProducer<ReflectiveClassBu

boolean needJsonSupport = restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.CONSUMES)
|| restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.PRODUCES)
|| restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_SSE_ELEMENT_TYPE);
|| restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_SSE_ELEMENT_TYPE)
|| restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_PART_TYPE);
if (needJsonSupport) {
LOGGER.warn(
"Quarkus detected the need of REST JSON support but you have not provided the necessary JSON " +
Expand Down Expand Up @@ -483,6 +484,18 @@ private static boolean collectDeclaredProviders(List<RestClientBuildItem> restCl
}
}
}

// handle @PartType: we don't know if it's used for writing or reading so we register both
for (AnnotationInstance partTypeAnnotation : index.getAnnotations(ResteasyDotNames.RESTEASY_PART_TYPE)) {
try {
MediaType partTypeMediaType = MediaType.valueOf(partTypeAnnotation.value().asString());
providersToRegister.addAll(categorizedReaders.getPossible(partTypeMediaType));
providersToRegister.addAll(categorizedWriters.getPossible(partTypeMediaType));
} catch (IllegalArgumentException e) {
// Let's not throw an error, there's a good chance RESTEasy will do it for us
// and if not, this might be valid.
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class ResteasyDotNames {
.createSimple("org.jboss.resteasy.annotations.jaxrs.MatrixParam");
public static final DotName RESTEASY_SSE_ELEMENT_TYPE = DotName
.createSimple("org.jboss.resteasy.annotations.SseElementType");
public static final DotName RESTEASY_PART_TYPE = DotName
.createSimple("org.jboss.resteasy.annotations.providers.multipart.PartType");
public static final DotName CONFIG_PROPERTY = DotName
.createSimple(ConfigProperty.class.getName());
public static final DotName CDI_INSTANCE = DotName
Expand Down
4 changes: 4 additions & 0 deletions extensions/resteasy-multipart/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-multipart</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.resteasy.multipart.parttype;

import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.annotations.providers.multipart.PartType;

public class PartTypeDto {

@FormParam("myMapping")
@PartType(MediaType.APPLICATION_JSON)
public Map<String, String> myMapping;

@FormParam("flag")
@PartType(MediaType.TEXT_PLAIN)
public boolean flag;

@FormParam("reproduceEnum")
@PartType(MediaType.TEXT_PLAIN)
public PartTypeEnum partTypeEnum;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.resteasy.multipart.parttype;

public enum PartTypeEnum {
PENDING,
ACTIVE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.resteasy.multipart.parttype;

import java.net.URISyntaxException;

import javax.ws.rs.core.MediaType;

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 io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.builder.MultiPartSpecBuilder;
import io.restassured.specification.MultiPartSpecification;

public class PartTypeMultipartTest {

@RegisterExtension
static QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(PartTypeDto.class, PartTypeEnum.class, PartTypeResource.class));

@Test
public void testMultipartEncoding() throws URISyntaxException {
MultiPartSpecification multiPartSpecification = new MultiPartSpecBuilder("{ \"key\": \"value\" }")
.controlName("myMapping")
.mimeType(MediaType.APPLICATION_JSON)
.build();

RestAssured
.given()
.multiPart(multiPartSpecification)
.multiPart("flag", "true")
.multiPart("partTypeEnum", "ACTIVE")
.post("/test/part-type/")
.then()
.statusCode(200);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.resteasy.multipart.parttype;

import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;

@Path("/test/part-type/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class PartTypeResource {

@POST
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@Produces({ MediaType.APPLICATION_JSON })
@Transactional
public Response reproduce(@MultipartForm PartTypeDto reproduceDto) {
return Response.ok().build();
}
}

0 comments on commit 75ee1d5

Please sign in to comment.