Code Monkey home page Code Monkey logo

Comments (5)

tarunchhn avatar tarunchhn commented on June 1, 2024 1

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.

uncle-lv avatar uncle-lv commented on June 1, 2024 1

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.

greyli avatar greyli commented on June 1, 2024

Hi, could you provide a minimal example to reproduce this issue?

from apiflask.

greyli avatar greyli commented on June 1, 2024

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.

https://github.com/lovasoa/marshmallow_dataclass/blob/d6396c18470582a4fe5f0f2bd29ac012da4f0f1f/marshmallow_dataclass/union_field.py#L80

from apiflask.

greyli avatar greyli commented on June 1, 2024

We could report it to marshmallow-dataclass or help them fix it.

from apiflask.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.