Skip to content
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

Configuration properties validation is propagated to non-@Valid fields #40345

Open
LeMikaelF opened this issue Apr 14, 2024 · 0 comments
Open
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@LeMikaelF
Copy link

LeMikaelF commented Apr 14, 2024

When a JSR 303 implementation is on the classpath, Spring Boot validates all Configuration Properties classes that are annotated with @Validated (see ConfigurationPropertiesBinder::getValidators). But the validation applies to all nested objects, even if they're not annotated with @Valid. This doesn't comply with the JSR 303 spec (see the notion of traversable properties) and can lead to some surprising results. For example, the bean below will fail validation given a single property props.nested.myprop1=value, even though the field private Nested nested isn't annotated with @Valid. Likewise, nested collections and maps will fail validation if they contain objects that are themselves annotated with jsr 303 annotations (ex: @NotEmpty), even if validation isn't cascaded with an @Valid. See this reproducer.

@Data
@Validated
@ConfigurationProperties(prefix = "props")
static class ConfigPropsClass {

    private Nested nested;

    @Data
    static class Nested {

        String myprop1;

        @NotEmpty String myprop2;

    }

}

The documentation is also somewhat confusing for this. It states:

To ensure that validation is always triggered for nested properties, even when no properties are found, the associated field must be annotated with @Valid.

But nested objects will not be validated if there are no properties to bind. For example, adding @Valid to the private Nested nested field above will not result in a failure if there is no props.nested.myprop1 property.

I can think of two solutions:

  1. Propagate validation to @Valid fields only.
  2. Modify the documentation to explicitly say that all nested objects will be validated if the configuration properties class is @Validated.

I personally prefer option 1, because it corresponds to behaviour that is expected from a JSR 303 validator.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 14, 2024
@philwebb philwebb added for: team-meeting An issue we'd like to discuss as a team to make progress and removed for: team-meeting An issue we'd like to discuss as a team to make progress labels Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants