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

5010 – Fix tag with trailing space error #1982

Merged
merged 4 commits into from
Aug 5, 2024

Conversation

vasconsaurus
Copy link
Contributor

@vasconsaurus vasconsaurus commented Aug 2, 2024

Description

When trying to create an imported fact-check that had a tag with a trailing space it would fail (text has already been taken).

We have a normalization step but that runs before_validation on TagText.
So when we are checking if the tag exists in line 92 from tag.rb, the tag has not been normalized. This means:

  • We look for 'tag', it is not there. We create it.
  • We look for ' tag', it is not there. We try to create it, it gets normalized, so it becomes 'tag'. Then we fail to create it,
    because it already exists.

Adding the strip when comparing tags fixes this, but I'm not sure if it is the best solution.

References: 5010

How has this been tested?

  • I added one big test that replicated the issue we saw: rails test test/controllers/graphql_controller_12_test.rb:613
  • And added one model test: rails test test/models/tag_test.rb:275

Things to pay attention to during code review

Please describe parts of the change that require extra attention during code review, for example:

  • File FFFF, line LL: This refactoring does this and this. Is it consistent with how it’s implemented elsewhere?
  • Etc.

Checklist

  • I have performed a self-review of my own code
  • I have added unit and feature tests, if the PR implements a new feature or otherwise would benefit from additional testing
  • I have added regression tests, if the PR fixes a bug
  • I have added logging, exception reporting, and custom tracing with any additional information required for debugging
  • I considered secure coding practices when writing this code. Any security concerns are noted above.
  • I have commented my code in hard-to-understand areas, if any
  • I have made needed changes to the README
  • My changes generate no new warnings
  • If I added a third party module, I included a rationale for doing so and followed our current guidelines

this seems too much, but at least it replicates the bug
We have a normalization steps but that runs before_validation on TagText.
Meaning when we are checking if the tag exists in line 92 from tag.rb, the
tag has not been normalized.

We look for 'tag', it is not there, we create it.
We look for ' tag', it is not there, but once we try to create it,
it is normalized, so it becomes 'tag', then we fail to create it,
because it already exists.

Adding the strip when compating tags fixes this, but I'm not sure if
it is the best solution.
@vasconsaurus
Copy link
Contributor Author

@caiosba Could you please take a look?

  • This fixes it, but I'm not sure if it's a good solution.
  • And although the first test helped me get this to work, I'm not sure we should keep it. WDYT?

@vasconsaurus vasconsaurus changed the title 5010 – Fix tag with trailing space error [WIP] 5010 – Fix tag with trailing space error Aug 2, 2024
@@ -89,7 +89,7 @@ def self.run_bulk_create_callbacks(ids_json, pmids_json)
def get_tag_text_reference
if self.tag.is_a?(String)
team_id = self.team&.id
tag_text = TagText.where(text: self.tag, team_id: team_id).last
tag_text = TagText.where(text: (self.tag).strip, team_id: team_id).last
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor, only FYI: The parenthesis are not needed here.

@caiosba
Copy link
Contributor

caiosba commented Aug 2, 2024

That's a good solution, Manu. And thanks for adding the tests. Were you able to reproduce the same exception in the test, before the fix?

@vasconsaurus vasconsaurus changed the title [WIP] 5010 – Fix tag with trailing space error 5010 – Fix tag with trailing space error Aug 2, 2024
@vasconsaurus vasconsaurus marked this pull request as ready for review August 2, 2024 16:27
@vasconsaurus vasconsaurus requested a review from caiosba August 5, 2024 11:32
@@ -89,7 +89,7 @@ def self.run_bulk_create_callbacks(ids_json, pmids_json)
def get_tag_text_reference
if self.tag.is_a?(String)
team_id = self.team&.id
tag_text = TagText.where(text: self.tag, team_id: team_id).last
tag_text = TagText.where(text: self.tag.strip, team_id: team_id).last
Copy link
Contributor

Choose a reason for hiding this comment

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

Also I suggest to trim database value so we can catch existing one with trailing space
i.e
tag_text = TagText.where(team_id: team_id).where('trim(text) = ?', self.tag.strip).last

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I see that we normalize tag before saving in DB and did self.text.strip.gsub(/^#/, '') so we can skip this comment

@vasconsaurus vasconsaurus merged commit 7fabdb7 into develop Aug 5, 2024
10 checks passed
@vasconsaurus vasconsaurus deleted the 5010-fix-tag-with-trailing-space branch August 5, 2024 13:17
DGaffney pushed a commit that referenced this pull request Aug 7, 2024
When trying to create an imported fact-check that had a tag with a trailing space it would fail (`text has already been taken`).

We have a normalization step but that runs `before_validation` on `TagText`.
So when we are checking if the tag exists in line 92 from `tag.rb`, the tag has not been normalized. This means:
- We look for 'tag', it is not there. We create it.
- We look for ' tag', it is not there. We try to create it, it gets normalized, so it becomes 'tag'. Then we fail to create it,
    because it already exists.

Adding the strip when comparing tags fixes this.

Tests
- rails test test/controllers/graphql_controller_12_test.rb:613
- rails test test/models/tag_test.rb:275

References: 5010
PR: 1982
DGaffney added a commit that referenced this pull request Aug 16, 2024
…1986)

* CV2-5011 refactors for making alegre dual purpose on text encoding

* first pass on adding tests for new functionality

* uncomment line

* fix holdover typo

* Query fact_check and explainer by id (#1980)

* Test syntax fix for docker compose commands. (#1984)

* Bump rexml from 3.2.8 to 3.3.3 (#1981)

Bumps [rexml](https://github.com/ruby/rexml) from 3.2.8 to 3.3.3.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](ruby/rexml@v3.2.8...v3.3.3)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Return the previous project media for claim description (#1985)

Use case: we may need to update the project media information in the Relay store when it's removed from a claim.

Reference: CV2-5000.

* Adding new GraphQL field to `ProjectMediaType`: `suggested_main_relationship`.

Reference: CV2-5004.

* CV2-5002: fix sentry issue (#1978)

* CV2-5002: fix sentry issue

* CV2-5002: fix tests

* Do not rate limit successful login attempts (#1968)

When a login attempt is successful, we should not increment the counter
for rate limiting.

* 5010 – Fix tag with trailing space error (#1982)

When trying to create an imported fact-check that had a tag with a trailing space it would fail (`text has already been taken`).

We have a normalization step but that runs `before_validation` on `TagText`.
So when we are checking if the tag exists in line 92 from `tag.rb`, the tag has not been normalized. This means:
- We look for 'tag', it is not there. We create it.
- We look for ' tag', it is not there. We try to create it, it gets normalized, so it becomes 'tag'. Then we fail to create it,
    because it already exists.

Adding the strip when comparing tags fixes this.

Tests
- rails test test/controllers/graphql_controller_12_test.rb:613
- rails test test/models/tag_test.rb:275

References: 5010
PR: 1982

* Ticket #4938: Sort workspaces alphabetically (#1988)

* Reverting migration until empty clusterization center issue is actually resolved (#1990)

Reference: CV2-4869

* Moving `accessible_teams` from `UserType` to `MeType`. (#1991)

This information is only needed for the current user, so, it should be under `MeType`, not `UserType`.

References: CV2-4938 and CV2-4704.

* Fixing Sidekiq error "ActiveRecord::RecordInvalid: Text has already been taken"

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Alexandre Amoedo Amorim <[email protected]>
Co-authored-by: Martin Peck <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Caio Almeida <[email protected]>
Co-authored-by: Mohamed El-Sawy <[email protected]>
Co-authored-by: Jay Joshua <[email protected]>
Co-authored-by: Manu Vasconcelos <[email protected]>
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.

3 participants