Skip to content

Commit

Permalink
fix: allow JSONField to be blank (#580)
Browse files Browse the repository at this point in the history
* Fix new JSONField validation.

**Description:**

Our setup (Django==3.2.25) has an automatic call to full_clean() on all Models in a pre-save signal.

After upgrading from 4.0.0 to 5.4.1 all our new Django Admins lost the ability to login.

We tracked it down to these calls here https://github.com/python-social-auth/social-app-django/blob/5.4.1/social_django/storage.py#L147, creation fails because `extra_data` is not passed to `create`, but the field is required not null and not blank and not empty dictionary.

Making it blank-able and null-able will solve the issue.

**TestPlan:**

Before the fix (any version between 5.3.0 and 5.4.1):

```
UserSocialAuth.objects.create(user=User.objects.first(), uid=1, provider="test")
[...]
ValidationError: {'extra_data': ['This field cannot be blank.']}
```

After the fix: the UserSocialAuth instance should be created correctly.

Co-authored-by: Arjuna Del Toso <[email protected]>
Co-authored-by: Michal Čihař <[email protected]>
  • Loading branch information
3 people authored Jun 11, 2024
1 parent e59bda7 commit 1c6f853
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions social_django/migrations/0016_alter_usersocialauth_extra_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2024-05-28 19:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("social_django", "0015_rename_extra_data_new_usersocialauth_extra_data"),
]

operations = [
migrations.AlterField(
model_name="usersocialauth",
name="extra_data",
field=models.JSONField(blank=True, default=dict),
),
]
2 changes: 1 addition & 1 deletion social_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AbstractUserSocialAuth(models.Model, DjangoUserMixin):
)
provider = models.CharField(max_length=32)
uid = models.CharField(max_length=UID_LENGTH, db_index=True)
extra_data = models.JSONField(default=dict)
extra_data = models.JSONField(default=dict, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
objects = UserSocialAuthManager()
Expand Down

0 comments on commit 1c6f853

Please sign in to comment.