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: PUT API response does not send ValidationError when expected #235

Merged
merged 7 commits into from
Mar 18, 2025

Conversation

Abbas-Askari
Copy link
Contributor

What

Fix the issue described in #117. The API previously just sent {"detail":"next version must be equal to 2, was 1"} instead of a ValidationError. Now the endpoint first fetches the current version from db, and send a validation error back if the version is not properly incremented.

Screenshot

{
  "detail": [
    {
      "type": "value_error",
      "loc": [
        "body",
        "version"
      ],
      "msg": "Value error, version must be exactly 2",
      "input": 4
    }
  ]
}

Fixes bug(s)

@Abbas-Askari Abbas-Askari requested a review from a team as a code owner March 1, 2025 09:08
Copy link
Contributor

@aadarsh-ram aadarsh-ram left a comment

Choose a reason for hiding this comment

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

Hey @Abbas-Askari, I added some comments. @CharlesNepote @alexgarel please feel free to add anything else.

Comment on lines 480 to 491
if product_tag.version != latest_version + 1:
raise HTTPException(
status_code=422,
detail=[
{
"type": "value_error",
"loc": ["body", "version"],
"msg": f"Value error, version must be exactly {latest_version + 1}",
"input": product_tag.version,
}
],
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you'll need to use Pydantic models for Validation errors. They should give you a json automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aadarsh-ram Ofc, validation errors should be dealt with in Pydantic models, But I didn't want to doa DB fetch in the model, I think DB fetches should be performed at the endpoint function.

Current validation of version in the pydantic model:

@field_validator('version')
    def version_check(cls, version):
        if version < 1:
            raise ValueError('version must be greater or equal to 1')
        return version

I can, however, move it to there if you say so.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I meant it'll be better if the JSON sent was constructed from an Pydantic error, rather than you constructing it. The current validation model doesn't need to be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, Okay. I'll change.

Copy link
Member

Choose a reason for hiding this comment

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

@Abbas-Askari I would not do a specific model for that, but you could isolate the dict creation in a function to improve readability.

Comment on lines +474 to +475
if not latest_version_row:
raise HTTPException(status_code=404, detail="Key was not found")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is being checked already. If you're adding this, please remove the other instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

@aadarsh-ram great to see you around 🙂 !

@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.53%. Comparing base (b9cda65) to head (3ce3536).
Report is 75 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #235      +/-   ##
==========================================
- Coverage   95.06%   94.53%   -0.54%     
==========================================
  Files           5        5              
  Lines         324      384      +60     
==========================================
+ Hits          308      363      +55     
- Misses         16       21       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

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

Great @Abbas-Askari this looks good to me :-)

Only thing maybe, make create_version_error a private function, that is just rename it _create_version_error.

I approve the PR but you can do the change before merging (provided tests pass).

@Abbas-Askari
Copy link
Contributor Author

Abbas-Askari commented Mar 12, 2025

Only thing maybe, make create_version_error a private function, that is just rename it _create_version_error.

Ok, I'll change

@Abbas-Askari
Copy link
Contributor Author

@alexgarel Done!

@alexgarel
Copy link
Member

@Abbas-Askari can you fix the tests ?

@alexgarel
Copy link
Member

Great @Abbas-Askari !

@alexgarel alexgarel merged commit 7f3a589 into openfoodfacts:main Mar 18, 2025
9 checks passed
@Abbas-Askari Abbas-Askari deleted the add-validation-error branch March 18, 2025 14:15
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.

4 participants