FasterXML / FasterXML/jackson-modules-base

NoCtorDeser module can not deserialize class derived from Throwable without default constructor

オープン
#342 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
no-ctor-deser
主要言語
Java
スター
180
フォーク
80
平均マージ
3時間 26分
マージ済み PR(30日)
1

説明

JDK: 25.0.2+10
Jackson version: 2.21.2

When using a custom exception without a default constructor, the NoCtorDeser module will construct it with the cause field set to null which will then make Throwable.initCause() throw an exception when deserialization tries to set cause field to its actual value. The exception that is thrown is a bit misleading as the exception "Caused by: java.lang.IllegalStateException: Can't overwrite cause with a null" is lost as it isn't the root cause.

```java
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.module.noctordeser.NoCtorDeserModule;

public class ExceptionDemo {
public static class DemoException extends Exception {
public DemoException(String message, Throwable cause) {
super(message, cause);
}
}

public static void main(String[] args) throws Exception {
DemoException demoEx = new DemoException("Error message", null);
JsonMapper mapper = JsonMapper.builder()
.addModule(new NoCtorDeserModule())
.build();

byte[] bytes = mapper.writerFor(DemoException.class).writeValueAsBytes(demoEx);
DemoException decoded = mapper.readValue(bytes, DemoException.class);

System.out.println(decoded);
}
}
```

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。