feat(ext-jackson): one module per data format, bodies checked on the way in
flash-ext-jackson and flash-ext-validation become three modules: - flash-ext-jackson-core: the Codec (one mapper, body/write/writeView), JacksonHandler, the outbound marshalling, and the constraint engine that used to be flash-ext-validation - flash-ext-jackson-json: Json, JsonExtension, JsonHandler - flash-ext-jackson-xml: Xml, XmlExtension, XmlHandler Every Jackson format is the same databind model behind a different factory, so the annotations, the constraints and the published schema are the same for all of them: only the mapper and the content type differ, and that is all a format module says. A route picks its format by the handler it extends — there is no negotiation and nothing to configure. A typed body is now always verified against its own type's jakarta constraints, whatever the format: malformed is a 400, a broken constraint is a 422, and neither reaches the handler. The validator was already allocation-free and stays so; validating is no longer something an application remembers to do. bodyFrom is gone. body has the streaming semantics, because the request's stream is reused per connection while bytes() allocates the whole body: one name, the path that does not allocate. JacksonExtension is JsonExtension, and autoJson() is auto(). The root POM now manages every module of this build, so anything composing Flash imports it once and never names a version again. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
ef4740f26d
commit
adcd6376b6
+20
@@ -0,0 +1,20 @@
|
||||
package dev.relism.flash.ext.jackson.xml;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import dev.relism.flash.ext.jackson.Codec;
|
||||
import dev.relism.flash.http.ContentType;
|
||||
|
||||
/**
|
||||
* XML in and out, checked against the body type's own constraints.
|
||||
*
|
||||
* <p>The same databind model as every other Jackson format: a type is annotated once and can be
|
||||
* read as XML here and as JSON elsewhere in the same application. XML's own concerns — a root
|
||||
* element name, an attribute rather than an element, how a list is wrapped — are Jackson's
|
||||
* {@code @JacksonXml*} annotations on the type.
|
||||
*/
|
||||
public final class Xml extends Codec {
|
||||
|
||||
public Xml(XmlMapper mapper) {
|
||||
super(mapper, ContentType.XML);
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package dev.relism.flash.ext.jackson.xml;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import dev.relism.flash.ext.jackson.Marshalling;
|
||||
import dev.relism.flash.extension.FlashContext;
|
||||
import dev.relism.flash.extension.FlashExtension;
|
||||
import dev.relism.flash.extension.FlashRegistrar;
|
||||
import dev.relism.flash.http.ContentType;
|
||||
import dev.relism.flash.routing.Middleware;
|
||||
|
||||
/**
|
||||
* XML for an application: the {@link Xml} codec and the middleware that serializes what a handler
|
||||
* returns.
|
||||
*
|
||||
* <pre>{@code
|
||||
* XmlExtension xml = new XmlExtension();
|
||||
* app.install(xml).use(xml.auto());
|
||||
* }</pre>
|
||||
*
|
||||
* <p>Install it beside {@code JsonExtension} when an application speaks both: each provides its
|
||||
* own codec, and a route picks one by the handler it extends.
|
||||
*/
|
||||
public class XmlExtension implements FlashExtension {
|
||||
|
||||
private final XmlMapper mapper;
|
||||
|
||||
public XmlExtension() {
|
||||
this(XmlMapper.builder().findAndAddModules().build());
|
||||
}
|
||||
|
||||
public XmlExtension(XmlMapper mapper) {
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
/** Serializes what a handler returns, unless it already returned a response, bytes or text. */
|
||||
public Middleware auto() {
|
||||
return Marshalling.of(mapper, ContentType.XML);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(FlashRegistrar<?> app, FlashContext ctx) {
|
||||
ctx.provide(Xml.class, new Xml(mapper));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package dev.relism.flash.ext.jackson.xml;
|
||||
|
||||
import dev.relism.flash.ext.jackson.Codec;
|
||||
import dev.relism.flash.ext.jackson.JacksonHandler;
|
||||
import dev.relism.flash.extension.Inject;
|
||||
import dev.relism.flash.http.ContentType;
|
||||
import dev.relism.flash.routing.Consumes;
|
||||
|
||||
/**
|
||||
* A handler that takes an XML body of one type.
|
||||
*
|
||||
* <p>Everything {@code JsonHandler} does, in XML: the body is read off the request stream,
|
||||
* verified against its type's constraints, and handed over. Both can live in the same
|
||||
* application, on different routes, over the same types.
|
||||
*/
|
||||
@Consumes(ContentType.XML)
|
||||
public abstract class XmlHandler<B> extends JacksonHandler<B> {
|
||||
|
||||
@Inject private Xml xml;
|
||||
|
||||
@Override protected final Codec codec() {
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package dev.relism.flash.ext.jackson.xml;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import dev.relism.flash.exceptions.HttpException;
|
||||
import dev.relism.flash.extension.FlashContext;
|
||||
import dev.relism.flash.http.ContentType;
|
||||
import dev.relism.flash.http.HttpMethod;
|
||||
import dev.relism.flash.models.Http1HeaderMap;
|
||||
import dev.relism.flash.models.Request;
|
||||
import dev.relism.flash.models.RequestLine;
|
||||
import dev.relism.flash.models.Response;
|
||||
import dev.relism.flash.routing.routers.fastpathrouter.FastPathViews;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/** The same handler shape as JSON, over the same types and the same constraints. */
|
||||
class XmlHandlerTest {
|
||||
|
||||
public record Order(@NotBlank String reference) {}
|
||||
|
||||
static final class Place extends XmlHandler<Order> {
|
||||
@Override protected Object handle(Request request, Response response, Order body) {
|
||||
return body.reference();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBodyArrivesParsedAsTheTypeTheHandlerDeclares() throws Exception {
|
||||
Place handler = new Place();
|
||||
handler.bind(context());
|
||||
|
||||
Object answer = handler.handle(request("<Order><reference>A-1</reference></Order>"), response());
|
||||
|
||||
assertEquals("A-1", answer);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBodyThatBreaksAConstraintNeverReachesTheHandler() throws Exception {
|
||||
Place handler = new Place();
|
||||
handler.bind(context());
|
||||
|
||||
HttpException refused = assertThrows(HttpException.class,
|
||||
() -> handler.handle(request("<Order><reference></reference></Order>"), response()));
|
||||
|
||||
assertEquals(422, refused.status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedBodyIsABadRequest() throws Exception {
|
||||
Place handler = new Place();
|
||||
handler.bind(context());
|
||||
|
||||
HttpException refused = assertThrows(HttpException.class,
|
||||
() -> handler.handle(request("<Order><reference>"), response()));
|
||||
|
||||
assertEquals(400, refused.status());
|
||||
}
|
||||
|
||||
private static FlashContext context() {
|
||||
FlashContext ctx = new FlashContext();
|
||||
ctx.provide(Xml.class, new Xml(XmlMapper.builder().build()));
|
||||
ctx.complete();
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private static Response response() {
|
||||
return new Response(200, ContentType.XML);
|
||||
}
|
||||
|
||||
private static Request request(String body) {
|
||||
return new Request(new RequestLine(HttpMethod.POST,
|
||||
new FastPathViews.StringByteView("/orders"), null,
|
||||
new FastPathViews.StringByteView("HTTP/1.1"), new Http1HeaderMap()),
|
||||
body.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user