Skip to content

Commit 7709a90

Browse files
committed
fix: Use record DTO to preserve health JSON field order
1 parent db08608 commit 7709a90

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

src/main/java/com/retailsvc/http/Handlers.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.sun.net.httpserver.HttpHandler;
1616
import java.io.IOException;
1717
import java.util.List;
18-
import java.util.Map;
1918
import java.util.Objects;
2019
import java.util.function.Supplier;
2120
import java.util.stream.Collectors;
@@ -88,10 +87,10 @@ public static HttpHandler aliveHandler() {
8887
*
8988
* <p>Serialisation is delegated to the supplied {@code jsonMapper} — typically the same {@link
9089
* TypeMapper} the caller registered for {@code application/json} on the server. The handler hands
91-
* the mapper a {@code Map<String,Object>} matching the shape above; any standard JSON library
92-
* (Gson, Jackson, …) serialises it identically.
90+
* the mapper a record-shaped DTO with the components in the order shown above; any standard JSON
91+
* library (Gson, Jackson, …) serialises it identically.
9392
*
94-
* @param jsonMapper used to encode the wire-shape {@code Map} to bytes
93+
* @param jsonMapper used to encode the wire-shape DTO to bytes
9594
* @param probe supplier of the current {@link HealthOutcome}
9695
*/
9796
public static HttpHandler healthHandler(TypeMapper jsonMapper, Supplier<HealthOutcome> probe) {
@@ -116,19 +115,24 @@ public static HttpHandler healthHandler(TypeMapper jsonMapper, Supplier<HealthOu
116115
});
117116
}
118117

119-
private static Map<String, Object> toWireShape(HealthOutcome outcome) {
120-
return Map.of(
121-
"outcome", label(outcome.up()),
122-
"dependencies",
123-
outcome.dependencies().stream()
124-
.map(d -> Map.<String, Object>of("id", d.id(), "status", label(d.up())))
125-
.toList());
118+
private static HealthBody toWireShape(HealthOutcome outcome) {
119+
return new HealthBody(
120+
label(outcome.up()),
121+
outcome.dependencies().stream()
122+
.map(d -> new DependencyBody(d.id(), label(d.up())))
123+
.toList());
126124
}
127125

128126
private static String label(boolean up) {
129127
return up ? "Up" : "Down";
130128
}
131129

130+
/** Wire-shape DTO for the health endpoint. Component order defines JSON field order. */
131+
private record HealthBody(String outcome, List<DependencyBody> dependencies) {}
132+
133+
/** Wire-shape DTO for a single dependency entry. */
134+
private record DependencyBody(String id, String status) {}
135+
132136
/**
133137
* Serves a classpath resource. Content-Type is inferred from the file extension. The resource is
134138
* loaded eagerly; a missing resource fails immediately with {@link IllegalArgumentException}.

0 commit comments

Comments
 (0)