Skip to content

[OP-19966] Fix 500 when creating working hours with a nil weekday - #24773

Open
jtauschl wants to merge 2 commits into
opf:devfrom
jtauschl:fix/user-working-hours-nil-weekday-crash
Open

[OP-19966] Fix 500 when creating working hours with a nil weekday#24773
jtauschl wants to merge 2 commits into
opf:devfrom
jtauschl:fix/user-working-hours-nil-weekday-crash

Conversation

@jtauschl

@jtauschl jtauschl commented Aug 14, 2026

Copy link
Copy Markdown

Ticket

https://community.openproject.org/wp/OP-19966

Summary

The presence/numericality validation on each *_hours attribute invokes the corresponding dynamically-defined getter, which unconditionally divided the raw (possibly nil) minutes column by 60.0 -- a weekday omitted from a create payload has no DB default and stays nil, so validating it crashed with NoMethodError instead of producing a clean 422. A second, related nil-unsafe site exists in at_least_one_working_day_selected, reachable once every weekday is nil at once.

Change

Make both sites nil-safe: the getter returns nil for a nil column instead of dividing it, and the working-day check coerces via to_i first.

Test plan

  • Added regression specs for a single nil weekday and for all seven weekdays nil, at both the model and hours-accessor level
  • Verified live against a real 17.7.1 instance: before the fix, omitting one weekday → 500; after the fix → 422 with a clean field error; omitting all seven → 500 before the fix, 422 with the "at least one working day" message after; sending all 7 fields still succeeds unaffected

The presence/numericality validation on each *_hours attribute invokes
the corresponding dynamically-defined getter, which unconditionally
divided the raw (possibly nil) minutes column by 60.0 -- a weekday
omitted from a create payload has no DB default and stays nil, so
validating it crashed with NoMethodError instead of producing a clean
422. A second, related nil-unsafe site exists in
at_least_one_working_day_selected, reachable once every weekday is nil
at once.

Make both sites nil-safe: the getter returns nil for a nil column
instead of dividing it, and the working-day check coerces via to_i
first.
@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@jtauschl

Copy link
Copy Markdown
Author

recheck

@myabc
myabc requested a lite review from Copilot August 15, 2026 18:34
@myabc myabc changed the title Fix 500 when creating working hours with a nil weekday [OP-19966] Fix 500 when creating working hours with a nil weekday Aug 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a server error when UserWorkingHours weekday minute columns are nil (e.g., when weekday fields are omitted from a create payload) by making the dynamically generated *_hours getters and the at_least_one_working_day_selected validation nil-safe, and adds regression coverage to prevent reintroducing the crash.

Changes:

  • Return nil from {day}_hours when the underlying {day} minutes column is nil (instead of raising on nil / 60.0).
  • Make at_least_one_working_day_selected robust to nil weekday values via to_i.
  • Add model/accessor regression specs for single-day nil and all-days nil scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
app/models/user_working_hours.rb Makes weekday hour getters and “at least one working day” validation nil-safe to avoid 500s on incomplete payloads.
spec/models/user_working_hours_spec.rb Adds regression specs covering nil weekday columns and nil-safe *_hours getters.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 74 to 77
define_method("#{day}_hours") do
(public_send(day) / 60.0).round(2)
minutes = public_send(day)
minutes.nil? ? nil : (minutes / 60.0).round(2)
end
UserWorkingHours#{day}_hours now returns nil for a day with no minutes
value at all (this branch's own earlier fix), but this form component
called .round(2) on it unconditionally -- re-rendering the create form
after a validation failure that left a weekday nil crashed with
NoMethodError instead of showing the validation error banner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants