You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2026/09/06 22:53:08 UTC

(juneau) branch master updated: WORK-J0525: REST-layer Widget.validate() serving-path hook

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new b254e470b4 WORK-J0525: REST-layer Widget.validate() serving-path hook
b254e470b4 is described below

commit b254e470b4f4d05c6ea6db52094d93bd97d3a579
Author: James Bognar <ja...@apache.org>
AuthorDate: Sun Sep 6 18:52:39 2026 -0400

    WORK-J0525: REST-layer Widget.validate() serving-path hook
    
    Registers WidgetsMixin.WidgetValidationProcessor, a ServiceLoader-discovered ResponseProcessor
    that calls Widget.validate() on any REST response content that is a Widget, fail-closed with a
    500 on a malformed ModalDef/FormDef. Closes the gap WORK-J0520 left open for the JSON serving
    path: ModalDef/FormDef ride the wire directly (unlike the other seven Widget implementations,
    which are validated on the way in by the juneau-rest-server-views HTML emitters), so a malformed
    instance returned from an @RestGet without an explicit .checked() call previously serialized
    successfully. Non-REST producers (a direct Json.of(...) call) remain untouched by design.
---
 .../apache/juneau/rest/server/widgets/FormDef.java |  14 +-
 .../juneau/rest/server/widgets/ModalDef.java       |  22 +-
 .../apache/juneau/rest/server/widgets/Widget.java  |  11 +
 .../juneau/rest/server/widgets/WidgetsMixin.java   |  91 ++++++-
 ....juneau.rest.server.processor.ResponseProcessor |   1 +
 .../ModalDef_ServingPathValidation_Test.java       | 278 +++++++++++++++++++++
 6 files changed, 406 insertions(+), 11 deletions(-)

diff --git a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/FormDef.java b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/FormDef.java
index 92e880bafb..1b5bd0618c 100644
--- a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/FormDef.java
+++ b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/FormDef.java
@@ -653,10 +653,16 @@ public class FormDef implements Widget {
 	 * repairs a version a caller explicitly cleared) then {@link #validate() validates}.
 	 *
 	 * <p>
-	 * {@link #contractVersion} no longer depends on this being called, but this is still the only fail-closed
-	 * structural {@link #validate() validation} the serving path has &mdash; every app {@code @RestGet} that returns
-	 * a form-bearing {@link ModalDef} should still reach this (via {@link ModalDef#checked()}) so a malformed form
-	 * fails at serve time, not silently on the wire.
+	 * {@link #contractVersion} no longer depends on this being called. As of WORK-J0525, structural validation
+	 * no longer depends on it either for a form served over REST: nested inside a returned {@link ModalDef},
+	 * this form is reached by
+	 * {@link org.apache.juneau.rest.server.widgets.WidgetsMixin.WidgetValidationProcessor
+	 * WidgetsMixin.WidgetValidationProcessor}'s call to {@link ModalDef#validate()}, which recurses into this
+	 * bean's own {@link #validate()}, fail-closed with a 500, whether or not the producing endpoint called
+	 * {@link ModalDef#checked()}. This method (via {@link ModalDef#checked()}) remains the only fail-closed
+	 * structural {@link #validate() validation} a <b>non-REST</b> producer has &mdash; a direct
+	 * {@code Json.of(...)} serialization, for instance &mdash; and calling it for a REST-bound form stays safe
+	 * (validation is idempotent) even though it is no longer strictly required there.
 	 *
 	 * @return This object.
 	 * @throws IllegalArgumentException If this form is not well-formed.
diff --git a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/ModalDef.java b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/ModalDef.java
index 8201a9a5ec..b5071b483c 100644
--- a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/ModalDef.java
+++ b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/ModalDef.java
@@ -646,12 +646,22 @@ public class ModalDef implements Widget {
 	 *
 	 * <p>
 	 * {@link #contractVersion} is guaranteed present on every modal from construction, so this is <b>no longer
-	 * required for versioning</b> (WORK-J0520).  It stays <b>strongly recommended</b> for its other duty: it is
-	 * the only fail-closed structural {@link #validate() validation} the serving path has, and skipping it means
-	 * a malformed modal/form now serializes as contract-<i>valid</i> and reaches the client instead of being
-	 * refused (see {@link #validate()}; {@code WORK-J0525} tracks closing this gap for the serving path
-	 * generally).  When a {@link #form} is present it also re-stamps {@link #CONTRACT_VERSION} on the form and
-	 * recurses into {@link FormDef#checked()}.
+	 * required for versioning</b> (WORK-J0520).  It stays <b>strongly recommended</b> for one remaining duty and
+	 * is <b>redundant-but-harmless</b> for another:
+	 * <ul>
+	 * 	<li><b>Non-REST producers</b> (a direct {@code Json.of(...)} serialization, or any consumer marshalling a
+	 * 		modal outside a REST response) have no other gate: calling this is the only fail-closed structural
+	 * 		{@link #validate() validation} they get, and skipping it means a malformed modal/form serializes as
+	 * 		contract-<i>valid</i> with no complaint.
+	 * 	<li><b>REST responses</b> are validated independently as of WORK-J0525:
+	 * 		{@link org.apache.juneau.rest.server.widgets.WidgetsMixin.WidgetValidationProcessor
+	 * 		WidgetsMixin.WidgetValidationProcessor} calls {@link #validate()} on every REST response that is a
+	 * 		{@code Widget}, fail-closed with a 500, whether or not the producing endpoint called this method.
+	 * 		Calling it anyway is safe (validation is idempotent) and remains the only gate for the non-REST case
+	 * 		above.
+	 * </ul>
+	 * When a {@link #form} is present it also re-stamps {@link #CONTRACT_VERSION} on the form and recurses into
+	 * {@link FormDef#checked()}.
 	 *
 	 * @return This object.
 	 * @throws IllegalArgumentException If this modal (or its form) is not well-formed.
diff --git a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/Widget.java b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/Widget.java
index e4f5c18736..1ab31691ba 100644
--- a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/Widget.java
+++ b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/Widget.java
@@ -24,6 +24,17 @@ package org.apache.juneau.rest.server.widgets;
  * implements {@link #validate()} as a fail-closed bean-level check.  Serving-path call sites (for example a
  * table emitter) must invoke {@code validate()} rather than treating it as documentation-only.
  *
+ * <p>
+ * The REST serving path now enforces this independently of any one call site: every classpath that carries this
+ * module registers a {@code ResponseProcessor} (see
+ * {@link org.apache.juneau.rest.server.widgets.WidgetsMixin.WidgetValidationProcessor
+ * WidgetsMixin.WidgetValidationProcessor}, WORK-J0525) that calls {@link #validate()} on any REST response content
+ * that is a {@code Widget}, fail-closed with a 500 on failure.  That closes the gap for the two implementations
+ * that ride the wire as a JSON response body ({@code ModalDef}/{@code FormDef}) rather than being consumed by a
+ * server-side emitter that already validates on the way in; a non-REST producer (a direct {@code Json.of(...)}
+ * serialization, for example) is untouched by construction and still relies on an explicit {@code validate()} /
+ * {@code checked()} call.
+ *
  * @since 10.0.0
  */
 public interface Widget {
diff --git a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/WidgetsMixin.java b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/WidgetsMixin.java
index fc7d962328..6cb956d576 100644
--- a/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/WidgetsMixin.java
+++ b/juneau-rest/juneau-rest-server-widgets/src/main/java/org/apache/juneau/rest/server/widgets/WidgetsMixin.java
@@ -19,7 +19,9 @@ package org.apache.juneau.rest.server.widgets;
 import static org.apache.juneau.commons.utils.Shorts.*;
 
 import org.apache.juneau.http.*;
+import org.apache.juneau.http.response.*;
 import org.apache.juneau.rest.server.*;
+import org.apache.juneau.rest.server.processor.*;
 import org.apache.juneau.rest.server.util.*;
 
 /**
@@ -63,18 +65,38 @@ import org.apache.juneau.rest.server.util.*;
  * {@code buildVersion} resolves this module's implementation version rather than a neighbouring module's.  The two
  * modules share the hash function, not a cache.
  *
+ * <h5 class='section'>The serving-path {@code Widget.validate()} gate (WORK-J0525)</h5>
+ * <p>
+ * This class also nests {@link WidgetValidationProcessor}, a {@link ResponseProcessor} that calls
+ * {@link Widget#validate()} on any REST response content that is a {@link Widget}, fail-closed, ahead of every
+ * byte-writing processor. It is registered two ways &mdash; both belt-and-braces, not redundant duplicates, since
+ * {@code ResponseProcessorList} de-duplicates by class:
+ * <ul>
+ * 	<li>Via {@code ServiceLoader} ({@code META-INF/services/org.apache.juneau.rest.server.processor.ResponseProcessor}),
+ * 		so it applies to <b>every</b> resource on a classpath that carries this module, with no consumer opt-in.
+ * 	<li>Via this class's own {@link Rest#responseProcessors() @Rest(responseProcessors=...)} +
+ * 		{@link Rest#mergeResponseProcessorsIntoHost() mergeResponseProcessorsIntoHost=true}, so a host that composes
+ * 		this mixin is covered even if its classpath loses the services file (a shading/repackaging step, for example).
+ * </ul>
+ * It is declared as a <b>nested</b> class here &mdash; rather than a new top-level file &mdash; because
+ * {@code Widgets_ModuleBoundary_Test.b01_onlyTheMixinReferencesTheRestServingFramework} allowlists exactly this file
+ * as the module's single exception to "the beans stay independent of the REST serving framework"; a response
+ * processor is itself a serving-framework type, so nesting it here keeps that rule literally true rather than
+ * requiring an amendment.
+ *
  * <h5 class='section'>See Also:</h5>
  * <ul>
  * 	<li class='jc'>{@link CardFieldList}
  * 	<li class='jc'>{@link CalendarDef}
  * 	<li class='jc'>{@link AppHeaderDef}
  * 	<li class='jc'>{@link BarSlot}
+ * 	<li class='jc'>{@link WidgetValidationProcessor}
  * </ul>
  *
  * @since 10.0.0
  */
 // @formatter:off
-@Rest
+@Rest(responseProcessors=WidgetsMixin.WidgetValidationProcessor.class, mergeResponseProcessorsIntoHost=true)
 public class WidgetsMixin {
 
 	/**
@@ -282,4 +304,71 @@ public class WidgetsMixin {
 		if (CHROME_JS_PATH.equals(path)) return CHROME_JS_RESOURCE;
 		throw iaex("Widget asset ''%s'' is not served by this mixin.", path);
 	}
+
+	/**
+	 * The serving-path {@link Widget#validate()} gate (WORK-J0525): fail-closed structural validation for any
+	 * REST response content that is a {@link Widget}, giving the JSON serving path the same gate the
+	 * {@code juneau-rest-server-views} HTML emitters already give every {@code Widget} they consume.
+	 *
+	 * <h5 class='section'>Why this is a {@link ResponseProcessor}, not a {@code RestContext} hook</h5>
+	 * <p>
+	 * {@code juneau-rest-server} does not (and must not) depend on this module, so a hard-coded validation step
+	 * inside it cannot see {@link Widget} without inverting that dependency. A {@link ResponseProcessor} declared
+	 * here, discovered by every classpath that carries this module via {@code ServiceLoader}, needs no such
+	 * reflection or hoisted marker interface &mdash; see the class Javadoc.
+	 *
+	 * <h5 class='section'>Why {@code NEXT}, never {@code FINISHED}</h5>
+	 * <p>
+	 * This processor validates and declines to handle: it never claims the response, so it cannot perturb chain
+	 * ordering, content-type negotiation, or the {@code NotImplemented} fallthrough. Non-{@link Widget} content
+	 * (including the unrelated {@code examples.views.Widget} demo row bean and the legacy
+	 * {@code org.apache.juneau.rest.server.widget.Widget} SVL resolver &mdash; see the module Javadoc's naming
+	 * collision note) costs one {@code instanceof} test and is otherwise untouched.
+	 *
+	 * <h5 class='section'>Why {@code InternalServerError} (500), not the caller's problem</h5>
+	 * <p>
+	 * A malformed {@link Widget} reaching the serving path is a <b>server</b> defect &mdash; the client asked for
+	 * a dialog correctly and the server built a broken one &mdash; not an inbound-validation failure of the kind
+	 * {@code ValidationException} (400) is scoped to. The underlying {@link IllegalArgumentException} rides as the
+	 * cause so the message names the failing rule.
+	 *
+	 * <h5 class='section'>Deliberately not deduplicated against {@link ModalDef#checked()}/{@link FormDef#checked()}</h5>
+	 * <p>
+	 * A widget that already called {@code checked()} is validated twice per response: once at build time, once
+	 * here. No {@code validated} flag is added to skip the second pass &mdash; a mutable "already validated" flag
+	 * on a wire bean would re-introduce an order-dependence bug in the validation dimension (mutate-after-checked)
+	 * that this module's contract deliberately avoids elsewhere. {@code validate()} is cheap and idempotent, so the
+	 * repeat costs nothing worth building around.
+	 *
+	 * <h5 class='section'>Traversal is top-level only</h5>
+	 * <p>
+	 * Only {@code res.getContent()} itself is tested. Nested widgets are covered by the beans' own recursive
+	 * {@code validate()} (e.g. {@link ModalDef#validate()} &rarr; {@link FormDef#validate()}); a {@link Widget}
+	 * inside a {@code Collection}/{@code Map} envelope is not walked into &mdash; no endpoint in this tree returns
+	 * that shape, and walking one would collide with the unrelated {@code examples.views.Widget} row-bean lists
+	 * some example endpoints do return.
+	 *
+	 * <h5 class='section'>Escape hatch</h5>
+	 * <p>
+	 * A resource that must serve a deliberately-invalid {@link Widget} can override the response-processor chain
+	 * entirely with a {@code @Bean ResponseProcessorList} factory method, which replaces the whole list. There is
+	 * no narrower, dedicated opt-out.
+	 *
+	 * @since 10.0.0
+	 */
+	public static class WidgetValidationProcessor implements ResponseProcessor {
+
+		@Override /* ResponseProcessor */
+		public int process(RestOpSession opSession) {
+			var o = opSession.getResponse().getContent().orElse(null);
+			if (o instanceof Widget w) {
+				try {
+					w.validate();
+				} catch (IllegalArgumentException e) {
+					throw new InternalServerError(e, "Malformed %s in response: %s", w.getClass().getSimpleName(), e.getMessage());
+				}
+			}
+			return NEXT;  // Validate and decline; the normal chain serializes.
+		}
+	}
 }
diff --git a/juneau-rest/juneau-rest-server-widgets/src/main/resources/META-INF/services/org.apache.juneau.rest.server.processor.ResponseProcessor b/juneau-rest/juneau-rest-server-widgets/src/main/resources/META-INF/services/org.apache.juneau.rest.server.processor.ResponseProcessor
new file mode 100644
index 0000000000..b24a487fd5
--- /dev/null
+++ b/juneau-rest/juneau-rest-server-widgets/src/main/resources/META-INF/services/org.apache.juneau.rest.server.processor.ResponseProcessor
@@ -0,0 +1 @@
+org.apache.juneau.rest.server.widgets.WidgetsMixin$WidgetValidationProcessor
diff --git a/juneau-rest/juneau-rest-server-widgets/src/test/java/org/apache/juneau/rest/server/widgets/ModalDef_ServingPathValidation_Test.java b/juneau-rest/juneau-rest-server-widgets/src/test/java/org/apache/juneau/rest/server/widgets/ModalDef_ServingPathValidation_Test.java
new file mode 100644
index 0000000000..b4bc42d7ab
--- /dev/null
+++ b/juneau-rest/juneau-rest-server-widgets/src/test/java/org/apache/juneau/rest/server/widgets/ModalDef_ServingPathValidation_Test.java
@@ -0,0 +1,278 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.juneau.rest.server.widgets;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.*;
+
+import org.apache.juneau.*;
+import org.apache.juneau.marshall.marshaller.*;
+import org.apache.juneau.rest.mock.classic.*;
+import org.apache.juneau.rest.server.*;
+import org.apache.juneau.rest.server.servlet.*;
+import org.junit.jupiter.api.*;
+
+/**
+ * REST-boundary tests for {@link WidgetsMixin.WidgetValidationProcessor} (WORK-J0525) &mdash; the
+ * {@code ServiceLoader}-registered {@link org.apache.juneau.rest.server.processor.ResponseProcessor
+ * ResponseProcessor} that calls {@link Widget#validate()} on any REST response content that is a {@link Widget},
+ * giving the JSON serving path ({@link ModalDef}/{@link FormDef}) the same fail-closed gate the
+ * {@code juneau-rest-server-views} HTML emitters already give every {@code Widget} they consume.
+ *
+ * <p>
+ * This is the REST-boundary inverse of {@code WORK-J0520}'s (f1)/(f2) tests: those pin that a malformed,
+ * never-{@code checked()} modal/form serializes successfully with the current contract version and does not
+ * throw. Here, served over a real REST response instead of a bare {@link Json#of(Object) Json.of(...)} call, the
+ * same malformed shapes now fail loud with a 500 &mdash; which is this item's entire reason to exist (see
+ * {@code h2} below for the pin that {@link Json#of(Object) Json.of(...)} itself is deliberately untouched).
+ *
+ * <p>
+ * No {@code @Rest(mixins=WidgetsMixin.class)} composition is required on any host below except {@code HostC}
+ * (the double-registration guard): the validator is discovered via {@code ServiceLoader} from this module's
+ * {@code META-INF/services} entry on the test classpath, with no consumer opt-in, which is the point of the
+ * design (design doc &sect;4.1 reason 1).
+ */
+class ModalDef_ServingPathValidation_Test extends TestBase {
+
+	//------------------------------------------------------------------------------------------------------------------
+	// Malformed-shape builders - one per J0520 harm-table row (design doc &sect;8(a3)), built WITHOUT the
+	// public factories' own argument checks so the shape survives construction and only fails at validate() time.
+	//------------------------------------------------------------------------------------------------------------------
+
+	/** Row: a {@code select} input with no options. */
+	private static ModalDef noOptionSelect() {
+		return ModalDef.create("Ack?").form(
+			FormDef.create().field(FormDef.Input.of("sev", "Severity", "select")));
+	}
+
+	/** Row: two fields sharing one name - collide on submit. */
+	private static ModalDef duplicateFieldName() {
+		return ModalDef.create("Ack?").form(
+			FormDef.create()
+				.field(FormDef.Input.of("x", "First", "text"))
+				.field(FormDef.Input.of("x", "Second", "text")));
+	}
+
+	/** Row: both {@code fields} and {@code sections} declared - mutually exclusive. */
+	private static ModalDef fieldsAndSections() {
+		var form = FormDef.create().field(FormDef.Input.of("a", "A", "text"));
+		form.sections = List.of(FormDef.Section.of("s1", "S1").field(FormDef.Input.of("b", "B", "text")));
+		return ModalDef.create("Ack?").form(form);
+	}
+
+	/** Row: a non-compiling client validation pattern. */
+	private static ModalDef nonCompilingPattern() {
+		return ModalDef.create("Ack?").form(
+			FormDef.create().field(FormDef.Input.of("p", "P", "text").pattern("[")));
+	}
+
+	/** Row: a blank modal title, bypassing {@link ModalDef#create(String)}'s own blank check. */
+	private static ModalDef blankTitle() {
+		var m = new ModalDef();
+		m.title = "   ";
+		return m;
+	}
+
+	/** Row (acceptable-today, becomes a 500 under this hook): a confirmation field {@code kind} off the allowlist. */
+	private static ModalDef fieldKindOffAllowlist() {
+		var m = ModalDef.create("Ack?");
+		var f = new ModalDef.Field();
+		f.label = "Severity";
+		f.kind = "not-a-real-kind";
+		m.fields = List.of(f);
+		return m;
+	}
+
+	/** Row (stays acceptable): an {@code action} input whose {@code actionId} is non-blank but resolves to nothing. */
+	private static ModalDef unknownActionId() {
+		return ModalDef.create("Ack?").form(
+			FormDef.create().field(FormDef.Input.of("go", "Go", "action").actionId("no-such-action")));
+	}
+
+	private static ModalDef validNoForm() {
+		return ModalDef.create("Ack?");
+	}
+
+	//------------------------------------------------------------------------------------------------------------------
+	// a) The headline - malformed Widget served over REST now fails; the full harm-table matrix.
+	//------------------------------------------------------------------------------------------------------------------
+
+	@Rest
+	public static class HostA extends BasicRestServlet {
+		private static final long serialVersionUID = 1L;
+
+		@RestGet(path="/unchecked-invalid") public ModalDef uncheckedInvalid() { return noOptionSelect(); }
+		@RestGet(path="/checked-valid") public ModalDef checkedValid() {
+			return ModalDef.create("Ack?").form(FormDef.create().field(FormDef.Input.of("sev", "Severity", "text"))).checked();
+		}
+		@RestGet(path="/dup-name") public ModalDef dupName() { return duplicateFieldName(); }
+		@RestGet(path="/fields-and-sections") public ModalDef fieldsAndSectionsOp() { return fieldsAndSections(); }
+		@RestGet(path="/bad-pattern") public ModalDef badPattern() { return nonCompilingPattern(); }
+		@RestGet(path="/blank-title") public ModalDef blankTitleOp() { return blankTitle(); }
+		@RestGet(path="/bad-kind") public ModalDef badKind() { return fieldKindOffAllowlist(); }
+		@RestGet(path="/unknown-action-id") public ModalDef unknownActionIdOp() { return unknownActionId(); }
+	}
+
+	private static final MockRestClient A = MockRestClient.buildLax(HostA.class);
+
+	@Test void a1_uncheckedMalformedModal_servedOverRest_fails500_noPartialBody() throws Exception {
+		A.get("/unchecked-invalid").accept("application/json").run()
+			.assertStatus(500)
+			.assertContent().asString().isNotContains("\"contractVersion\"", "\"fields\"");
+	}
+
+	@Test void a2_control_checkedValidModal_serves200_normalPayload() throws Exception {
+		A.get("/checked-valid").accept("application/json").run().assertStatus(200)
+			.assertContent().asString().isContains("\"contractVersion\"", "\"fields\"");
+	}
+
+	@Test void a3_harmTableMatrix_allFiveSilentlyDegradingShapes_fail500() throws Exception {
+		A.get("/dup-name").run().assertStatus(500);
+		A.get("/fields-and-sections").run().assertStatus(500);
+		A.get("/bad-pattern").run().assertStatus(500);
+		A.get("/blank-title").run().assertStatus(500);
+		// a1 above is the fifth row (no-option select).
+	}
+
+	@Test void a4_kindOffAllowlist_becomes500_behaviorChangeFromDegradesSafely() throws Exception {
+		// Previously degraded safely to "text" because nothing called validate() on the unchecked path; this hook
+		// now enforces ModalDef.Field's kind allowlist on every response, so this becomes a loud 500 instead.
+		A.get("/bad-kind").run().assertStatus(500).assertContent().asString().isContains("kind");
+	}
+
+	@Test void a4_unknownActionId_staysAcceptable_200() throws Exception {
+		// Not a validate() rule (the bean cannot resolve the id against the enclosing view's action catalog) - the
+		// hook must not newly reject this; the runtime paints it disabled instead.
+		A.get("/unknown-action-id").run().assertStatus(200);
+	}
+
+	//------------------------------------------------------------------------------------------------------------------
+	// b) The fail-closed response shape.
+	//------------------------------------------------------------------------------------------------------------------
+
+	@Test void b1_status_isExactly500_not400_not200() throws Exception {
+		A.get("/unchecked-invalid").run().assertStatus(500);
+	}
+
+	@Test void b2_body_namesTheFailingRule() throws Exception {
+		// suppressedErrorBodyMessage(...) surfaces e2.getMessage() whenever the thrown exception is itself a
+		// BasicHttpException (appThrown==true) - true here regardless of renderResponseStackTraces - so the
+		// InternalServerError's own message, naming the widget type and the underlying validate() failure, reaches
+		// the client.
+		A.get("/unchecked-invalid").run().assertStatus(500)
+			.assertContent().asString().isContains("ModalDef", "select must declare at least one option");
+	}
+
+	@Test void b3_noBytesWrittenBeforeTheThrow_bodyIsTheErrorBodyOnly() throws Exception {
+		var body = A.get("/unchecked-invalid").run().assertStatus(500).getContent().asString();
+		assertTrue(body.startsWith("HTTP 500"), body);
+		assertFalse(body.contains("\"title\""), body);
+	}
+
+	//------------------------------------------------------------------------------------------------------------------
+	// c) No double-handling; inert off its own path.
+	//------------------------------------------------------------------------------------------------------------------
+
+	/** A {@link Widget} whose {@code validate()} counts calls instead of checking anything - always "valid". */
+	static final class CountingWidget implements Widget {
+		static final AtomicInteger COUNT = new AtomicInteger();
+		@Override public void validate() { COUNT.incrementAndGet(); }
+	}
+
+	@Rest(mixins=WidgetsMixin.class)
+	public static class HostC extends BasicRestServlet {
+		private static final long serialVersionUID = 1L;
+		@RestGet(path="/counting") public CountingWidget counting() { return new CountingWidget(); }
+		@RestGet(path="/plain") public String plain() { return "plain"; }
+		@RestGet(path="/map") public Map<String,Object> map() { return Map.of("a", 1); }
+		@RestGet(path="/list") public List<String> list() { return List.of("x", "y"); }
+	}
+
+	private static final MockRestClient C = MockRestClient.buildLax(HostC.class);
+
+	@Test void c1_registeredByServiceLoaderAndMixin_validatesExactlyOnce_notTwice() throws Exception {
+		// HostC composes WidgetsMixin, so both the ServiceLoader entry and the mixin's own
+		// responseProcessors + mergeResponseProcessorsIntoHost=true declaration are candidates;
+		// ResponseProcessorList.Builder de-duplicates by class, so validate() must run exactly once.
+		var before = CountingWidget.COUNT.get();
+		C.get("/counting").run().assertStatus(200);
+		assertEquals(before + 1, CountingWidget.COUNT.get(), "validate() must run exactly once, not once per registration");
+	}
+
+	@Test void c2_nonWidgetContent_servesUnaffected() throws Exception {
+		C.get("/plain").accept("application/json").run().assertStatus(200).assertContent().asString().is("\"plain\"");
+		C.get("/map").accept("application/json").run().assertStatus(200).assertContent().asString().isContains("\"a\":1");
+		C.get("/list").accept("application/json").run().assertStatus(200).assertContent().asString().isContains("\"x\"", "\"y\"");
+	}
+
+	//------------------------------------------------------------------------------------------------------------------
+	// d) Async - CompletableFuture<ModalDef>, covered on the MockServletRequest sync-fallback (RESTART) path.
+	//------------------------------------------------------------------------------------------------------------------
+
+	@Rest
+	public static class HostD extends BasicRestServlet {
+		private static final long serialVersionUID = 1L;
+		@RestGet(path="/future-invalid") public CompletableFuture<ModalDef> futureInvalid() {
+			return CompletableFuture.completedFuture(noOptionSelect());
+		}
+		@RestGet(path="/future-valid") public CompletableFuture<ModalDef> futureValid() {
+			return CompletableFuture.completedFuture(validNoForm());
+		}
+	}
+
+	private static final MockRestClient D = MockRestClient.buildLax(HostD.class);
+
+	@Test void d1_malformedCompletableFuture_fails500() throws Exception {
+		D.get("/future-invalid").run().assertStatus(500);
+	}
+
+	@Test void d2_validCompletableFuture_serves200() throws Exception {
+		D.get("/future-valid").accept("application/json").run().assertStatus(200)
+			.assertContent().asString().isContains("\"contractVersion\"");
+	}
+
+	//------------------------------------------------------------------------------------------------------------------
+	// h) The redundancy pin - .checked() and this hook are deliberately not deduplicated.
+	//------------------------------------------------------------------------------------------------------------------
+
+	@Rest
+	public static class HostH extends BasicRestServlet {
+		private static final long serialVersionUID = 1L;
+		@RestGet(path="/checked") public ModalDef checkedOp() { return validNoForm().checked(); }
+		@RestGet(path="/unchecked") public ModalDef uncheckedOp() { return validNoForm(); }
+	}
+
+	private static final MockRestClient H = MockRestClient.buildLax(HostH.class);
+
+	@Test void h1_checkedAndUnchecked_validModal_serveByteIdentical() throws Exception {
+		// checked() re-validates (the hook validates again); unchecked relies solely on the hook.  Both are valid,
+		// so the served bytes must be identical - the assertion that keeps ".checked()-vs-hook" drift impossible.
+		var checked = H.get("/checked").run().assertStatus(200).getContent().asString();
+		var unchecked = H.get("/unchecked").run().assertStatus(200).getContent().asString();
+		assertEquals(checked, unchecked);
+	}
+
+	@Test void h2_jsonOf_malformedModal_stillSucceeds_notTheSerializerLevelGate() throws Exception {
+		// A ResponseProcessor never runs for Json.of(...): this is the pin that this item did not accidentally
+		// become the serializer-level gate the design rejected (design doc fork F12(f), section 4.3(d)).  It is
+		// deliberately uncomfortable to read, exactly like WORK-J0520's own (f1).
+		assertDoesNotThrow(() -> Json.of(noOptionSelect()));
+	}
+}