From 5b70a2f403a82dc3f9d81870778204b156cd9b8c 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 | 42 +++++++++++++++++++ 2 files changed, 47 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 50bf2637442e4a..d096d04387b9cc 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 00000000000000..867ba8073145f9 --- /dev/null +++ b/independent-projects/resteasy-reactive/common/runtime/src/test/java/org/jboss/resteasy/reactive/common/providers/serialisers/AbstractJsonMessageBodyReaderTest.java @@ -0,0 +1,42 @@ +package org.jboss.resteasy.reactive.common.providers.serialisers; + +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.MultivaluedMap; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +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)); + } +}