Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Parse object from GET parameters #580

Closed
zueve opened this issue Jun 7, 2018 · 4 comments
Closed

Parse object from GET parameters #580

zueve opened this issue Jun 7, 2018 · 4 comments

Comments

@zueve
Copy link

zueve commented Jun 7, 2018

Version 0.5.27
Now i can't parse object if i send params in query (works only if send in body). Example:

from apistar import App, Route, types, validators

class Product(types.Type):
    name = validators.String(max_length=100)
    rating = validators.Integer(minimum=1, maximum=5)

def welcome(product: Product) -> Product:
    return product

routes = [
    Route('/', method='POST', handler=welcome),
    Route('/', method='GET', handler=welcome, name='welcome_get'),
]

app = App(routes=routes)
if __name__ == '__main__':
    app.serve('127.0.0.1', 5000, debug=True)

POST query working good:

curl -X POST -F 'name=Name' -F 'rating=5' http://127.0.0.1:5000/
200 - {"rating":5,"name":"Name"}

GET Query dosn't work:

curl http://127.0.0.1:5000/?name=Name&rating=5
400 - "Must be an object."

@mekicha
Copy link

mekicha commented Jun 12, 2018

I think this is similar to #487

@zueve
Copy link
Author

zueve commented Jun 13, 2018

I think this is not same. This issue is bug because apistar generate correct openapi scheme:

 "paths": {
        "/": {
            "get": {
                "operationId": "welcome_get",
                "parameters": [
                    {
                        "name": "name",
                        "in": "query",
                        "schema": {
                            "type": "string",
                            "maxLength": 100
                        }
                    },
                    {
                        "name": "rating",
                        "in": "query",
                        "schema": {
                            "type": "integer",
                            "minimum": 1.0,
                            "maximum": 5.0
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Product"
                                }
                            }
                        }
                    }
                }
            },

But api don't work

@danigosa
Copy link
Contributor

The only way to do what you try to do is by separating the handlers and using the same type validator:

from apistar import App, Route, types, validators, http

class Product(types.Type):
    name = validators.String(max_length=100)
    rating = validators.Integer(minimum=1, maximum=5)

def post_welcome(product: Product) -> Product:
    return product

def get_welcome(params: http.QueryParams) -> Product:
    product = Product(dict(params)).validate()  # yeah, it's hacky
    return product

routes = [
    Route('/', method='POST', handler=post_welcome),
    Route('/', method='GET', handler=get_welcome, name='welcome_get'),
]

app = App(routes=routes)
if __name__ == '__main__':
    app.serve('127.0.0.1', 5000, debug=True)

Yes having a Type annotation for GET requests is as easy as parse the query params but it's not implemented yet, in the forum I opened another thread discussing about a related matter (among others): declare parameters with a direct validator instance annotation.

@tomchristie
Copy link
Member

Closing this off given that 0.6 is moving to a framework-agnostic suite of API tools, and will no longer include the server. See https://discuss.apistar.org/t/api-star-as-a-framework-independant-tool/614 and #624.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants