asyrjasalo / RESTinstance

Robot Framework library for RESTful JSON APIs
https://pypi.org/project/RESTinstance
GNU Lesser General Public License v3.0
206 stars 84 forks source link

Empty response after post leads to an error #47

Closed Caritrian closed 5 years ago

Caritrian commented 5 years ago

Hi @asyrjasalo
At work i am currently facing some problems with your library. I found out after a post request to our internal rest interface a 200 OK was responded with an empty body { }. This is of course also a defect on our side but leads to an error in robotframework:

KeyError: u'properties' Traceback (most recent call last): File "C:\Anaconda3\envs\py2_automation\lib\site-packages\REST\keywords.py", line 404, in post return self._request(endpoint, request, validate)['response'] File "C:\Anaconda3\envs\py2_automation\lib\site-packages\REST\keywords.py", line 1229, in _request instance = self._instantiate(request, response, validate) File "C:\Anaconda3\envs\py2_automation\lib\site-packages\REST\keywords.py", line 1257, in _instantiate self._add_defaults_to_schema(schema, response) File "C:\Anaconda3\envs\py2_automation\lib\site-packages\REST\keywords.py", line 1293, in _add_defaults_to_schema self._add_property_defaults(body, schema['properties'])

I changed the code locally to test if this is working,..

in keywords.py, line 1289

def _add_defaults_to_schema(self, schema, response):
    body = response['body']
    schema = schema['properties']['response']['properties']['body']
    if isinstance(body, (dict)):
        self._add_property_defaults(body, schema['properties'])

this was changed to

def _add_defaults_to_schema(self, schema, response):
    body = response['body']
    schema = schema['properties']['response']['properties']['body']
    # also check if dictionary is empty
    if isinstance(body, (dict)) and not bool(dict):
        self._add_property_defaults(body, schema['properties'])

in case of empty body.

How do you think about it? BR Kev

asyrjasalo commented 5 years ago

Would fix, as well as if isinstance(body, (dict)) and dict:. The dict is True if not empty, False if empty. Very good and thanks for a detailed issue report!

I had to double-check that according to the commit history I already fixed this 17 days ago. I ended up checking there is no key properties in the generated schema: I thought it is more readable, though longer, but the logic is essentially the same.

Just released rc3 where this fix is included. Leaving this open for a while, in case the issue continues to persist.

Caritrian commented 5 years ago

Hi

Thx I will have a look.

Br Kev

asyrjasalo commented 5 years ago

Assuming this fixed - please reopen if the issue persists.