Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,13 @@ However, test dependencies must not accidentally become runtime dependencies of
JaCoCo (`jacoco.version` in the parent POM) is wired report-only: every
`./mvnw clean verify` writes per-module reports to `target/site/jacoco` without
failing the build. There are no coverage gates; use the reports during review
to spot untested branches. One known artifact: single-statement lines that
always throw (for example a guard delegating to a method that only throws)
execute but never complete, so JaCoCo reports them as missed despite a passing
test proving otherwise.
to spot untested branches. Two known artifacts, both verified by other means
and deliberately left alone: single-statement lines that always throw (for
example a guard delegating to a method that only throws) execute but never
complete, so JaCoCo reports them as missed despite a passing test proving
otherwise; and `this == obj` fast-path guards in `equals`, which only a
self-comparison could cover — a test SonarQube rightly flags as a bug, so the
partial branch stands.

### Completion Requirement

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.othmaneataallah.problemdetails.core;

import java.io.Serializable;
import java.net.URI;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -33,6 +34,10 @@
* be reused to create several instances; values are copied at {@link Builder#build() build} time,
* so later builder mutations never affect already-built instances.
*
* <p>Instances are {@link Serializable} so problem details can cross process boundaries (for
* example inside a serialized {@link ProblemDetailException}); extension <em>values</em> travel
* along, so they should be serializable too.
*
* <pre>{@code
* ProblemDetail problem =
* ProblemDetail.builder()
Expand All @@ -44,7 +49,9 @@
* .build();
* }</pre>
*/
public final class ProblemDetail {
public final class ProblemDetail implements Serializable {

private static final long serialVersionUID = 1L;

/** The problem type assumed when {@code type} is absent (RFC 9457, Section 3.1.1). */
public static final URI ABOUT_BLANK = URI.create("about:blank");
Expand All @@ -54,19 +61,26 @@ public final class ProblemDetail {
private final Integer status;
private final String detail;
private final URI instance;
private final Map<ProblemDetailKey<?>, Object> extensions;
private final Map<String, Object> extensionMembers;

// S1948 is suppressed by design: the declared Map types are not Serializable, but the
// runtime instances always are — and extension values remain the caller's responsibility,
// as documented on the class.
@SuppressWarnings("java:S1948")
private final LinkedHashMap<ProblemDetailKey<?>, Object> extensions;

@SuppressWarnings("java:S1948")
private final LinkedHashMap<String, Object> extensionMembers;

private ProblemDetail(Builder builder) {
this.type = builder.type;
this.title = builder.title;
this.status = builder.status;
this.detail = builder.detail;
this.instance = builder.instance;
this.extensions = Collections.unmodifiableMap(new LinkedHashMap<>(builder.extensions));
Map<String, Object> members = new LinkedHashMap<>();
this.extensions = new LinkedHashMap<>(builder.extensions);
LinkedHashMap<String, Object> members = new LinkedHashMap<>();
this.extensions.forEach((key, value) -> members.put(key.name(), value));
this.extensionMembers = Collections.unmodifiableMap(members);
this.extensionMembers = members;
}

/**
Expand Down Expand Up @@ -173,10 +187,11 @@ public boolean has(ProblemDetailKey<?> key) {
* <p>This view exists for serialization modules (JSON, XML), which must write members the typed
* API does not know statically. Application code should prefer {@link #get(ProblemDetailKey)}.
*
* @return an unmodifiable snapshot of the extension members; never null
* @return an unmodifiable view of the extension members; never null, and safe to retain since the
* underlying data never changes
*/
public Map<String, Object> extensionMembers() {
return extensionMembers;
return Collections.unmodifiableMap(extensionMembers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ProblemDetailException extends RuntimeException {

private static final long serialVersionUID = 1L;

private static final String PROBLEM_DETAIL_PARAMETER = "problemDetail";

private final ProblemDetail problemDetail;

/**
Expand All @@ -31,7 +33,7 @@ public class ProblemDetailException extends RuntimeException {
*/
public ProblemDetailException(ProblemDetail problemDetail) {
super(messageFor(problemDetail));
this.problemDetail = Objects.requireNonNull(problemDetail, "problemDetail");
this.problemDetail = Objects.requireNonNull(problemDetail, PROBLEM_DETAIL_PARAMETER);
}

/**
Expand All @@ -43,7 +45,7 @@ public ProblemDetailException(ProblemDetail problemDetail) {
*/
public ProblemDetailException(ProblemDetail problemDetail, Throwable cause) {
super(messageFor(problemDetail), cause);
this.problemDetail = Objects.requireNonNull(problemDetail, "problemDetail");
this.problemDetail = Objects.requireNonNull(problemDetail, PROBLEM_DETAIL_PARAMETER);
}

/**
Expand All @@ -56,7 +58,7 @@ public ProblemDetail getProblemDetail() {
}

private static String messageFor(ProblemDetail problemDetail) {
Objects.requireNonNull(problemDetail, "problemDetail");
Objects.requireNonNull(problemDetail, PROBLEM_DETAIL_PARAMETER);
if (problemDetail.getDetail() != null) {
return problemDetail.getDetail();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.othmaneataallah.problemdetails.core;

import java.io.Serializable;
import java.util.Objects;

/**
Expand All @@ -26,7 +27,9 @@
*
* @param <T> the Java type of the extension member value
*/
public final class ProblemDetailKey<T> {
public final class ProblemDetailKey<T> implements Serializable {

private static final long serialVersionUID = 1L;

private final String name;
private final Class<T> type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URI;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -47,8 +51,33 @@ void messagePrefersDetailThenTitleThenType() {
.isEqualTo(URI.create("about:blank").toString());
}

@Test
void survivesJavaSerialization() throws Exception {
ProblemDetailException original =
new ProblemDetailException(
ProblemDetail.builder()
.title("Title.")
.status(400)
.extension(ProblemDetailKey.of("balance", Integer.class), 30)
.build());

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(bytes)) {
out.writeObject(original);
}
ProblemDetailException readBack;
try (ObjectInputStream in =
new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
readBack = (ProblemDetailException) in.readObject();
}

assertThat(readBack.getProblemDetail()).isEqualTo(original.getProblemDetail());
assertThat(readBack.getMessage()).isEqualTo(original.getMessage());
}

@Test
void nullProblemDetailIsRejected() {

assertThatThrownBy(() -> new ProblemDetailException(null))
.isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new ProblemDetailException(null, new RuntimeException()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,9 @@ void equalityIsByNameOnly() {
assertThat(ProblemDetailKey.of("balance", Integer.class))
.isNotEqualTo(ProblemDetailKey.of("other", Integer.class));
assertThat(ProblemDetailKey.of("balance", Integer.class)).isNotEqualTo("balance");
assertThat(ProblemDetailKey.of("balance", Integer.class)).isNotEqualTo(null);
assertThat(ProblemDetailKey.of("balance", Integer.class).hashCode())
.isEqualTo(ProblemDetailKey.of("balance", String.class).hashCode());
}

@Test
void selfEquality() {
ProblemDetailKey<Integer> key = ProblemDetailKey.of("balance", Integer.class);

assertThat(key).isEqualTo(key);
assertThat(ProblemDetailKey.of("balance", Integer.class)).isNotNull();
assertThat(ProblemDetailKey.of("balance", Integer.class))
.hasSameHashCodeAs(ProblemDetailKey.of("balance", String.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void builderDefaultsToAboutBlankWithAbsentMembers() {

@Test
void aboutBlankConstantMatchesDefaultType() {
assertThat(ProblemDetail.ABOUT_BLANK).isEqualTo(URI.create("about:blank"));
assertThat(URI.create("about:blank")).isEqualTo(ProblemDetail.ABOUT_BLANK);
assertThat(ProblemDetail.builder().build().getType()).isEqualTo(ProblemDetail.ABOUT_BLANK);
}

Expand Down Expand Up @@ -184,14 +184,12 @@ void valueSemantics() {
ProblemDetail.builder().title("Title").status(400).extension(balance, 30).build();
ProblemDetail different = ProblemDetail.builder().title("Other").status(400).build();

assertThat(first).isEqualTo(first);
assertThat(first).isEqualTo(second);
assertThat(first)
.isEqualTo(second)
.hasSameHashCodeAs(second)
.isNotEqualTo(different)
.isNotNull();
assertThat(second).isEqualTo(first);
assertThat(first.hashCode()).isEqualTo(second.hashCode());
assertThat(first).isNotEqualTo(different);
assertThat(first).isNotEqualTo(null);
assertThat(first).isNotEqualTo("not a problem detail");
assertThat(first.toString()).contains("Title", "400", "balance=30");
}

@Test
Expand All @@ -216,8 +214,7 @@ void inequalityIsDetectedPerMember() {
.detail("Detail")
.instance("/instances/1")
.extension(balance, 30)
.build());
assertThat(base)
.build())
.isNotEqualTo(
ProblemDetail.builder()
.type("https://example.com/probs/a")
Expand All @@ -226,8 +223,7 @@ void inequalityIsDetectedPerMember() {
.detail("Detail")
.instance("/instances/1")
.extension(balance, 30)
.build());
assertThat(base)
.build())
.isNotEqualTo(
ProblemDetail.builder()
.type("https://example.com/probs/a")
Expand All @@ -236,8 +232,7 @@ void inequalityIsDetectedPerMember() {
.detail("Detail")
.instance("/instances/1")
.extension(balance, 30)
.build());
assertThat(base)
.build())
.isNotEqualTo(
ProblemDetail.builder()
.type("https://example.com/probs/a")
Expand All @@ -246,8 +241,7 @@ void inequalityIsDetectedPerMember() {
.detail("Other")
.instance("/instances/1")
.extension(balance, 30)
.build());
assertThat(base)
.build())
.isNotEqualTo(
ProblemDetail.builder()
.type("https://example.com/probs/a")
Expand All @@ -256,8 +250,7 @@ void inequalityIsDetectedPerMember() {
.detail("Detail")
.instance("/instances/2")
.extension(balance, 30)
.build());
assertThat(base)
.build())
.isNotEqualTo(
ProblemDetail.builder()
.type("https://example.com/probs/a")
Expand Down Expand Up @@ -294,8 +287,7 @@ void toStringRendersAllPresentMembers() {

@Test
void toStringOmitsAbsentMembers() {
assertThat(ProblemDetail.builder().build().toString())
.isEqualTo("ProblemDetail[type=about:blank]");
assertThat(ProblemDetail.builder().build()).hasToString("ProblemDetail[type=about:blank]");
}

@Test
Expand Down
Loading
Loading