Skip to content

Using types in @SessionAttributes is sometimes not reflected in the model #30463

@yu-shiba

Description

@yu-shiba

@SessionAttributes stores the session object specified by the following attributes in the Model for each method of the annotated Controller.

  • names
  • types

The process is implemented in SessionAttributesHandler#retrieveAttributes(WebRequest), but sometimes it is not reflected in the Model because it only looks at knownAttributeNames.
The object specified in types is not initially defined in knownAttributeNames, but is added to knownAttributeNames when DI by a controller method.

Instances of SessionAttributesHandler are cached, so information added to knownAttributeNames is preserved, but it is only valid within the same VM since it is not stored in the session.

Therefore, if different methods of the same Controller are executed on different servers, the expected behavior will not occur because the knownAttributeNames state will be different.

e.g.) If handle1 and handle2 below are executed on different servers, Pet is not stored in handle2's model.

@Controller
@SessionAttributes(types = { Pet.class }) 
public class EditPetForm {
    // ...

    @PostMapping("...")
    public String handle1(Pet pet, BindingResult errors) {
    }

    @PostMapping("...")
    public String handle2(Model model) {
        
    }
}

Defining the above in names instead of types will result in the expected behavior on different servers.
I do not think this behavior is what is expected.

Note that this problem does not occur when SessionAttributesHandler is the same on all servers.
The problem is easily missed because it occurs immediately after a server restart when the SessionAttributesHandler cache is different.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: feedback-providedFeedback has been providedtype: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions