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

th:hasErrors() creates model attributes if not present (side-effect) [SPR-17567] #22099

spring-issuemaster opened this issue Dec 5, 2018 · 0 comments


Copy link

@spring-issuemaster spring-issuemaster commented Dec 5, 2018

Nicolai Ehemann opened SPR-17567 and commented

I'm not 100% sure this is a problem of spring core or perhaps thymeleaf (due to my lack of knowledge of internals), but I suspect the problem is actually with spring:

Calling th:hasErrors() in a thymeleaf template can have the potentially unwanted side-effect of creating model attributes while evaluating the thymeleaf template.

One way I found to reproduce this is using uninitialized nested model attributes. If I have a model attribute that has another object as a field, and that field is not initialized per construction, it will be initialized upon calling th:hasErrors() on it.

For example, I have a model attribute person with a field address which in turn has a field street. Consider the following thymeleaf snippet:

<div th:object="${person}>
  <span th:text="*{address}"/><!-- adress is null -->
  <span th:text="${#fields.hasErrors('address.street')}"/>
  <span th:text="*{address}"/><!-- adress is now an object -->

I don't think hasErrors() should have such unexpected side effects.

I made a minimal modification to the spring example project gs-validating-form-input to demonstrate/reproduce the issue: McNetic/gs-validating-form-input@55a265f

(I know the examples are not very meaningful)

Affects: 5.1.2

Reference URL: McNetic/gs-validating-form-input@55a265f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.