Skip to content

Commit c139f3d

Browse files
committed
Add generic properties map to ProblemDetail
Closes gh-28665
1 parent b72ee5f commit c139f3d

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

spring-web/src/main/java/org/springframework/http/ProblemDetail.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.http;
1818

1919
import java.net.URI;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
2022

2123
import org.springframework.lang.Nullable;
2224
import org.springframework.util.Assert;
@@ -54,6 +56,9 @@ public class ProblemDetail {
5456
@Nullable
5557
private URI instance;
5658

59+
@Nullable
60+
private Map<String, Object> properties;
61+
5762

5863
/**
5964
* Protected constructor for subclasses.
@@ -75,6 +80,7 @@ protected ProblemDetail(ProblemDetail other) {
7580
this.status = other.status;
7681
this.detail = other.detail;
7782
this.instance = other.instance;
83+
this.properties = (other.properties != null ? new LinkedHashMap<>(other.properties) : null);
7884
}
7985

8086
/**
@@ -201,6 +207,18 @@ public void setInstance(@Nullable URI instance) {
201207
this.instance = instance;
202208
}
203209

210+
/**
211+
* Set a "dynamic" property to be added to a generic {@link #getProperties()
212+
* properties map}.
213+
* @param name the property name
214+
* @param value the property value
215+
*/
216+
public void setProperty(String name, Object value) {
217+
this.properties = (this.properties != null ? this.properties : new LinkedHashMap<>());
218+
this.properties.put(name, value);
219+
}
220+
221+
204222

205223
// Getters
206224

@@ -249,6 +267,14 @@ public URI getInstance() {
249267
return this.instance;
250268
}
251269

270+
/**
271+
* Return a generic map of properties that are not known ahead of time.
272+
*/
273+
@Nullable
274+
public Map<String, Object> getProperties() {
275+
return this.properties;
276+
}
277+
252278

253279
@Override
254280
public String toString() {
@@ -264,7 +290,8 @@ protected String initToStringContent() {
264290
", title='" + getTitle() + "'" +
265291
", status=" + getStatus() +
266292
", detail='" + getDetail() + "'" +
267-
", instance='" + getInstance() + "'";
293+
", instance='" + getInstance() + "'" +
294+
", properties='" + getProperties() + "'";
268295
}
269296

270297

0 commit comments

Comments
 (0)