-
-
Notifications
You must be signed in to change notification settings - Fork 974
Make domain properties nullable by default (Grails 8) #15721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codeconsole
wants to merge
5
commits into
apache:8.0.x
Choose a base branch
from
codeconsole:nullable-by-default
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c27b930
Make domain properties nullable by default (Grails 8)
codeconsole 5216696
Merge branch '8.0.x' into nullable-by-default
jamesfredley d92c416
Document nullable-by-default: upgrade guide, constraints guide, IDE m…
codeconsole 180306b
Merge branch '8.0.x' into nullable-by-default
codeconsole b112a97
Address review feedback on the nullable-by-default config wiring
codeconsole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,5 +66,6 @@ class ObjectB { | |
| String name | ||
|
|
||
| static constraints = { | ||
| name nullable: false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,5 +66,6 @@ class ObjectB { | |
| String name | ||
|
|
||
| static constraints = { | ||
| name nullable: false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...atamapping-validation/src/test/groovy/grails/gorm/validation/NullableByDefaultSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.gorm.validation | ||
|
|
||
| import org.grails.datastore.gorm.validation.constraints.registry.DefaultValidatorRegistry | ||
| import org.grails.datastore.mapping.core.connections.ConnectionSourceSettings | ||
| import org.grails.datastore.mapping.keyvalue.mapping.config.GormKeyValueMappingFactory | ||
| import org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext | ||
| import org.grails.datastore.mapping.model.MappingContext | ||
| import org.grails.datastore.mapping.model.PersistentEntity | ||
| import org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy | ||
| import org.grails.datastore.mapping.validation.ValidationErrors | ||
| import org.grails.datastore.mapping.validation.ValidatorRegistry | ||
| import org.springframework.validation.Errors | ||
| import org.springframework.validation.Validator | ||
| import spock.lang.Shared | ||
| import spock.lang.Specification | ||
|
|
||
| import jakarta.persistence.Entity | ||
|
|
||
| /** | ||
| * Demonstrates the Grails 8 default: an unconstrained persistent property is nullable by default, | ||
| * aligning Grails with the rest of the JVM persistence/validation ecosystem. This spec deliberately | ||
| * does NOT opt out (no {@code grails.gorm.default.constraints} override), so it exercises the real | ||
| * framework default applied by {@code DefaultConstraintEvaluator}. | ||
| */ | ||
| class NullableByDefaultSpec extends Specification { | ||
|
|
||
| @Shared Validator widgetValidator | ||
|
|
||
| void setupSpec() { | ||
| MappingContext mappingContext = new KeyValueMappingContext("test") | ||
| mappingContext.mappingFactory = new GormKeyValueMappingFactory("test") | ||
| mappingContext.syntaxStrategy = new GormMappingConfigurationStrategy(mappingContext.mappingFactory) | ||
|
|
||
| PersistentEntity widgetEntity = mappingContext.addPersistentEntity(Widget) | ||
|
|
||
| ValidatorRegistry registry = new DefaultValidatorRegistry(mappingContext, new ConnectionSourceSettings()) | ||
| widgetValidator = registry.getValidator(widgetEntity) | ||
| } | ||
|
|
||
| void "an unconstrained property is nullable by default"() { | ||
| given: "a widget whose only populated property is the explicitly-required one" | ||
| Widget widget = new Widget(requiredName: 'gizmo') | ||
| Errors errors = new ValidationErrors(widget) | ||
|
|
||
| when: | ||
| widgetValidator.validate(widget, errors) | ||
|
|
||
| then: "the unconstrained properties are valid while null - they are nullable by default" | ||
| !errors.hasErrors() | ||
| !errors.getFieldError('description') | ||
| !errors.getFieldError('quantity') | ||
| } | ||
|
|
||
| void "a property with an explicit nullable: false constraint is still required"() { | ||
| given: "a widget missing the explicitly-required property" | ||
| Widget widget = new Widget() | ||
| Errors errors = new ValidationErrors(widget) | ||
|
|
||
| when: | ||
| widgetValidator.validate(widget, errors) | ||
|
|
||
| then: "only the explicitly-required property is rejected; the unconstrained ones are not" | ||
| errors.hasErrors() | ||
| errors.getFieldError('requiredName')?.code == 'nullable' | ||
| !errors.getFieldError('description') | ||
| !errors.getFieldError('quantity') | ||
| } | ||
|
|
||
| void "grails.gorm.default.nullable = false restores required-by-default"() { | ||
| given: "a validator built with the YAML-friendly flag disabled" | ||
| MappingContext mappingContext = new KeyValueMappingContext("test") | ||
| mappingContext.mappingFactory = new GormKeyValueMappingFactory("test") | ||
| mappingContext.syntaxStrategy = new GormMappingConfigurationStrategy(mappingContext.mappingFactory) | ||
| PersistentEntity widgetEntity = mappingContext.addPersistentEntity(Widget) | ||
|
|
||
| ConnectionSourceSettings settings = new ConnectionSourceSettings() | ||
| settings.default.nullable = false | ||
| Validator validator = new DefaultValidatorRegistry(mappingContext, settings).getValidator(widgetEntity) | ||
|
|
||
| and: "a widget whose unconstrained properties are null" | ||
| Widget widget = new Widget(requiredName: 'gizmo') | ||
| Errors errors = new ValidationErrors(widget) | ||
|
|
||
| when: | ||
| validator.validate(widget, errors) | ||
|
|
||
| then: "the unconstrained properties are required again - the flag restored legacy behaviour" | ||
| errors.hasErrors() | ||
| errors.getFieldError('description')?.code == 'nullable' | ||
| errors.getFieldError('quantity')?.code == 'nullable' | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class Widget { | ||
| String description | ||
| Integer quantity | ||
| String requiredName | ||
|
|
||
| static constraints = { | ||
| requiredName nullable: false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You added this to grails-core instead of grails-datastore-core.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jamesfredley why are we not adding a specific configuration file for the datastore project? That's where the constraint is actually used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdaugherty there is no similar file in
grails-datastore-core. I put it where all the othergrails.gorm.*keys are. It wouldn't make sense to put it a location different from where the other keys are.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, and we shouldn't let this block this change. But I think we've made a larger mistake here - the config keys were supposed to exist in the projects where they're used/defined. It's my understanding that hasn't happened based on this.