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

fixed server for duplicate attribute names #7890

Merged
merged 7 commits into from
May 16, 2024

Conversation

KrishavRajSingh
Copy link
Contributor

@KrishavRajSingh KrishavRajSingh commented May 14, 2024

Fixes #3113

Checked the array of attributes for duplicate attribute names and thrown Validation error when found
testing video

Checklist

  • [* ] I submit my changes into the develop branch
  • [* ] I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • [* ] I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • [ *] I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • Bug Fixes
    • Added validation to prevent duplicate attribute names when updating or creating labels in the REST API. Users will now receive a ValidationError if duplicate attribute names are detected.

Copy link
Contributor

coderabbitai bot commented May 14, 2024

Walkthrough

The recent changes enhance the CVAT application by introducing a validation mechanism to prevent the creation of duplicate attribute names within the same label. This improvement ensures data integrity by checking for duplicate attribute names when updating or creating labels, leading to better user feedback and error handling.

Changes

Files/Modules Change Summary
cvat/apps/engine/views.py Added validation logic in the perform_update method to check for duplicate attribute names.
cvat/apps/engine/serializers.py Added duplicate attribute name checks in update_label and create_labels functions.
changelog.d/20240514_201903_krishavrajsingh_... Documented the fix to check for duplicate attribute names and throw a ValidationError.

Assessment against linked issues

Objective (Issue #3113) Addressed Explanation
Prevent creation of tasks with duplicate attribute names within the same label.
Provide a notification/error for duplicate attribute names instead of a server error.

In the fields where labels grow,
Attributes now in a row,
No more duplicates to show,
Validation makes it so.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (3)
cvat/apps/engine/views.py (3)

Line range hint 44-44: Remove unused import statement.

- import cvat.apps.dataset_manager.views  # pylint: disable=unused-import

Line range hint 1967-1967: Remove unnecessary f-string formatting.

- return Response("{} is an invalid directory".format(directory_param),
+ return Response(f"{directory_param} is an invalid directory",

Line range hint 2721-2721: Remove unnecessary f-string formatting.

- return Response("{}".format(directory_param),
+ return Response(directory_param,
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f4ee3f9 and 3edc25e.
Files selected for processing (1)
  • cvat/apps/engine/views.py (1 hunks)
Additional Context Used
Ruff (3)
cvat/apps/engine/views.py (3)

44-44: cvat.apps.dataset_manager.views imported but unused


1967-1967: f-string without any placeholders


2721-2721: f-string without any placeholders

Additional comments not posted (1)
cvat/apps/engine/views.py (1)

2285-2292: LGTM! The validation logic for duplicate attribute names is correctly implemented.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 3edc25e and c901746.
Files selected for processing (1)
  • changelog.d/20240514_201903_krishavrajsingh_duplicateAttribute.md (1 hunks)
Additional Context Used
LanguageTool (1)
changelog.d/20240514_201903_krishavrajsingh_duplicateAttribute.md (1)

Near line 3: You might be missing the article “the” here.
Context: ### Fixed - server checks attributes with duplicate names ...

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between c901746 and 517f42a.
Files selected for processing (1)
  • changelog.d/20240514_201903_krishavrajsingh_duplicateAttribute.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • changelog.d/20240514_201903_krishavrajsingh_duplicateAttribute.md

@@ -2282,7 +2282,14 @@ def perform_update(self, serializer):
"Sublabels cannot be modified this way. "
"Please send a PATCH request with updated parent label data instead.",
code=status.HTTP_400_BAD_REQUEST)

attributes = self.request.data.get('attributes')
Copy link
Member

Choose a reason for hiding this comment

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

We need not only run this validation when updating a Label, but also when creating it.

Please, move this logic to the LabelSerializer (there are methods update_label and create_labels).
These methods are called from ProjectWriteSerializer and TaskWriteSerializer when labels updated.

@@ -0,0 +1,4 @@
### Fixed

the server checks attributes with duplicate names and throws ValidationError if found
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
the server checks attributes with duplicate names and throws ValidationError if found
- REST API allowed to create several attributes with the same name within one label

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Out of diff range and nitpick comments (1)
cvat/apps/engine/serializers.py (1)

Line range hint 718-718: Remove unnecessary f-string.

- f'label:update Skeleton id:{db_skeleton.id} for label_id:{db_label.id}'
+ 'label:update Skeleton id:{} for label_id:{}'.format(db_skeleton.id, db_label.id)
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 517f42a and 81cc42f.
Files selected for processing (2)
  • changelog.d/20240514_201903_krishavrajsingh_duplicateAttribute.md (1 hunks)
  • cvat/apps/engine/serializers.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • changelog.d/20240514_201903_krishavrajsingh_duplicateAttribute.md
Additional Context Used
Ruff (2)
cvat/apps/engine/serializers.py (2)

718-718: f-string without any placeholders


1153-1153: f-string without any placeholders

Comment on lines 338 to 344
encountered_names = set()
for attribute in attributes:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise exceptions.ValidationError('attribute with same name exists')
else:
encountered_names.add(attr_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Validation logic for duplicate attribute names looks good. However, the error message can be more descriptive.

- raise exceptions.ValidationError('attribute with same name exists')
+ raise exceptions.ValidationError(f"Duplicate attribute name '{attr_name}' found.")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
encountered_names = set()
for attribute in attributes:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise exceptions.ValidationError('attribute with same name exists')
else:
encountered_names.add(attr_name)
encountered_names = set()
for attribute in attributes:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise exceptions.ValidationError(f"Duplicate attribute name '{attr_name}' found.")
else:
encountered_names.add(attr_name)

Comment on lines 461 to 467
encountered_names = set()
for attribute in attributes:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise exceptions.ValidationError('attribute with same name exists')
else:
encountered_names.add(attr_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Validation logic for duplicate attribute names looks good. However, the error message can be more descriptive.

- raise exceptions.ValidationError('attribute with same name exists')
+ raise exceptions.ValidationError(f"Duplicate attribute name '{attr_name}' found.")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
encountered_names = set()
for attribute in attributes:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise exceptions.ValidationError('attribute with same name exists')
else:
encountered_names.add(attr_name)
encountered_names = set()
for attribute in attributes:
attr_name = attribute.get('name')
if attr_name in encountered_names:
raise exceptions.ValidationError(f"Duplicate attribute name '{attr_name}' found.")
else:
encountered_names.add(attr_name)

cvat/apps/engine/serializers.py Outdated Show resolved Hide resolved
cvat/apps/engine/serializers.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 81cc42f and b52798f.
Files selected for processing (1)
  • cvat/apps/engine/serializers.py (3 hunks)
Additional Context Used
Ruff (2)
cvat/apps/engine/serializers.py (2)

717-717: f-string without any placeholders


1152-1152: f-string without any placeholders

Additional comments not posted (3)
cvat/apps/engine/serializers.py (3)

324-333: The check_attribute_names_unique method is a good addition to ensure attribute names are unique. This will help prevent errors related to duplicate attribute names.


349-349: The call to check_attribute_names_unique in the update_label method ensures that attribute names are validated for uniqueness before updating a label. This is a necessary validation step.


466-466: The call to check_attribute_names_unique in the create_labels method ensures that attribute names are validated for uniqueness before creating labels. This is a necessary validation step.

@bsekachev bsekachev merged commit 852b396 into cvat-ai:develop May 16, 2024
32 checks passed
@bsekachev
Copy link
Member

Great, thanks for the contribution!

@cvat-bot cvat-bot bot mentioned this pull request May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating a task with the same attributes name within the same label.
2 participants