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

Raise ValidationError when other type than json is provided during Ad… #1850

Merged
merged 1 commit into from Sep 25, 2020

Conversation

ipanova
Copy link
Member

@ipanova ipanova commented Sep 18, 2020

…visory upload.

closes #7468
https://pulp.plan.io/issues/7468

@pulpbot
Copy link
Member

pulpbot commented Sep 18, 2020

Attached issue: https://pulp.plan.io/issues/7468

try:
update_record_data.update(json.loads(data["file"].read()))
update_record_data.update(data)
except ValueError as e:
Copy link
Member

@fao89 fao89 Sep 18, 2020

Choose a reason for hiding this comment

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

A RPM file rose UnicodeDecodeError at data["file"].read()
testing a format different than JSON I got:

In [1]: import json                                                                                                                                                                                                                           

In [2]: json.loads("a")                                                                                                                                                                                                                       
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-2-c0c0ca6f67c6> in <module>
----> 1 json.loads("a")

~/.pyenv/versions/3.8.0/lib/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    355             parse_int is None and parse_float is None and
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:
    359         cls = JSONDecoder

~/.pyenv/versions/3.8.0/lib/python3.8/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~/.pyenv/versions/3.8.0/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Copy link
Member Author

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/json.html#exceptions

In [12]: from json import JSONDecodeError

In [13]: try:
    ...:     json.loads('/home/vagrant/devel/pulp_rpm/docs/_scripts/bear-4.1-1.noarch.rpm')
    ...: except JSONDecodeError:
    ...:     print("lala")
    ...: 
    ...: 
    ...: 
    ...: 
lala

In [14]: try:
    ...:     json.loads('/home/vagrant/devel/pulp_rpm/docs/_scripts/bear-4.1-1.noarch.rpm')
    ...: except ValueError:
    ...:     print("ina")
    ...: 
    ...: 
ina

Copy link
Member

Choose a reason for hiding this comment

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

oh! it is a subclass!
today I learned!
thank you @ipanova

Copy link

Choose a reason for hiding this comment

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

While JSONDecodeError is a subclass of ValueError, I think it would be "cleaner" to catch the specific exception. Otherwise a ValueError coming from some different location could be hidden. Granted I don't see such a location in the current code, I think it's a good idea to be as specific as possible.

Copy link
Member

@fao89 fao89 left a comment

Choose a reason for hiding this comment

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

LGTM! Please address the CI issue, thank you Ina!

@goosemania
Copy link
Member

Could you add a test please?

update_record_data.update(data)
try:
update_record_data.update(json.loads(data["file"].read()))
update_record_data.update(data)
Copy link
Member

Choose a reason for hiding this comment

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

Does this line needs to be in the try/except clause?

Copy link

Choose a reason for hiding this comment

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

I don't think so.

Copy link
Member Author

Choose a reason for hiding this comment

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

normally i would move this to the else clause however in this case this line does not pollute much, imo.

update_record_data.update(json.loads(data["file"].read()))
except json.JSONDecodeError:
raise serializers.ValidationError(
"Json file is expected"
Copy link
Member

Choose a reason for hiding this comment

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

s/Json/JSON/

@@ -0,0 +1 @@
Raise ValidationError when other type than json is provided during Advisory upload.
Copy link
Member

Choose a reason for hiding this comment

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

s/json/JSON/

update_record_data.update(json.loads(data["file"].read()))
update_record_data.update(data)
try:
update_record_data.update(json.loads(data["file"].read()))
Copy link
Member

Choose a reason for hiding this comment

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

😆 🤣 That's a solution to address PR comments ;)

@pep8speaks
Copy link

pep8speaks commented Sep 24, 2020

Hello @ipanova! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-09-25 15:42:51 UTC

@ipanova ipanova merged commit 6b78e36 into pulp:master Sep 25, 2020
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.

None yet

6 participants