From e23f7b0aa2705fed8a4a55106b7b79731501b7e7 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Sat, 21 Nov 2026 14:19:08 -0500 Subject: [PATCH 01/14] refactor(codegen-spring): write the Spring controller through the code writer (W-JV-3) SpringControllerGenerator writes through SpringJavaWriter, a JavaWriter adding continuation lines and indented case bodies. Each member of the generated controller comes from its own protected write* method. SpringGeneratorSupport's cross-field validator members and annotation import lists go through the writer too, and both files leave the layout ratchet's allowlist. Output is unchanged on 1,512 controllers from every fixture model except for sorted imports and no blank line after the class opener; the six golden controllers are regenerated. DTO and value-object output is byte-identical. --- CHANGELOG.md | 11 + .../resources/layout-ratchet/allowlist.json | 2 - .../spring/SpringControllerGenerator.java | 2394 +++++++++-------- .../generator/spring/SpringDtoGenerator.java | 29 +- .../spring/SpringGeneratorSupport.java | 58 +- .../generator/spring/SpringJavaWriter.java | 59 + .../spring/SpringValueObjectGenerator.java | 10 +- .../acme/shop/CustomerController.java | 21 +- .../shop/CustomerStatusTotalsController.java | 15 +- .../expected/acme/shop/OrderController.java | 21 +- .../acme/shop/OrderSummaryController.java | 15 +- .../acme/shop/OrderTagController.java | 21 +- .../expected/acme/shop/TagController.java | 21 +- 13 files changed, 1422 insertions(+), 1255 deletions(-) create mode 100644 server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringJavaWriter.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 534327d4c4..6f20a91a40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,17 @@ here.** TypeScript passes both. The other ports take them on in their 1.3.0 REST-standard work. The decision is [ADR-0068](spec/decisions/ADR-0068-list-query-cross-port-contract.md). A scenario can now send a body that is not JSON with `rawBody`. +- **Java: the Spring controller generator writes through the code writer (ADR-0063).** + `SpringControllerGenerator` writes through `SpringJavaWriter`, a `JavaWriter` that adds the two + layouts Spring source uses and a block does not: a continuation line (two indents deeper) and an + indented `case` body. Every member of the generated controller comes from its own protected + `write*` method, so an owned copy changes one handler by overriding one method; the `append*` + helpers that took a `StringBuilder` are gone. The shared validator members and the decimal and + duration import lists in `SpringGeneratorSupport` are written through the writer too + (`writeCrossFieldValidationMembers`; `decimalAnnotationImports` and `durationAnnotationImports` + now return qualified names). A generated controller changes in two layout details only, checked + on 1,512 controllers from every fixture model: its imports are sorted, and the blank line after + `public class ... {` is gone. The DTO and value-object output is unchanged. - **runtime-ts: one list-query parse for every mount.** `parseListQuery` (exported from `@metaobjectsdev/runtime-ts`; it does not depend on Drizzle) reads a list request into a port-neutral `ListQuery`, and each mount compiles that for its own query library. The Drizzle diff --git a/server/java/codegen-base/src/test/resources/layout-ratchet/allowlist.json b/server/java/codegen-base/src/test/resources/layout-ratchet/allowlist.json index 1ad86f3d8c..0c676096b4 100644 --- a/server/java/codegen-base/src/test/resources/layout-ratchet/allowlist.json +++ b/server/java/codegen-base/src/test/resources/layout-ratchet/allowlist.json @@ -9,10 +9,8 @@ "codegen-spring/src/main/java/com/metaobjects/generator/spring/Fr019SharedEnum.java": "Emits the shared enum class by string concatenation with hand-typed line breaks and indentation. Converts with the Java small generators and polish writer PR (W-JV-5).", "codegen-spring/src/main/java/com/metaobjects/generator/spring/JUnitRequirementTestsGenerator.java": "Emits the JUnit requirement test classes by string concatenation with hand-typed line breaks and indentation. Converts with the Java small generators and polish writer PR (W-JV-5).", "codegen-spring/src/main/java/com/metaobjects/generator/spring/LlmTraceHelperGenerator.java": "Emits the LLM trace helper class by string concatenation with hand-typed line breaks and indentation. Converts with the Java small generators and polish writer PR (W-JV-5).", - "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java": "Emits the Spring controller by string concatenation with hand-typed line breaks and indentation. Converts with the Java controller and support class writer PR (W-JV-3).", "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringDtoGenerator.java": "Emits the DTO records by string concatenation with hand-typed line breaks and indentation. Converts with the Java DTO, parser, names, repository and report export writer PR (W-JV-4).", "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringFilterAllowlistGenerator.java": "Emits the filter allowlist class by string concatenation with hand-typed line breaks and indentation. Converts with the Java small generators and polish writer PR (W-JV-5).", - "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringGeneratorSupport.java": "Emits the validator methods and import lines shared by the Spring generators by string concatenation with hand-typed line breaks and indentation. Converts with the Java controller and support class writer PR (W-JV-3).", "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNamesGenerator.java": "Emits the names constants class by string concatenation with hand-typed line breaks and indentation. Converts with the Java DTO, parser, names, repository and report export writer PR (W-JV-4).", "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringNaming.java": "Writes the package line of a generated file with a typed line break. Converts with the Java DTO, parser, names, repository and report export writer PR (W-JV-4), when that package line comes from JavaWriter.packageName().", "codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringOutputParserGenerator.java": "Emits the output parser class by string concatenation with hand-typed line breaks and indentation. Converts with the Java DTO, parser, names, repository and report export writer PR (W-JV-4).", diff --git a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java index fc6471fcdd..03283bb26c 100644 --- a/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java +++ b/server/java/codegen-spring/src/main/java/com/metaobjects/generator/spring/SpringControllerGenerator.java @@ -6,6 +6,7 @@ // same-package bare-name resolution, is what keeps the ejected copy compiling. import com.metaobjects.generator.spring.AutoSetSupport; import com.metaobjects.generator.spring.SpringGeneratorSupport; +import com.metaobjects.generator.spring.SpringJavaWriter; import com.metaobjects.generator.spring.SpringM2mSupport; import com.metaobjects.generator.spring.SpringNaming; import com.metaobjects.generator.spring.SpringTypeMapper; @@ -21,6 +22,7 @@ import com.metaobjects.generator.RunAware; import com.metaobjects.generator.GeneratorIOWriter; import com.metaobjects.generator.direct.MultiFileDirectGeneratorBase; +import com.metaobjects.generator.writer.CodeWriter; import com.metaobjects.loader.MetaDataLoader; import com.metaobjects.generator.util.RestSurfaceGate; import com.metaobjects.object.MetaObject; @@ -32,7 +34,6 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -86,15 +87,11 @@ * consumer implements with their persistence layer of choice (Spring Data * JPA / jOOQ / plain JDBC).

* - *

Substrate justification (hand-rolled string builder rather than - * JavaPoet): same trade-off as - * {@code KotlinSpringControllerGenerator}. Spring annotations - * ({@code @GetMapping}, {@code @PathVariable}, {@code @RequestParam}) plus - * generic types ({@code ResponseEntity}, {@code Optional}) - * don't translate cleanly to JavaPoet's {@code MethodSpec}/{@code AnnotationSpec} - * APIs without a verbose dance of {@code $T} placeholders; the syntactic - * surface is small (~110 lines per file) and matches the idiomatic Spring - * style without the indirection of a typed-emit DSL.

+ *

Layout (ADR-0063): the controller is written through a {@link SpringJavaWriter}. The + * imports are collected and sorted by the writer, fixed code is written as it reads with + * {@code text()}, and every member of the generated class comes from its own protected + * {@code write*} method, so an owned copy (or a subclass) changes one handler by overriding + * one method.

* *

Args:

*