Giter VIP home page Giter VIP logo

Comments (3)

kemingy avatar kemingy commented on May 21, 2024 1

I see. I think this is caused by BaseModel.validate(). I should use BaseModel.parse_obj() instead. Will create a PR to fix this.

Thanks.

from spectree.

kemingy avatar kemingy commented on May 21, 2024

Here is an example:

from wsgiref import simple_server
import falcon
from pydantic import BaseModel
from typing import Dict
from spectree import SpecTree


class Req(BaseModel):
    __root__: Dict[str, str]


api = SpecTree('falcon')


class Index:
    @api.validate(json=Req)
    def on_post(self, req, resp):
        print(req.context.json)
        resp.media = {'msg': 'ok'}


if __name__ == "__main__":
    app = falcon.App()
    api.register(app)
    app.add_route('/', Index())

    httpd = simple_server.make_server("localhost", 8000, app)
    httpd.serve_forever()

So if I send a request with httpie: http ':8000/' hello=world, I will get the correct output.

>> __root__={'hello': 'world'}
>> 127.0.0.1 - - [11/Dec/2020 22:57:26] "POST / HTTP/1.1" 200 13

HTTP/1.0 200 OK
Date: Fri, 11 Dec 2020 14:57:26 GMT
Server: WSGIServer/0.2 CPython/3.6.10
content-length: 13
content-type: application/json

{
    "msg": "ok"
}

Maybe you can provide an example to reproduce the bug?

from spectree.

i404788 avatar i404788 commented on May 21, 2024

The error is in response validation.

import falcon
from pydantic import BaseModel
from typing import Dict
from spectree import SpecTree, Response

spectree = SpecTree('falcon', title='API', version='v1')


class GetResp(BaseModel):
    __root__: Dict[str, str]


class Test:
    @spectree.validate(resp=Response(HTTP_200=GetResp))
    def on_get(self, req, resp):
        resp.media = {'a': 'b'}

api = falcon.App()
api.add_route('/api/test', Test())

With both waitress-server and gunicorn it will give back:

[{"loc": ["__root__"], "msg": "field required", "type": "value_error.missing"}]

Interestingly enough it does work with:

class GetResp(BaseModel):
    __root__: List[str]

class Test:
    @spectree.validate(resp=Response(HTTP_200=GetResp))
    def on_get(self, req, resp):
        resp.media = ['a', 'b']

from spectree.

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.