Skip to content

Commit aa3e919

Browse files
mkurzmergify[bot]
authored andcommitted
Make sure Java's Session and Flash always exists even if empty req attrs
1 parent 0f9ce37 commit aa3e919

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

core/play/src/main/java/play/mvc/Http.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ default Long id() {
373373
* {@link Cell} to store the session cookie, to allow it to be evaluated on-demand.
374374
*/
375375
default Session session() {
376-
return attrs().get(RequestAttrKey.Session().asJava()).value().asJava();
376+
return attrs()
377+
.getOptional(RequestAttrKey.Session().asJava())
378+
.map(cell -> cell.value().asJava())
379+
.orElseGet(() -> new Session());
377380
}
378381

379382
/**
@@ -382,7 +385,10 @@ default Session session() {
382385
* store the flash, to allow it to be evaluated on-demand.
383386
*/
384387
default Flash flash() {
385-
return attrs().get(RequestAttrKey.Flash().asJava()).value().asJava();
388+
return attrs()
389+
.getOptional(RequestAttrKey.Flash().asJava())
390+
.map(cell -> cell.value().asJava())
391+
.orElseGet(() -> new Flash());
386392
}
387393

388394
/**

0 commit comments

Comments
 (0)