Skip to content

Setting ignoreInvalidFields=true on @ConfigurationProperties causes unknown fields to be ignored as well #22308

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
brucelwl opened this issue Jul 12, 2020 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@brucelwl
Copy link

brucelwl commented Jul 12, 2020

mybatis.mapperLocations= classpath*:mapper/*.xml
mybatis.defaultStatementTimeoutInSecond= 5
mybatis.mapUnderscoreToCamelCase= false
mybatis.unknownFields= exception
@ConfigurationProperties(prefix = "mybatis", ignoreInvalidFields = false, ignoreUnknownFields = false)
public class MyBatisProperties {
    private String basePackage;
    private String mapperLocations;
    private String typeAliasesPackage;
    private String markerInterface;
    private Integer defaultStatementTimeoutInSecond;
    private Boolean mapUnderscoreToCamelCase;
    private String configLocation;
}

Throw an exception

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

Description:

Binding to target [Bindable@450794b4 type = com.example.demo.config.MyBatisProperties, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=false, ignoreUnknownFields=false, prefix=mybatis, value=mybatis)]] failed:

    Property: mybatis.unknownFields
    Value: false
    Origin: class path resource [application.properties]:7:13
    Reason: The elements [mybatis.aa] were left unbound.

but

@ConfigurationProperties(prefix = "mybatis", ignoreInvalidFields = true, ignoreUnknownFields = false)
public class MyBatisProperties {
  //.......
}

It works fine, but I think it's time to throw an exception

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 12, 2020
@wilkinsona wilkinsona changed the title "ignoreInvalidFields = true, ignoreUnknownFields = false" the combination is invalid Setting ignoreInvalidFields=true on @ConfigurationProperties causes unknown fields to be ignored as well Jul 13, 2020
@wilkinsona
Copy link
Member

Thanks for the report. This looks like a bug to me.

The problem can be reproduced by modifying ConfigurationPropertiesTests to add ignoreInvalidFields = true to @ConfigurationProperties on IgnoreUnknownFieldsFalseProperties:

@ConfigurationProperties(ignoreUnknownFields = false, ignoreInvalidFields = true)
static class IgnoreUnknownFieldsFalseProperties extends BasicProperties {

}

With this change in place loadWhenHasIgnoreUnknownFieldsFalseAndUnknownFieldsShouldFail will fail. IgnoreErrorsBindHandler is catching and ignoring the UnboundConfigurationPropertiesException. I think we may be able to fix it by overriding onFailure in NoUnboundElementsBindHandler to prevent UnboundConfigurationPropertiesException from being ignored:

@Override
public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error)
		throws Exception {
	if (error instanceof UnboundConfigurationPropertiesException) {
		throw error;
	}
	return super.onFailure(name, target, context, error);
}

Flagging for team attention to double-check my reasoning and discuss which release we should fix this in.

@wilkinsona wilkinsona added for: team-attention An issue we'd like other members of the team to review type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 13, 2020
@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label Jul 13, 2020
@philwebb philwebb added this to the 2.2.x milestone Jul 13, 2020
YangfanCheng1 pushed a commit to YangfanCheng1/spring-boot that referenced this issue Jul 23, 2020
Exception should be thrown if there are unknown external properties and its corresponding @ConfigurationProperties has attributes 'ignoreUnknownFields' as false and 'ignoreInvalidFields' as true

Fixes spring-projectsgh-22308
@YangfanCheng1
Copy link

I had ran into the same issue and I thought it was expected behavior. Open a PR using what @wilkinsona suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants