Skip to content

Commit

Permalink
change abstractjsonmessagebodyreader to use lowercase mediatype for e…
Browse files Browse the repository at this point in the history
…asier and more clear matching
  • Loading branch information
antonwiens committed Apr 24, 2024
1 parent e7ede18 commit 5b70a2f
Show file tree
Hide file tree
Showing 2 changed files with 47 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,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<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 5b70a2f

Please sign in to comment.