Repository navigation
fix: enhance validation logic to include workspace_id in model checks #7855
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review & Analysis
Here is a concise analysis of the provided code diff, highlighting irregularities, potential issues, and optimization suggestions.
1. Irregularity / Bug: String
"None"used as default"None"as a default value forworkspace_idis dangerous and likely incorrect. Ifworkspace_idis meant to beNULLor an empty string in the database, storing or filtering by the literal string"None"will cause logic errors.None(Python object) or""(empty string), depending on DB schema. Also,getkey should matchself.fields. Useself.data.get("workspace_id") or None.2. API Contract Violation: Overriding
is_valid()signatureSerializer.is_valid()does not accept aworkspace_idargument. Adding it here breaks polymorphism and can cause unexpected behavior if the serializer is used generically (e.g., by DRF views).workspace_idfromis_valid(). Instead, ensureworkspace_idis always available inself.initial_dataor passed explicitly during validation via context (self.context.get('workspace_id')). Alternatively, perform the duplicate check directly invalidate()orcreate().3. Security & Logic Concern: Trusting User Input for Duplicate Check
workspace_idfalls back to user-provided data, a user might bypass constraints by providing a specific ID.workspace_idcomes from request context (e.g., authenticated user’s workspace), it should not be optional in the serializer field definition unless you intend to allow cross-workspace operations (which is rare for multi-tenancy).workspace_idis derived from authentication context, not just client input. If it must come from input, validate its integrity before DB queries.4. Performance Optimization: Redundant
.get()callsself.data.get("name"),self.data.get("provider"), etc.5. Error Handling: Exception swallowing risk
In
insert():model_not_fount→ likelymodel_not_found.AppApiExceptions are re-raised or handled appropriately. Otherwise, invalid input (e.g., missing provider) will silently proceed with partial validation failure if not caught.6. QuerySet Optimization: Avoid
.exists()inside loops or heavy conditions✅ Recommended Refactored Approach
Remove
workspace_idfromis_valid()and rely on context:In the view: