Skip to main content

OpenAPI (v3) schema generation via Pydantic models using Django REST Framework.

Project description

Generate OpenAPI schema with DRF code using pydantic models. Supports referencing other service's components.

Usage

Add urls to the project

 urlpatters = [
    path("openapi/", include("drf_pydantic_openapi.urls")),
 ]
  • Available endpoints
    • /schema.json
    • /docs
    • /redoc

Reference OpenAPI source

Add the following setting to your projects settings.py. This will allow the module to access to the other OpenAPI components defined in seperate projects.

# settings.py

DRF_PYDANTIC_OPENAPI = {
    "REF_SOURCES": {
        "service_B": "http://localhost:8000/openapi",
        "service_C": "http://localhost:8001/openapi",
    }
}

Using a component defined in another service

# views.py
from drf_pydantic_openapi.ref_utils import RefType

BookModel = RefType("service_B", "BookModel")

class BookView(ApiView):
    def get(self, request) -> BookModel:
        ...

# More complex example

class CustomBookModel(RefType("service_B", "BookModel")):
    # add new field
    read_count: int

    class Config:
        # Remove field from referenced type
        ref_exclude = ("author",)
        # Rename field
        ref_rename = (("book_name", "name"),)

class PaginatedBookModel(BaseModel):
    total: int
    next: str
    prev: str
    data: list[CustomBookModel]

class BookView(ApiView):
    def get(self, request) -> PaginatedBookModel:
        ...

Using the @docs

  • Parameters
    • body: Request body model
    • errors: List of error models
    • query: Query model
    • path: Api path parameter model
from pydantic import BaseModel
from drf_pydantic_openapi.errors import BadRequestError, NotFoundError

class RetrieveQuery(BaseModel):
    book_id: str

class Path(BaseModel):
    book_id: str = Field(description="Book id to retrieve")


class BookView(ApiView):
    @docs(errors=[NotFoundError], query=RetrieveQuery, path=Path)
    def get(self, request):
        ...
...

Typed exception handler

Assign the typed_exception_handler to rest framework. This will catch any ValidationError and the custom HttpError and return the response as json.

# settings.py

REST_FRAMEWORK = {
    "EXCEPTION_HANDLER": "drf_pydantic_openapi.exception_handler.typed_exception_handler"
}
# Example
from drf_pydantic_openapi.errors import BadRequestError
class SomeView(ApiView):
    @docs(errors=[BadRequestError])
    def post(self, request):
        raise BadRequestError()

Response

{
	"detail": {
		"message": "Bad request"
	}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_pydantic_openapi-0.6.4.tar.gz (931.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_pydantic_openapi-0.6.4-py3-none-any.whl (938.1 kB view details)

Uploaded Python 3

File details

Details for the file drf_pydantic_openapi-0.6.4.tar.gz.

File metadata

  • Download URL: drf_pydantic_openapi-0.6.4.tar.gz
  • Upload date:
  • Size: 931.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.9.1 Darwin/24.5.0

File hashes

Hashes for drf_pydantic_openapi-0.6.4.tar.gz
Algorithm Hash digest
SHA256 7a4172728ecb436ec5af91d603288b57209c6e440ac8ab2673f6035152a90189
MD5 c326e5053c233ee4f9a81b9a0b8f3182
BLAKE2b-256 54dfd7cdb413ae3f005a3f353e9debe799a24d4565f63eca038ae83ca0f3bf60

See more details on using hashes here.

File details

Details for the file drf_pydantic_openapi-0.6.4-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_pydantic_openapi-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 34e3ca82e2cbbd3273b2d0136825148e594eb7ebddc8dcf76b893cff657367a9
MD5 e74eece8ed2faa72a2ad3f0ced758c54
BLAKE2b-256 f3062b7e3ac861088eee53974e5331b77b386b461e907c43ed44e790258d07ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page