question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Binding to a collection passes in the current instance to the setter

See original GitHub issue

In 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:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Mar 3, 2018

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.

1reaction
philwebbcommented, Mar 2, 2018

Perhaps if there’s a setter we should try to create a new collection. Then only mutate an existing one if that fails?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found