Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make resteasy-reactive AbstractJsonMessageBodyReader handle MediaType case insensitive #40255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading