Skip to content
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

Fix validation of integration name when team is not present in request data #4132

Merged
merged 6 commits into from
Mar 29, 2024

Conversation

Konstantinov-Innokentii
Copy link
Member

@Konstantinov-Innokentii Konstantinov-Innokentii commented Mar 28, 2024

This PR fixes validation of integration name when team is not present in request data. Also it slightly improves code structure of this validation.

@Konstantinov-Innokentii Konstantinov-Innokentii requested a review from a team March 28, 2024 08:02
@Konstantinov-Innokentii Konstantinov-Innokentii added pr:no public docs Added to a PR that does not require public documentation updates release:patch PR will be added to "Other Changes" section of release notes labels Mar 28, 2024
@Konstantinov-Innokentii Konstantinov-Innokentii changed the title Fix validation of integration name Fix validation of integration name when team is not present in request data Mar 28, 2024
if "team" in validated_data:
team = validated_data.get("team", None)
else:
team = self.instance.team
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this if block be simplified to
team = validated_data.get("team", self.instance.team)
? (same below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it seems so. For some reason I though that if team is in validated data, but it's none (if integration is un-attached from team for example) it will use second argument of get, but it works!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it could be simplified only to
team = validated_data.get("team", self.instance.team) if self.instance else validated_data.get("team")
becase validate is used both for create when self.instance is None and update when self.instance is object being updated.

Comment on lines 175 to 193
def get_team_for_name_validation(self, validated_data):
"""
get_team_for_name_validation retrieves the team to be used in the validation process.

If the serializer is used to update an existing object, it returns the team from the request data if it's present,
otherwise, it returns the team from the existing instance.
It's needed to validate name correctly even if team is not present in request data (It's not required).

If the serializer is used to create a new object, it returns the team from the request data.
"""
if self.instance:
if "team" in validated_data:
team = validated_data.get("team", None)
else:
team = self.instance.team
else:
team = validated_data.get("team")
return team

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make a BaseIntegrationSerializer in api.serializers that both the internal and public API serializers inherit from? (to deduplicate this function).

There's also a lot of other duplication in these serializers that we could deduplicate (at a future time 🙂)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I overcomplicated this logic, it will be just a one-liner :) I'm not sure about having unifying serializer between different APIs since it will violate single responsibility principle.

@Konstantinov-Innokentii Konstantinov-Innokentii added this pull request to the merge queue Mar 29, 2024
Merged via the queue into dev with commit 407b85e Mar 29, 2024
21 checks passed
@Konstantinov-Innokentii Konstantinov-Innokentii deleted the fix-integration-name-validation branch March 29, 2024 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:no public docs Added to a PR that does not require public documentation updates release:patch PR will be added to "Other Changes" section of release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants