Skip to content

Misleading exception for ConfigurationProperties when map key is handled by custom converter #18509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zeldigas opened this issue Oct 4, 2019 · 3 comments
Labels
status: duplicate A duplicate of another issue

Comments

@zeldigas
Copy link
Contributor

zeldigas commented Oct 4, 2019

Context

Spring boot version - 2.1.9

I've bumped into a problem with very misleading exception for ConfigurationProperties class.
Setup (for brevity, all getters and setters are omitted)

@Validated
public class AppProperties {

    @Valid
    private Map<KeyType, NestedProperty> myMap;

}
public interface KeyType {

}
public enum MyEnum implements KeyType {

    ONE, TWO

}
public class NestedProperty {

    @NotEmpty
    private String first;

    @NotEmpty
    private String second;

}
@ConfigurationPropertiesBinding
@Component
public class KeyTypeConverter implements Converter<String, KeyType> {

    @Override
    public KeyType convert(String s) {
        return MyEnum.valueOf(s);
    }

}

Configuration that fails validation, e.g. second is empty

test.my-map.ONE.first=hello
test.my-map.ONE.second=

Expected result

Message about nested property and it's failed constraint

APPLICATION FAILED TO START
***************************

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'test' to com.example.conversionproblem.AppProperties failed:

    Property: test.myMap[ONE].second
    Value: 
    Reason: must not be empty

Actual Result

I got this one:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'test' to com.example.conversionproblem.AppProperties:

    Property: test.my-map.one.second
    Value: 
    Origin: class path resource [application.properties]:3:0
    Reason: Cannot convert value of type 'java.lang.String' to required type 'com.example.conversionproblem.KeyType': no matching editors or conversion strategy found

Action:

Update your application's configuration

Extra info

I've create a sample project for you to look at the problem: https://github.com/zeldigas/spring-boot-conversion-problem

Also worth mentioning that I observe this behavior only with boot 2, boot 1.5 correctly reported issue about validation failure on second due to it's empty.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 4, 2019
@zeldigas zeldigas changed the title Misleading exception for ConfigurationProperties when key is handled by custom converter Misleading exception for ConfigurationProperties when map key is handled by custom converter Oct 4, 2019
@mbhave mbhave added the for: team-attention An issue we'd like other members of the team to review label Oct 4, 2019
@philwebb philwebb added type: bug A general bug and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Oct 11, 2019
@philwebb philwebb added this to the 2.1.x milestone Oct 11, 2019
@philwebb
Copy link
Member

With Spring Boot 2.2 the correct message is displayed:

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'test' to com.example.conversionproblem.AppProperties failed:

    Property: test.myMap[ONE].second
    Value: null
    Reason: must not be empty


Action:

Update your application's configuration

This is due to the changes we made for #17424. Those changes are quite involved so I'm not keen to back-port them to 2.1.x given the risk.

@zeldigas Can you try 2.2 with your real application and let me know if you still see the problem.

@philwebb philwebb added status: duplicate A duplicate of another issue and removed type: bug A general bug labels Oct 22, 2019
@philwebb philwebb removed this from the 2.1.x milestone Oct 22, 2019
@zeldigas
Copy link
Contributor Author

Hi @philwebb!

I confirm that using 2.2.0.RELEASE fixes the problem. It works for me and I assume that even my test app is good enough to proof the fix.

Thank you so much!

@philwebb
Copy link
Member

Thanks for confirming @zeldigas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants