From b02a75d324ff3a616100f8ca2f8265b2af3f6e8d Mon Sep 17 00:00:00 2001 From: antonwiens <> Date: Wed, 24 Apr 2024 17:05:56 +0200 Subject: [PATCH] change abstractjsonmessagebodyreader to use lowercase mediatype for easier and more clear matching --- .../AbstractJsonMessageBodyReader.java | 9 ++-- .../AbstractJsonMessageBodyReaderTest.java | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReaderTest.java diff --git a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReader.java b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReader.java index 50bf2637442e4..d096d04387b9c 100644 --- a/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReader.java +++ b/independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReader.java @@ -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)); } } diff --git a/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReaderTest.java b/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReaderTest.java new file mode 100644 index 0000000000000..25330dd08f303 --- /dev/null +++ b/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReaderTest.java @@ -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 aClass, Type type, Annotation[] annotations, MediaType mediaType, + MultivaluedMap 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)); + } +}