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

OpenAPI extension parses dataclass validators as body #89

Closed
Silur opened this issue Jun 4, 2022 · 2 comments · Fixed by #98
Closed

OpenAPI extension parses dataclass validators as body #89

Silur opened this issue Jun 4, 2022 · 2 comments · Fixed by #98

Comments

@Silur
Copy link

Silur commented Jun 4, 2022

#from pydantic.dataclasses import dataclass
from pydantic.dataclasses import dataclass

@dataclass
class RegisterBody:
    name: str
    email: str
    password: str

    @validator('name')
    def validate_name(cls, v):
        if len(v) < 5:
            raise ValueError('name must be at leasth 5 characters')

@app.post("/register")
@openapi.body({"application/json": RegisterBody})
@validate(json=RegisterBody)
async def register(request, body):
     return json({})

The code above produces the OpenAPI /docs page to populate a validator_name field in the Swagger UI, while for the actual mode it is not a field. Tried with the builting python dataclasses and the Pydantic wrapper too.

image

Sanic v22.3.2
Sanic-ext v22.3.2
Arch Linux x86_64
Python 3.10.4

@SerGeRybakov
Copy link

Because "schema" is just schema. It's better to have schemas separately from objects with working business logic. You can inherit classes with business logic from schema objects.

@ahopkins
Copy link
Member

Fix on the way. FYI...

@openapi.body({"application/json": RegisterBody}, validate=True)

is the same as

@openapi.body({"application/json": RegisterBody})
@validate(json=RegisterBody)

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