Skip to content

Render nullable boolean columns with default value of false as not optional on rails 8.1 (like previous versions of rails did) - #36

Merged
ElMassimo merged 3 commits into
ElMassimo:mainfrom
h-lame:support-default-false-on-rails-8-1
Jul 3, 2026
Merged

ElMassimo merged 3 commits into
ElMassimo:mainfrom
h-lame:support-default-false-on-rails-8-1

Conversation

@h-lame

@h-lame h-lame commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description 📖

This pull request fixes a change in behaviour when running on rails 8.1 in how we decide if a nullable boolean column with a default value of false is optional or not when rendering the types from it. In rails 8.0 or lower we think such a column is not optional, in rails 8.1 with no other changes we think it is optional.

Background 📜

This was happening because in rails 8.1 default values in the column objects are now typecast to return the ruby object they'd represent, rather than a raw string from the db. So, a boolean false is now false not "false". Deciding if an attribute for a should be optional or not in the rendered types relies on checking if the column is nullable and if it has a default value set. The check used !column.default for the default values, and now that false is the boolean false instead of the string "false" we get different behaviour after upgrading to rails 8.1.

The Fix 🔨

By changing !column.default to column.default.nil? we preserve the existing behaviour and still let false default values on boolean columns through.

Screenshots 📷

N/A

Notes

I think what's in the commits is the minimum change needed, but locally I had to make many changes to Gemfile, Gemfile.lock, package.json, ppm-lock.yaml to get things to work. I also couldn't get the specs to fail so while I'm confident the fix is the right one, they only way I proved this to myself was:

  1. adding the boolean columns
  2. running bundle exec rake types_from_serializers:generate
  3. observing the new columns are rendered as required
  4. bumping rails in playground/vanilla to 8.1
  5. re-running bundle exec rake types_from_serializers:generate
  6. observing that the new columns were now rendered as optional
  7. applying the fix
  8. re-running bundle exec rake types_from_serializers:generate
  9. observing that the new columns were now rendered as required

At no point would rspec from the main folder fail. Trying to run rspec using the rails 8.1 gemfile I added in gemfiles didn't seem to do anything either. Let me know if we need more infrastructure changes to prove this out.

h-lame added 3 commits July 3, 2026 16:37
To give us some nullable boolean columns with defaults to test against
rails 8.1 behaviour.
Rails 8.1 changes how default values are represented, and testing
against it with the boolean values we added in the previous commit
should trigger an error.
On rails 8.1 default values are type cast rather than being returned as db strings which means a default value of false for a boolean column now comes through as the boolean `false` rather than the string `"false"`.  This means when we check if a nullable boolean column with a default value of `false` is optional we say it is optional on rails 8.1 because `false` fails the `!column.default` check whereas on rails 8.0 or lower we say it is not optional because `"false"` passes the `!column.default` check.

This fixes that by using `column.default.nil?` instead.
@h-lame h-lame changed the title Support default false on rails 8 1 Render nullable boolean columns with default value of false as not optional on rails 8.1 (like previous versions of rails did) Jul 3, 2026
@ElMassimo

Copy link
Copy Markdown
Owner

Looks good, thanks Murray!

@ElMassimo
ElMassimo merged commit 7c84709 into ElMassimo:main Jul 3, 2026
5 checks passed
@h-lame
h-lame deleted the support-default-false-on-rails-8-1 branch July 6, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants