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

[Bug]Post json string array with pydantic error #198

Open
Jenuce opened this issue May 30, 2023 · 2 comments · May be fixed by #258
Open

[Bug]Post json string array with pydantic error #198

Jenuce opened this issue May 30, 2023 · 2 comments · May be fixed by #258

Comments

@Jenuce
Copy link

Jenuce commented May 30, 2023

Environment (please complete the following information):

  • OS: any
  • Browser any
  • Version 23.3.0

Describe the bug
ex:
curl --location --request POST 'http://127.0.0.1:8080/testarray'
--header 'Content-Type: application/json'
--data-raw '[ "dog","cat"]'
the pydantic example:
https://docs.pydantic.dev/latest/usage/models/ Custom Root Types

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

class Controller(HTTPMethodView):
     @validate(json=Pets)
    async def post(self, req: request.Request, body: Pets):
        return json({})

Expected behavior
sanic_ext.exceptions.ValidationError: Invalid request body: EncryptVO. Error: ModelMetaclass object argument after ** must be a mapping, not list

Additional context
how to fix:

  1. extra/validation/validators.py line 33
    [old]
  def _validate_instance(model, body, allow_coerce):
      data = clean_data(model, body) if allow_coerce else body
      return model(**data)

[new]

 def _validate_instance(model, body, allow_coerce):
     data = clean_data(model, body) if allow_coerce else body
     from sanic_ext.utils.typing import is_pydantic
     if is_pydantic(model) and isinstance(body,list):
          return model.parse_obj(body)
     return model(**data)

2.extensions/openapi/types.py line 301
[old]

              if is_pydantic(value):
                try:
                    value = value.__pydantic_model__
                except AttributeError:
                    ...
                extra = value.schema()["properties"]

[new]

             if is_pydantic(value):
                try:
                    value = value.__pydantic_model__
                except AttributeError:
                    ...
                value_schema = value.schema()
                if "properties" in value_schema:
                    extra = value_schema["properties"]
                else:
                    extra = value_schema
@ahopkins
Copy link
Member

Can you add some code formatting so it is easier to read?

@Panaetius Panaetius linked a pull request Aug 7, 2024 that will close this issue
@Panaetius
Copy link

I'm seeing the same issue. I took a stab at fixing it, but using model_validate instead of the proposed fix above.

leafty added a commit to SwissDataScienceCenter/renku-data-services that referenced this issue Sep 27, 2024
Add `@validate_body_root_model` which is a drop-in replacement for Sanic's `@validate` when validating a `RootModel` or a model deriving from it.

The decorator should be replaced by `@validate` once sanic-org/sanic-ext#198 is fixed.
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

Successfully merging a pull request may close this issue.

3 participants