Skip to content

Commit

Permalink
Merge pull request #40255 from antonwiens/40244-content-type-case-ins…
Browse files Browse the repository at this point in the history
…ensitive

Make resteasy-reactive AbstractJsonMessageBodyReader handle MediaType case insensitive
  • Loading branch information
gastaldi authored Apr 25, 2024
2 parents 1f4b5c0 + b02a75d commit 228ef7b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ protected boolean isReadable(MediaType mediaType, Class<?> type) {
if (String.class.equals(type)) { // don't attempt to read plain strings
return false;
}
String subtype = mediaType.getSubtype();
boolean isApplicationMediaType = "application".equals(mediaType.getType());
return (isApplicationMediaType && "json".equalsIgnoreCase(subtype) || subtype.endsWith("+json")
|| subtype.equalsIgnoreCase("x-ndjson"))
String subtype = mediaType.getSubtype().toLowerCase();
final String mainType = mediaType.getType().toLowerCase();
boolean isApplicationMediaType = "application".equals(mainType);
return (isApplicationMediaType && "json".equals(subtype) || subtype.endsWith("+json")
|| "x-ndjson".equals(subtype))
|| (mediaType.isWildcardSubtype() && (mediaType.isWildcardType() || isApplicationMediaType));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jboss.resteasy.reactive.common.providers.serialisers;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;

import org.junit.jupiter.api.Test;

class AbstractJsonMessageBodyReaderTest {

class TestReader extends AbstractJsonMessageBodyReader {
@Override
public Object readFrom(Class<Object> aClass, Type type, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> multivaluedMap, InputStream inputStream)
throws IOException, WebApplicationException {
return null;
}
}

@Test
void isReadableCaseInsensitive() {
final TestReader testReader = new TestReader();
assertFalse(testReader.isReadable(new MediaType("application", "jso"), Object.class));
assertFalse(testReader.isReadable(new MediaType("application", "json+anything"), Object.class));
assertFalse(testReader.isReadable(new MediaType("test", "json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("test", "test+json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("test", "x-ndjson"), Object.class));
assertTrue(testReader.isReadable(new MediaType("application", "test+json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("application", "json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("Application", "Json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("appliCAtion", "json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("application", "jSOn"), Object.class));
assertTrue(testReader.isReadable(new MediaType("application", "test+json"), Object.class));
assertTrue(testReader.isReadable(new MediaType("application", "x-ndjson"), Object.class));
assertTrue(testReader.isReadable(new MediaType("applIcation", "x-ndjson"), Object.class));
}
}

0 comments on commit 228ef7b

Please sign in to comment.