jmcarp / jmcarp/flask-apispec

marshal_with not working with return code

Open
#93 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
652
Forks
151
PR merge metrics
No merged PRs in 30d

Description

works perfectly:

```python
@marshal_with(CreditCardResponseSchema)
def create_card():
card = CreditCard()
return card
```

returns an error:

```python
@marshal_with(CreditCardResponseSchema, code=201)
def create_card():
card = CreditCard()
return card
```

```
Traceback (most recent call last):
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask_jwt/__init__.py", line 177, in decorator
return fn(*args, **kwargs)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask_apispec/annotations.py", line 117, in wrapped
return wrapper(*args, **kwargs)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask_apispec/wrapper.py", line 34, in __call__
return self.marshal_result(unpacked, status_code)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask_apispec/wrapper.py", line 62, in marshal_result
return format_output((format_response(output), ) + unpacked[1:])
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/json.py", line 263, in jsonify
(dumps(data, indent=indent, separators=separators), '\n'),
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/json.py", line 123, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 437, in _iterencode
o = _default(o)
File "/Users/john/.virtualenvs/real_state_app/lib/python3.6/site-packages/flask/json.py", line 80, in default
return _json.JSONEncoder.default(self, o)
File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'CreditCard' is not JSON serializable
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the two create_card examples, comparing marshal_with with and without code=201. Start in flask_apispec/wrapper.py at marshal_result and inspect how the returned CreditCard is formatted before Flask jsonify runs; done means the response is serialized with the requested status code without the JSON serialization error.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.