Skip to content
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
1 change: 1 addition & 0 deletions src/main/java/com/retailsvc/http/OpenApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ private static Map<String, TypeMapper> resolveBodyMappers(
LinkedHashMap<String, TypeMapper> out = new LinkedHashMap<>();
out.put("application/x-www-form-urlencoded", new FormTypeMapper());
out.put("text/plain", new TextTypeMapper());
out.put("text/html", new TextTypeMapper());
out.putAll(userSupplied);
if (!out.containsKey(JSON)) {
TypeMapper fallback = tryLoadGsonMapper();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/retailsvc/http/TypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/**
* Reads and writes request/response bodies for a specific media type. Registered on {@link
* OpenApiServer.Builder#bodyMapper(String, TypeMapper)} keyed by media type. The library ships
* built-in mappers for {@code application/x-www-form-urlencoded} and {@code text/plain}; an {@code
* application/json} mapper must be supplied by the caller or auto-detected via Gson on the
* classpath.
* built-in mappers for {@code application/x-www-form-urlencoded}, {@code text/plain}, and {@code
* text/html}; an {@code application/json} mapper must be supplied by the caller or auto-detected
* via Gson on the classpath.
*/
public interface TypeMapper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ private void renderBytes(
}

private byte[] serialize(Object body, String contentType) {
TypeMapper mapper = mappers.get(contentType.toLowerCase(Locale.ROOT));
String mediaType = ContentTypeHeader.mediaType(contentType);
TypeMapper mapper = mappers.get(mediaType.toLowerCase(Locale.ROOT));
if (mapper == null) {
throw new IllegalStateException("No TypeMapper registered for " + contentType);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/retailsvc/http/internal/TextTypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import java.nio.charset.StandardCharsets;

/**
* Built-in {@link TypeMapper} for {@code text/plain}. Reads decode bytes using the charset declared
* on {@code Content-Type} (default UTF-8). Writes return {@code String.valueOf(value)} encoded as
* UTF-8.
* Built-in {@link TypeMapper} for textual media types (e.g. {@code text/plain}, {@code text/html}).
* Media-type-agnostic — only the {@code charset} parameter on the {@code Content-Type} header is
* read. Reads decode bytes using that charset (default UTF-8). Writes return {@code
* String.valueOf(value)} encoded as UTF-8.
*/
public final class TextTypeMapper implements TypeMapper {

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/retailsvc/http/ExtraHandlersIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ void specHandlerServesClasspathResource() throws Exception {
}
}

@Test
void textHtmlResponseIsSerializedByDefaultMapper() throws Exception {
RequestHandler html =
req -> Response.of(200, "<h1>hi</h1>").withContentType("text/html; charset=UTF-8");

try (var s =
newBuilder().spec(spec).handlers(Map.of()).port(0).extraRoute("/page", html).build();
var client = httpClient()) {

var req =
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + s.listenPort() + "/page"))
.GET()
.build();
var resp = client.send(req, BodyHandlers.ofString());

assertThat(resp.statusCode()).isEqualTo(200);
assertThat(resp.headers().firstValue("Content-Type"))
.hasValueSatisfying(v -> assertThat(v).startsWith("text/html"));
assertThat(resp.body()).isEqualTo("<h1>hi</h1>");
}
}

@Test
void extraHandlerExceptionFlowsThroughExceptionHandler() throws Exception {
RequestHandler boom =
Expand Down
Loading