Skip to content

Commit

Permalink
Expand types which are considered text in multipart handling
Browse files Browse the repository at this point in the history
Fixes: #38802
  • Loading branch information
geoand committed Feb 16, 2024
1 parent e97f3e2 commit b3a9aa7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.resteasy.reactive.server.test.multipart;

import static io.restassured.RestAssured.given;

import java.io.IOException;
import java.util.function.Supplier;

import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

import org.jboss.resteasy.reactive.RestResponse;
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;

public class MultipartTextWithoutFilenameTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Resource.class);
}
});

@Test
public void test() throws IOException {
given()
.contentType("multipart/form-data")
.multiPart("firstParam", "{\"id\":\"myId\",\"name\":\"myName\"}", "application/json")
.when()
.post("/test")
.then()
.statusCode(200);
}

@Path("/test")
public static class Resource {

@POST
public RestResponse<Void> testMultipart(@FormParam("firstParam") final String firstParam,
@FormParam("secondParam") final String secondParam) {
return RestResponse.ok();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,12 @@ public static boolean equivalentParams(MediaType m1, MediaType m2) {
return true;
}

// TODO: does this need to be more complex?
public static boolean isTextLike(MediaType mediaType) {
return "text".equalsIgnoreCase(mediaType.getType())
|| ("application".equalsIgnoreCase(mediaType.getType())
&& mediaType.getSubtype().toLowerCase().startsWith("xml"));
String type = mediaType.getType();
String subtype = mediaType.getSubtype();
return (type.equals("application") && (subtype.contains("json") || subtype.contains("xml") || subtype.contains("yaml")))
|| type.equals("text");
}

public static boolean isUnsupportedWildcardSubtype(MediaType mediaType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import jakarta.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.common.util.MediaTypeHelper;
import org.jboss.resteasy.reactive.server.spi.ContentType;

/**
Expand All @@ -21,7 +22,7 @@ public EncodedMediaType(MediaType mediaType) {
MediaType effectiveMediaType = mediaType;
String effectiveCharset;
String originalCharset = mediaType.getParameters().get("charset");
if (isStringMediaType(mediaType)) {
if (MediaTypeHelper.isTextLike(mediaType)) {
effectiveCharset = originalCharset;
if (effectiveCharset == null) {
effectiveCharset = StandardCharsets.UTF_8.name();
Expand All @@ -38,12 +39,6 @@ public EncodedMediaType(MediaType mediaType) {
}

// TODO: does this need to be more complex?
private boolean isStringMediaType(MediaType mediaType) {
String type = mediaType.getType();
String subtype = mediaType.getSubtype();
return (type.equals("application") && (subtype.contains("json") || subtype.contains("xml") || subtype.contains("yaml")))
|| type.equals("text");
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.common.headers.HeaderUtil;
import org.jboss.resteasy.reactive.common.util.CaseInsensitiveMap;
import org.jboss.resteasy.reactive.common.util.MediaTypeHelper;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.spi.ServerHttpRequest;

Expand Down Expand Up @@ -366,7 +367,7 @@ private boolean isText(String contentType) {
if (contentType == null || contentType.isEmpty()) { // https://www.rfc-editor.org/rfc/rfc7578.html#section-4.4 says the default content-type if missing is text/plain
return true;
}
return MediaType.TEXT_PLAIN_TYPE.isCompatible(MediaType.valueOf(contentType));
return MediaTypeHelper.isTextLike(MediaType.valueOf(contentType));
}

public List<Path> getCreatedFiles() {
Expand Down

0 comments on commit b3a9aa7

Please sign in to comment.