vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.34k stars 437 forks source link

Map Django's EmailField to OpenAPI string format `email` #1329

Open Zerotask opened 2 weeks ago

Zerotask commented 2 weeks ago

Is your feature request related to a problem? Please describe. If you have a model which defines an EmailField, the generated OpenAPI specification is displayed as: email string <= 254 characters image

If you manually define this email field as email: pydantic.EmailStr in your schema, it will be displayed as: email string email image

So in this case, the string format email is used: https://swagger.io/docs/specification/v3_0/data-models/data-types/#strings

image

Describe the solution you'd like

Automatically map Django's EmailField to OpenAPI string format email

There are probably other Django field classes, which could be enhanced with string formats to be more precise

Zerotask commented 2 weeks ago

I noticed the same issue with Django's URLField, which is displayed as a regular string, but if you annotate it with pydantic.HttpUrl in your schema, it will be displayed with the OpenAPI string format uri