Repository navigation
M6 FIX: Require a username and password on the spring-boot-starter-security User - #16291
borinquenkid merged 3 commits into
Conversation
Grails 8 makes an unconstrained persistent property nullable by default (upgrading80x.adoc, "GORM Properties Are Nullable by Default"), so the template's `blank: false` constraints stopped making the credentials required: a User with a null username and a null password validated clean, and the scaffolded admin would happily save one. Every other domain template in the forge -- role, userClassic, userRole -- already declares `nullable: false`; this one was missed when the default flipped. The generated UserSpec failed out of the box for the same reason. Its `!new User(username: '', password: '').validate()` never exercised the blank constraint at all: the map constructor binds through data binding with convertEmptyStringsToNull, so both values arrived as null and then passed. Assign the blank values directly, which is the only way a blank string reaches the property, and cover the nullable case as its own feature so the two constraints are asserted separately.
The template emitted `table 'users'` for every data implementation, but `table` is a Hibernate directive. On MongoDB it was silently ignored, so the intent behind it -- don't name the persistence unit `user`, which is a reserved word in PostgreSQL, H2 and SQL Server -- was lost exactly where nothing warned about it. Booting a generated mongodb app showed the documents landing in a `user` collection; with `collection 'users'` they land in `users`. Pick the directive from the selected features, the way GrailsBase and DatabaseMigrationPlugin already do, rather than from the gorm option, so it holds whether the implementation was chosen with --data or by naming the feature. Hibernate 5 and 7 keep `table 'users'`, MongoDB gets `collection 'users'`, and a store that uses neither -- Neo4j -- gets no mapping block at all rather than a directive it does not understand.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 8.0.x #16291 +/- ##
==================================================
+ Coverage 54.7060% 54.7484% +0.0424%
- Complexity 20427 20475 +48
==================================================
Files 2101 2103 +2
Lines 100978 101077 +99
Branches 17907 17928 +21
==================================================
+ Hits 55241 55338 +97
+ Misses 37872 37861 -11
- Partials 7865 7878 +13 🚀 New features to boost your workflow:
|
…derstands The classic User/Role/UserRole domain model shared by grails-spring-security and grails-spring-security-ui hardcoded a Hibernate-only `table` mapping directive, same defect this PR already fixes for the spring-boot-starter-security User template. Neither feature is restricted to Hibernate, so selecting gorm-mongodb alongside either one silently dropped the directive. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K4Wxz1HXEyfVWGm52jzvpR
🚨 TestLens detected 9 failed tests 🚨Here is what you can do:
Test SummaryCI / Build Grails Forge (Java 21, indy=false) > :grails-forge-cli:test [grails-forge]
CI / Build Grails Forge (Java 21, indy=false) > :test-core:test [grails-forge]
Groovy Snapshot Canary Build / Build Grails (shard 2) > :grails-test-examples-scaffolding:integrationTest
🏷️ Commit: efb3310 Test FailuresUserControllerSpec > User list (:grails-test-examples-scaffolding:integrationTest in Groovy Snapshot Canary Build / Build Grails (shard 2))CreateAppSpec > test basic create-app build task (:test-core:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))CreateControllerCommandSpec > test app with controller (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))CreateDomainClassCommandSpec > test app with domain (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))CreateInterceptorCommandSpec > test app with interceptor (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))CreateRestApiSpec > test rest-api app with default json-views (:test-core:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))CreateServiceCommandSpec > test app with service (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))CreateTagLibCommandSpec > test app with taglib (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))ScaffoldingSpec > test generate-controller command (:test-core:test [grails-forge] in CI / Build Grails Forge (Java 21, indy=false))Rerun ControlsNote Checks are currently running using the configuration below. Select tests to mute in this pull request: 🔲 CreateAppSpec > test basic create-app build task Reuse successful test results: 🔲 ♻️ Only rerun the tests that failed or were muted before Click the checkbox to trigger a rerun: 🔲 Rerun jobs Learn more about TestLens at testlens.app/docs. |
borinquenkid
left a comment
There was a problem hiding this comment.
Reviewed the nullable/blank constraint fix and the Hibernate/MongoDB mapping-directive fix for spring-boot-starter-security's User template.
Also pushed a follow-up commit extending the same mapping-directive fix to the classic User/Role/UserRole model shared by grails-spring-security and grails-spring-security-ui (userClassic.rocker.raw), which had the identical Hibernate-only table directive and no validator restricting it from MongoDB. Added matching test coverage in GrailsSpringSecuritySpec for Hibernate5/7, MongoDB, and Neo4j. All targeted tests pass and codeStyle is clean.
Two defects in the
spring-boot-starter-securityUsertemplate, both of which only show up once you generate an app and run it.1. The credentials were not actually required
Grails 8 makes an unconstrained persistent property nullable by default (documented in
upgrading80x.adoc, "GORM Properties Are Nullable by Default"). The template was never updated for that flip, so itsblank: falseconstraints no longer make the credentials required — aUserwith a null username and a null password validates clean, and the scaffolded user admin will save one.Every other domain template in the forge already declares
nullable: false, which is what makes this look like a miss rather than a choice:role.rocker.rawauthority unique: true, nullable: falseuserClassic.rocker.rawusername unique: true, nullable: false/password nullable: falseuserRole.rocker.rawuser nullable: false/role nullable: false, unique: 'user'user.rocker.rawusername blank: false, unique: true← missingThe generated spec failed out of the box
Creating an app with this feature produced a red build on the very first
./gradlew test:The assertion exercised neither constraint it appeared to test. The domain map constructor binds through data binding with
convertEmptyStringsToNull, sousername: ''arrives asnull— theblankconstraint never sees an empty string — and nullable-by-default then let the nulls through.The spec now covers the two constraints as separate features, assigning the blank values directly since that is the only way a blank string actually reaches the property:
2. The mapping directive did not match the data store
The template emitted
table 'users'for every data implementation, buttableis a Hibernate directive. On MongoDB it is silently ignored, so the reason it is there — don't name the persistence unituser, a reserved word in PostgreSQL, H2 and SQL Server — was lost exactly where nothing warns about it. Reading the embedded MongoDB of a generated app, before and after:usersusertable 'users'["user.next_id", "user"]collection 'users'["users.next_id", "users"]The directive is now chosen from the selected features, the way
GrailsBaseandDatabaseMigrationPluginalready do, rather than from the gorm option — so it holds whether the implementation was picked with--dataor by naming the feature:Hibernate 5 and 7 keep
table 'users', so the default app is unchanged. Neo4j gets no mapping block rather than a directive it does not understand.Verification
Generated a real app (
create-app --data mongodb --features spring-boot-starter-security --jdk 25) and ran it, rather than only asserting on template text::grails-forge-core:test— 347 tests, 0 failuresgrails-forgecodeStyle— cleanUserSpec— 4/4 passnullable: falseand re-running makes the nullable feature fail, so the new spec is not vacuous. The blank feature passes either way —blank: falsewas always correct; the nullable gap was the whole defect.Phases.CONVERSIONto confirm it is valid Groovy; that check caught a swallowed blank line beforestatic constraintsthat thecontainsassertions did not.The app was also exercised end to end on embedded MongoDB:
/anonymous 200,/useranonymous redirects to login, form login succeeds,/userasROLE_ADMIN200,/userasROLE_USER403.