Skip to content

Commit

Permalink
Fix Contact/Team REST API (#5489)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennmatthews committed Mar 29, 2024
1 parent fe4b7b6 commit a2269a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/5489.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed REST API for Contact and Team incorrectly marking the `phone` and `email` fields as mandatory.
21 changes: 20 additions & 1 deletion nautobot/extras/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,18 @@ class ContactSerializer(NautobotModelSerializer):
class Meta:
model = Contact
fields = "__all__"
# https://www.django-rest-framework.org/api-guide/validators/#optional-fields
validators = []
extra_kwargs = {
"email": {"default": ""},
"phone": {"default": ""},
}

def validate(self, data):
attrs = data.copy()
attrs.pop("teams", None)
validator = UniqueTogetherValidator(queryset=Contact.objects.all(), fields=("name", "phone", "email"))
validator(attrs, self)
super().validate(attrs)
return data

Expand Down Expand Up @@ -946,7 +954,18 @@ class TeamSerializer(NautobotModelSerializer):
class Meta:
model = Team
fields = "__all__"
extra_kwargs = {"contacts": {"required": False}}
extra_kwargs = {
"contacts": {"required": False},
"email": {"default": ""},
"phone": {"default": ""},
}
# https://www.django-rest-framework.org/api-guide/validators/#optional-fields
validators = []

def validate(self, data):
validator = UniqueTogetherValidator(queryset=Team.objects.all(), fields=("name", "phone", "email"))
validator(data, self)
return super().validate(data)


#
Expand Down
4 changes: 0 additions & 4 deletions nautobot/extras/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,9 @@ def setUpTestData(cls):
{
"name": "Contact 3",
"phone": "555-0123",
"email": "",
},
{
"name": "Contact 4",
"phone": "",
"email": "contact4@example.com",
},
]
Expand Down Expand Up @@ -3613,11 +3611,9 @@ def setUpTestData(cls):
{
"name": "Team 3",
"phone": "555-0123",
"email": "",
},
{
"name": "Team 4",
"phone": "",
"email": "team4@example.com",
"address": "Rainbow Bridge, Central NJ",
},
Expand Down

0 comments on commit a2269a9

Please sign in to comment.