Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: dictionary update sequence element #0 has length 3; 2 is required #55

Closed
v-v-vishnevskiy opened this issue May 31, 2019 · 4 comments

Comments

@v-v-vishnevskiy
Copy link

This happens when I try to send something like this in body:

[
  {"name": "John"},
  {"name": "Max"}
]

with appropriate request schema.

The error in this line https://github.com/maximdanilchenko/aiohttp-apispec/blame/master/aiohttp_apispec/middlewares.py#L37
kwargs is dictionary and can't update with list or tuple.

@v-v-vishnevskiy
Copy link
Author

And in which cases may have several request schemas?

@maximdanilchenko
Copy link
Owner

maximdanilchenko commented May 31, 2019

In my opinion it is more correct and clear to have a name for each value in request body.
Like this:

class Person(Schema):
    name = fields.String()

class ManyPersons(Schema):
    persons = fields.Nested(Person, many=True)

Now you can use ManyPersons schema in @request_schema decorator and body of your request will be like:

{
    "persons": [
      {"name": "John"},
      {"name": "Max"}
    ]
}

I think that it is more understandable approach for API users.

I will think about your case as well. I will try to make a patch to fix it.

@georg-wolflein
Copy link

This error also occurs when deserialising to objects.

For example:

class SomeObject(typing.NamedTuple):
    x: int
    y: int


class SomeObjectSchema(Schema):
    x = fields.Int()
    y = fields.Int()

    @post_load
    def make_object(self, data: dict):
        return SomeObject(**data)

@request_schema(SomeObjectSchema())
async def index(request):
     # ...

@georg-wolflein
Copy link

PR #57 fixes this for me, but I'm not sure if that solves @v-v-vishnevskiy's exact problem too.

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

No branches or pull requests

3 participants