spring-projects / spring-projects/spring-boot

@ConfigurationProperties creates mutable collections, even on immutable classes

Open
#27,582 16 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement
Dominant language
Java
Stars
81.5k
Forks
42.7k
Avg merge
2d 4h
Merged PRs (30d)
65

Description

Consider simple example:

@ConfigurationProperties("some.prefix")
@ConstructorBinding
class Props {
  final List<String> strings;
  public Props(List<String> strings) {
    this.strings = strings;
  }
}

Even though I'm trying to make immutable class here, Spring will unhelpfully inject mutable ArrayList.

Now in a very simple example like this you can of course wrap it yourself in Collections.unmodifiableList() but doing so quickly becomes tedious especially when you nest collection types.

Imagine the code to deeply freeze e.g. Map<String, List<String>> :(

It's even worse when you just want to use lombok.Value or maybe Kotlin (didn't check it) to avoid writing boilerplate completely.

Not sure why these property classes should ever be mutable but if desired one can add property to @ConfigurationProperties to control it. It should automatically be enabled when using @ConstructorBinding though as that's a sure sign someone is trying to make immutable properties.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how @ConfigurationProperties and @ConstructorBinding create and populate collection values. Define the expected immutability behavior for constructor-bound properties, including nested collections and any opt-in control, then verify it with focused configuration-properties tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.