I am using spring-boot 2.2.10.

This works:

@Data
@ConstructorBinding
@ConfigurationProperties(prefix = "someconfig")
public class SomeConfigProperties {
    private final NestedProperties nested;
    private final Integer value;
}

@Data
public class NestedProperties {
    private final Integer value;
}

I can supply the property by having environment variables SOMECONFIG_NESTED_VALUE=1 SOMECONFIG_VALUE=1 while starting the app.

However, the below does not work (note the dash-delimited prefix):

@Data
@ConstructorBinding
@ConfigurationProperties(prefix = "some-config")
public class SomeConfigProperties {
    private final NestedProperties nested;
    private final Integer value;
}

@Data
public class NestedProperties {
    private final Integer value;
}

With environment variables SOME_CONFIG_NESTED_VALUE=1 SOME_CONFIG_VALUE=1 during startup, I can only see the SomeConfigProperties#value initialized to the correct 1 value, while SomeConfigProperties#nested is set to null.

After some debug and trace, I believe it starts to go wrong from:

https://github.com/spring-projects/spring-boot/blob/d005a64c9f99c280f19ad37e8deb64968c965729/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java#L254

and

https://github.com/spring-projects/spring-boot/blob/d005a64c9f99c280f19ad37e8deb64968c965729/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java#L328

Comment From: wilkinsona

Have you tried it without the -, so using the same environment variables in both cases, as described in the documentation?

Comment From: gjgarryuan

Hey @wilkinsona, thanks for the response.

But my major concern is that, for the second case where the prefix is some-config, SOME_CONFIG_VALUE=1 works for the non-nested value, while SOME_CONFIG_NESTED_VALUE=1 does not work for the nested value.

Comment From: gjgarryuan

Interestingly, I have always been mapping the - in the property name to an _ in the environment variable and it always works...

Now that I know the rule of

Remove any dashes (-).

So is my current way of doing it (ie. mapping - in property name to _ in environment variable) just accidentally supported (ie. never officially supported)?

If that is the case I guess this is not an issue any more.

Comment From: wilkinsona

Some support for mapping - to _ was added in https://github.com/spring-projects/spring-boot/issues/10873. This was done to preserve backwards compatibility with Spring Boot 1.x but was focused on the property name rather than the prefix containing a -.

I'll mark this one for team attention so that we can discuss expanding that to supporting - mapping to _ in the prefix too. Irrespective of the outcome of that, I'd recommend following the documentation and removing - characters.

Comment From: gjgarryuan

Thanks for the clarification!

I will follow the documented way of environment variable binding from now on.

Comment From: wilkinsona

We've discussed this today and agreed that we don't want to add any extra complexity to further support the undocumented - to _ case. Instead, the translation should be performed as documented with - characters being removed rather than replaced.