Skip to content

Commit dd3e286

Browse files
committed
Refine exception message
1 parent 793041b commit dd3e286

File tree

1 file changed

+10
-2
lines changed
  • spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind

1 file changed

+10
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/MapBinder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.boot.context.properties.source.IterableConfigurationPropertySource;
3232
import org.springframework.core.CollectionFactory;
3333
import org.springframework.core.ResolvableType;
34+
import org.springframework.core.env.PropertySource;
3435

3536
/**
3637
* {@link AggregateBinder} for Maps.
@@ -173,8 +174,15 @@ void bindEntries(ConfigurationPropertySource source, Map<Object, Object> map) {
173174
String key = sourceName.substring(index + 1);
174175
if (!key.equals(name.getLastElement(Form.ORIGINAL))
175176
&& !key.equalsIgnoreCase(name.getLastElement(Form.UNIFORM))) {
176-
throw new IllegalArgumentException("Please rewrite key '" + key + "' to '[" + key
177-
+ "]' and surround it with quotes if YAML is using");
177+
boolean forYaml = false;
178+
if (source.getUnderlyingSource() instanceof PropertySource<?> ps) {
179+
String propertySourceName = ps.getName();
180+
forYaml = propertySourceName.contains(".yaml") || propertySourceName.contains(".yml");
181+
}
182+
String validKey = (forYaml ? "\"[" : "[") + key + (forYaml ? "]\"" : "]");
183+
throw new IllegalArgumentException(
184+
"Please rewrite key '" + key + "' to '" + validKey + "'");
185+
178186
}
179187
}
180188
Bindable<?> valueBindable = getValueBindable(name);

0 commit comments

Comments
 (0)