Comments (5)
test.py
from apiflask import APIFlask
from marshmallow_dataclass import dataclass
app = APIFlask(__name__)
@dataclass
class Req:
var1: int | str
@app.post("/")
@app.input(Req.Schema)
def index():
return {"message": "lol"}
shell
flask --app test:app run
req
curl http://127.0.0.1:5000 -H 'Content-Type: application/json' -d '{"var1": true}'
output:
* Serving Flask app 'test:app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
[2024-02-05 12:37:31,799] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/app.py", line 2528, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/app.py", line 1826, in full_dispatch_request
return self.finalize_request(rv)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/app.py", line 1845, in finalize_request
response = self.make_response(rv)
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/app.py", line 2156, in make_response
rv = self.json.response(rv)
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/json/provider.py", line 309, in response
f"{self.dumps(obj, **dump_args)}\n", mimetype=mimetype
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/json/provider.py", line 230, in dumps
return json.dumps(obj, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/Users/tarun/workspace/feature-foundation/venv/lib/python3.11/site-packages/flask/json/provider.py", line 122, in _default
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
TypeError: Object of type ValidationError is not JSON serializable
127.0.0.1 - - [05/Feb/2024 12:37:31] "POST / HTTP/1.1" 500 -
from apiflask.
I have found the reason.
When union fields raise errors, marshmallow_dataclass adds the error objects into errors
directly.
But marshmallow hints errors
should be dict[str, list[str]]
.
marshmallow_dataclass doesn't obey it.
from apiflask.
Hi, could you provide a minimal example to reproduce this issue?
from apiflask.
Thanks for the example. After some investigation, I find the root cause of this issue. When handling an error with union type, the error message generated by marshmallow-dataclass will be a list of ValidationError
objects.
from apiflask.
We could report it to marshmallow-dataclass or help them fix it.
from apiflask.
Related Issues (20)
- Deprecation warning with Flask 3.0 HOT 2
- Allow defining additional media types for a response
- `pagination_builder` data is not always compliant with PaginationSchema HOT 1
- Endpoints with authentication in debug mode HOT 1
- Support adding headers to the response schema HOT 2
- Support the OpenTelemetry into APIFlask
- OneOf, ContainsOnly for dataclass
- .
- Support Pydantic HOT 1
- Support `from apiflask import current_app` HOT 2
- `@app.error_processor` with specific errors HOT 2
- WebSocket support ?
- Re-use the File, Config field, and file-related validators from flask-marshmallow HOT 1
- `flask spec` command raises TypeError on decimal field validator
- Add support for a --quiet flag for the spec command HOT 2
- Behaviur when use files type parameters HOT 5
- Can APIFlask support Quart? HOT 1
- Create Combo Box HOT 1
- Create migration guides for deprecated/unmaintained Flask REST extensions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apiflask.