-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Embedded nil pointer structs should be retrieved as nil #5431
Comments
When we reuse We can only reset before I recommend adding test files and revert them. |
Not sure what you mean here. As a workaround I wrote a utility that walks the object and nils out any zero values recursively depth first, but of course it would be faster for Gorm to recycle the zero values it allocated in scan.go instead of Field.Set them. It is probably necessary to add a Config defaulting to off to allow users of Gorm turn on this behavior, since consumers of Gorm may have code that already assumes they get a zero value and not nil. |
I had to implement a similar utility to work around this. Note that I am using gorm 1.23.4 and I'm seeing different results than those from @nickpalmer . If the Author remains as defined above in his test, then the Author is returned as nil. But if the Author struct is changed to use a pointer within it like so:
Then, the result is &{ } When upgrading to 1.23.8, I see &{ } either way. |
…ining structure There are locations that our code assumes that embedded structure is always not nil. This change adds tests to verify it. For more information: go-gorm/gorm#5431
…ining structure (#4730) There are locations that our code assumes that embedded structure is always not nil. This change adds tests to verify it. For more information: go-gorm/gorm#5431
@jinzhu Is this issue being worked upon? Has this been acknowledged? This is causing regression failure for us. One of our embedded struct which used to be read as nil, is now coming as zero value of the struct. This was working for us in v1.23.4 and breaking in 1.24.6. |
…ining structure (openshift#4730) There are locations that our code assumes that embedded structure is always not nil. This change adds tests to verify it. For more information: go-gorm/gorm#5431
Describe the feature
Gorm should round trip an embedded pointer to a struct as nil when the struct is given as nil.
Motivation
My domain model has optional embedded objects, which then may have required fields on them.
Our validator will validate the embedded object's required fields if the pointer value is non-nil but will ignore the required validations on the embedded struct if it is nil.
Gorm should detect that that the embedded struct has the zero value and not set it to the zero value if only zero values have been loaded into that value.
Without this a save of nil is not preserved on reload for the embedded struct.
Related Issues
Example Failing Test
This change to the existing test outputs:
This test would pass if this feature were implemented.
The text was updated successfully, but these errors were encountered: