Skip to content

Conversation

@doraemonkeys
Copy link

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Fix: #7274

Added a nil check before calling s.Serializer.Scan(). When both s.fieldValue and s.Serializer are nil, the field is now set to its zero value and returns early, preventing the panic.

User Case Description

Problem

When using any type fields, if both s.fieldValue and s.Serializer are nil, the code would directly call s.Serializer.Scan() at line 974, causing a nil pointer panic.

@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Oct 29, 2025

Gracefully handle any serializer fields when both fieldValue and Serializer are nil

This PR prevents a nil-pointer panic that occurred when persisting models containing any typed fields decorated with gorm:"serializer:json". The panic was triggered in schema/field.go when s.Serializer was nil yet its Scan method was invoked. A short-circuit branch now returns early after resetting the destination field to its zero value. A dedicated regression test demonstrates the fix across create, update, and read paths.

Key Changes

• Added early-return guard in schema/field.go inside the generated field.Set closure: if s.fieldValue == nil && s.Serializer == nil, set the model field to reflect.Zero and exit.
• Introduced TestSerializerWithAnyType in tests/serializer_test.go covering create, update, query for records where Data any is nil and non-nil.

Affected Areas

schema/field.go
tests/serializer_test.go

This summary was automatically generated by @propel-code-bot

@jinzhu
Copy link
Member

jinzhu commented Oct 30, 2025

Can you add some tests?

@jinzhu jinzhu added the type:not tested not tested label Oct 30, 2025
@propel-code-bot propel-code-bot bot changed the title Fix nil pointer panic when using any type field with nil Serializer Fix nil pointer panic in serializer for nil any-type fields Oct 31, 2025
@doraemonkeys
Copy link
Author

Can you add some tests?

Hi, thanks for the review! I've added comprehensive test cases for this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic saving a struct with nil value field and json serializer

2 participants