Skip to content

Commit 19c5864

Browse files
committed
Clarify square bracket map binding notation
Update reference docs to make it clearer when the square bracket map binding notation should be used. Closes gh-23390
1 parent 549d4f7 commit 19c5864

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,23 +1278,29 @@ TIP: We recommend that, when possible, properties are stored in lower-case kebab
12781278

12791279
[[boot-features-external-config-relaxed-binding-maps]]
12801280
===== Binding Maps
1281-
When binding to `Map` properties, if the `key` contains anything other than lowercase alpha-numeric characters or `-`, you need to use the bracket notation so that the original value is preserved.
1282-
If the key is not surrounded by `[]`, any characters that are not alpha-numeric or `-` are removed.
1283-
For example, consider binding the following properties to a `Map`:
1281+
When binding to `Map` properties you may need to use a special bracket notation so that the original `key` value is preserved.
1282+
If the key is not surrounded by `[]`, any characters that are not alpha-numeric, `-` or `.` are removed.
1283+
1284+
For example, consider binding the following properties to a `Map<String,String>`:
12841285

12851286
[source,yaml,indent=0]
12861287
----
12871288
acme:
12881289
map:
1289-
"[/key1]": value1
1290-
"[/key2]": value2
1291-
/key3: value3
1290+
"[/key1]": "value1"
1291+
"[/key2]": "value2"
1292+
"/key3": "value3"
12921293
12931294
----
12941295

1296+
NOTE: For YAML files, the brackets need to be surrounded by quotes for the keys to be parsed properly.
1297+
12951298
The properties above will bind to a `Map` with `/key1`, `/key2` and `key3` as the keys in the map.
1299+
The slash has been removed from `key3` because it wasn't surrounded by square brackets.
1300+
1301+
You may also occasionally need to use the bracket notation if your `key` contains a `.` and you are binding to non-scalar value.
1302+
For example, binding `a.b=c` to `Map<String, Object>` will return a Map with the entry `{"a"={"b"="c"}}` where as `[a.b]=c` will return a Map with the entry `{"a.b"="c"}`.
12961303

1297-
NOTE: For YAML files, the brackets need to be surrounded by quotes for the keys to be parsed properly.
12981304

12991305

13001306
[[boot-features-external-config-relaxed-binding-from-environment-variables]]

0 commit comments

Comments
 (0)