Skip to content

ConfigurationProperties, Records, Optional and Validation #27141

@lpandzic

Description

@lpandzic

I'm trying to wrap my head around how to use configuration properties functionality of Spring Boot while trying to keep things concise, robust and safe.
What I'd like to have is a configuration properties definition that is:

  • immutable (final fields and class)
  • concise (records)
  • robust (validated)
  • safe (primarily null safe by using both validation and Optional).

Something like

@Validated
@ConfigurationProperties("fooBar")
@ConstructorBinding
record FooBarProperties(@NotNull Foo foo,
                        Optional<Bar> bar) {
    
}

Due to limitations of records of keeping exact types for accessors and parameters user is forced to use Optional on record parameter definitions. My understanding is this clashes with

The use of java.util.Optional with @ConfigurationProperties is not recommended as it is primarily intended for use as a return type. As such, it is not well-suited to configuration property injection. For consistency with properties of other types, if you do declare an Optional property and it has no value, null rather than an empty Optional will be bound.

Can support for this use case be added?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions