Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17567

th:hasErrors() creates model attributes if not present (side-effect)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 5.1.2
    • Fix Version/s: Waiting for Triage
    • Component/s: Web
    • Labels:
      None

      Description

      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 -->
      </div>
      

      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: https://github.com/McNetic/gs-validating-form-input/commit/55a265fa0b8a2df4f60af211699d43633319fd88
       

      (I know the examples are not very meaningful)

        Attachments

          Activity

            People

            Assignee:
            bclozel Brian Clozel
            Reporter:
            McNetic Nicolai Ehemann
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Days since last comment:
              3 years, 23 weeks, 5 days ago