odoo/odoo#109822

Created by chienandalu
Closed
label
Tecnativa:15.0-fix-js-legacy-context-setter
head
120fd89c2401d9d93fc382eb7e7627976f637dab
target
15.0

[15.0][FIX] web: legacy user_context setter

When writing extension modules it's possible to override the session context so we can achieve effects on the overriden functions orm calls. This is very handy to avoid ugly patches that doesn't respect proper inheritance. This can be done using the `user` service and its `updateContext()` function.

Since v15, this is not possible for legacy widgets, as the `user_context` key in the session is behind a getter. Before, this was possible:

```javascript
Object.assign(session.user_context, {"new_key": "new_value"});
```

With this patch, a slight variation would be possible:

```javascript
session.user_context = Object.assign(
{}, session.user_context, {"new_key": "new_value"}
);
```

Some extra context on the user_context thing (no pun intended :slightly_smiling_face: ): this would be a real world use case for the attendance action where we need to add some extra values to the attendances when the user clicks on the check in/out button:

https://github.com/OCA/hr-attendance/blob/3a9237e035a01dbe28378c8201669d995fe163d0/hr_attendance_geolocation/static/src/js/attendance_geolocation.js#L42-L53

The previous code was this and it avoided another modules adding more extra info as the original function was being completely shadowed.

https://github.com/OCA/hr-attendance/blob/17c3b2797e9e1fb8554e5af57dca7aedc66b0f4a/hr_attendance_geolocation/static/src/js/attendance_geolocation.js#L83-L133

Another option was to use a base module that could allow to hook into the function to add the extra context but that would imply shadowing the core function as well with all the issues that could come along with that design.

cc @Tecnativa


---
I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr