Skip to main content

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

Reference another 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"
	}
}

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.1.0.tar.gz (9.4 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.1.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: drf_pydantic_openapi-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.2 Darwin/22.2.0

File hashes

Hashes for drf_pydantic_openapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 331b7023444a367b08c845b39c04a2aea4d2d151cc24466cc3f43188ee31f242
MD5 6bb6154026122a7b498a34580f8138fb
BLAKE2b-256 7b511808fb0bb3ea0855ecb97f0e568c721c43e49b29221ff327968d56816638

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for drf_pydantic_openapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91e49af886f1610ca521a5d0b6a0f99b38c49dbac7518c79d5dd5769a46d31ea
MD5 835d72e94cc0e2511802c49ef6cbf537
BLAKE2b-256 38415f8c2b7a5e844c53e008a6836a909b0bcf5fcdf508b86f90c3d6ff36f974

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.5

2 files

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6

2 files

0.5.6

2 files

0.5.5

2 files

0.5.4

2 files

0.5.3

2 files

0.5.2

2 files

0.5

2 files

0.4.6

2 files

0.4.5

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

1 file

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page