exceptions: Jackson 3 unchecked JacksonException hierarchy, Spring Boot 4.2 error handling, OpenRewrite and sed migration

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b4HkWoC7hLzk61vZa8
This commit is contained in:
Claude
2026-12-03 23:41:56 +05:30
parent 26f64bc184
commit f97ec4e014
71 changed files with 2210 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- A small, ordinary Jackson 2 codebase. The migration scripts work on a COPY of it. -->
<groupId>com.ankurm</groupId>
<artifactId>orders-jackson2</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,20 @@
package com.ankurm.orders;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import tools.jackson.core.JacksonException;
/** FIX: catches JacksonException; IOException stays only for the stream's close(). */
public class Importer {
private final OrderCodec codec = new OrderCodec();
public String importOne(String json) {
try (var in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) {
return "imported " + codec.read(in).id();
} catch (IOException | JacksonException e) {
return "rejected";
}
}
}
@@ -0,0 +1,3 @@
package com.ankurm.orders;
public record Order(long id, int quantity) {}
@@ -0,0 +1,71 @@
package com.ankurm.orders;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import tools.jackson.core.JacksonException;
import tools.jackson.core.exc.StreamReadException;
import tools.jackson.databind.DatabindException;
/** The OpenRewrite output, plus the two hand fixes marked FIX. */
public class OrderCodec {
private final ObjectMapper mapper = new JsonMapper();
/** 1. declare it */
public Order parse(String json) {
return mapper.readValue(json, Order.class);
}
/** 2. catch the subtypes, most specific first */
public String diagnose(String json) {
try {
mapper.readValue(json, Order.class);
return "ok";
} catch (StreamReadException e) {
return "not JSON";
} catch (DatabindException e) {
return "bad field " + e.getPath().get(0).getPropertyName();
} catch (JacksonException e) {
return "other";
}
}
/** 3. catch IOException around a call that only Jackson can fail */
public Order orDefault(String json) {
try {
return mapper.readValue(json, Order.class);
} catch (JacksonException e) {
return new Order(0, 0);
}
}
/** 4. one catch for the file read and the parse */
public Order load(Path file) {
try {
String json = Files.readString(file);
return mapper.readValue(json, Order.class);
} catch (JacksonException | IOException e) {
return new Order(0, 0);
}
}
/** 5. FIX: dropped "throws IOException", so callers can no longer believe it covers bad JSON */
public Order read(InputStream in) {
return mapper.readValue(in, Order.class);
}
/** 6. FIX: convertValue now throws a JacksonException, not IllegalArgumentException */
public boolean isOrder(Map<String, Object> map) {
try {
mapper.convertValue(map, Order.class);
return true;
} catch (JacksonException e) {
return false;
}
}
}
@@ -0,0 +1,46 @@
package com.ankurm.orders;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/** Pins the behaviour before the upgrade. The migrated copy has to pass the same assertions. */
class OrderCodecTest {
static final String BAD = "{\"id\": 1, \"quantity\": }";
final OrderCodec codec = new OrderCodec();
@Test
void diagnoses() {
assertEquals("not JSON", codec.diagnose(BAD));
assertEquals("bad field quantity", codec.diagnose("{\"id\":1,\"quantity\":\"two\"}"));
}
@Test
void fallsBackOnBadText() {
assertEquals(new Order(0, 0), codec.orDefault(BAD));
}
@Test
void fallsBackOnBadFile(@TempDir Path dir) throws IOException {
Path file = Files.writeString(dir.resolve("order.json"), BAD);
assertEquals(new Order(0, 0), codec.load(file));
}
@Test
void callerRejectsBadStream() {
assertEquals("rejected", new Importer().importOne(BAD));
}
@Test
void rejectsBadMap() {
assertFalse(codec.isOrder(Map.of("id", "abc")));
}
}