Binding to a collection passes in the current instance to the setter
See original GitHub issueIn a @ConfigurationProperties
bean if you bind to a collection Spring Boot binds the value and calls the setter (with the same instance). It’s a bit confusing and probably unexpected.
Failing test:
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "app.values=foo")
public class BindingApplicationTests {
@Autowired
private ConfigProps props;
@Test
public void contextLoads() {
assertThat(props.getValues()).contains("foo");
}
}
@ConfigurationProperties("app")
@Component
class ConfigProps {
private Set<String> values = new HashSet<>();
public Set<String> getValues() {
return values;
}
public void setValues(Set<String> values) {
this.values.clear();
this.values.addAll(values);
}
}
Set a debug breakpoint in the setter to see what I mean.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Problem applying binding to style setter value in XAML and ...
I cannot pass in the property name from the XAML binding at least, because I am using a dynamic data source and only...
Read more >Binding value from an ObservableObject - Apple Developer
isReadyForSale) //Throws a compilation error and rightly so, but how to pass it as a Binding variable ? PlaygroundPage.current.setLiveView(button).
Read more >Binded Propertys' Setter called before View finished Loading
Firstly, are changes to the SelectedPivotItemIndex in the view being reflected in the ViewModel i.e is the two way binding working correctly?
Read more >two-way binding on variable with setter causes initial call to ...
I have a pretty involved grid component that uses two-way binding to ... (eg the binding instance) resulting in sourceExpression.assign .
Read more >Two-way data binding - Android Developers
Because the bindable property's getter method is called getRememberMe() , the property's corresponding setter method automatically uses the name ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Changing the behaviour based on whether or not we can create a new collection sounds confusing to me.
FWIW, I think the setter in the test above is broken. It’s not a setter method as it doesn’t actually set the field’s value. I think it’s perfectly reasonable for the binder to expect that when it calls a setter, the passed in value will actually be set. It’s easy to describe and should be easy for users to implement.
Perhaps if there’s a setter we should try to create a new collection. Then only mutate an existing one if that fails?