Skip to content

Commit 875eeab

Browse files
committed
Add a properties setter to ProblemDetail
Mainly to allow Kotlin idiomatic properties assignment. Closes gh-31430
1 parent 7a60e20 commit 875eeab

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ public void setProperty(String name, @Nullable Object value) {
216216
this.properties.put(name, value);
217217
}
218218

219+
/**
220+
* Setter for the {@link #getProperties() properties map}.
221+
* <p>By default, this is not set.
222+
* <p>When Jackson JSON is present on the classpath, any properties set here
223+
* are rendered as top level key-value pairs in the output JSON. Otherwise,
224+
* they are rendered as a {@code "properties"} sub-map.
225+
* @param properties the properties map
226+
*/
227+
public void setProperties(@Nullable Map<String, Object> properties) {
228+
this.properties = properties;
229+
}
230+
219231
/**
220232
* Return a generic map of properties that are not known ahead of time,
221233
* possibly {@code null} if no properties have been added. To add a property,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.http
17+
18+
import org.assertj.core.api.Assertions.assertThat
19+
import org.junit.jupiter.api.Test
20+
21+
/**
22+
* Kotlin tests for [ProblemDetail].
23+
*/
24+
class KotlinProblemDetailTests {
25+
26+
@Test // gh-31430
27+
fun propertiesSetter() {
28+
val problemDetail = ProblemDetail()
29+
val map = mapOf("foo" to "bar")
30+
problemDetail.properties = map
31+
assertThat(problemDetail.properties).isEqualTo(map)
32+
}
33+
34+
}

0 commit comments

Comments
 (0)