Skip to content

Commit

Permalink
Add Support for Pydantic V2 (#6)
Browse files Browse the repository at this point in the history
* Bump fastAPI to `^0.104.1`, Pydantic to `^2.4.2`

* Add breaking changes to only support Pydantic V2  in fastapi-docx >=1.0.

* Update README to mention that only V2 of Pydantic is supported in fastapi-docx >=1.0.
  • Loading branch information
Saran33 authored Nov 12, 2023
1 parent 8feb7b4 commit c41118c
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 723 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ dmypy.json

# generated responses from test fails
*.json
!**/unit_tests/*.json

.DS_Store
log.txt
25 changes: 0 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,113 +63,88 @@ venv-install:
fi
@${MAKE} poetry-install

.PHONY: poetry-setup
poetry-setup:
@echo "Installing poetry..."
@curl -sSL https://install.python-poetry.org | python3 -
@${eval include ${HOME}/.poetry/env}

.PHONY: poetry-install
poetry-install:
@${POETRY} config virtualenvs.in-project true && \
${POETRY} config virtualenvs.prompt ${VENV_NAME} && \
${POETRY} install ${poetry-install-flags}

.PHONY: poetry-build
poetry-build:
@${POETRY} build

.PHONY: poetry-publish
poetry-publish:
@${POETRY} publish --build

.PHONY: poetry-update
poetry-update:
@${POETRY} update

.PHONY: poetry-update-lock
poetry-update-lock:
@${POETRY} update --lock

.PHONY: poetry-add
poetry-add:
@${POETRY} add ${PACKAGE}

.PHONY: poetry-add-dev
poetry-add-dev:
@${POETRY} add --group dev ${PACKAGE}

.PHONY: poetry-add-docs
poetry-add-docs:
@${POETRY} add --group docs ${PACKAGE}

.PHONY: poetry-remove
poetry-remove:
@${POETRY} remove ${PACKAGE}

.PHONY: poetry-remove-dev
poetry-remove-dev:
@${POETRY} remove --group dev ${PACKAGE}

.PHONY: poetry-add-pypi-token
poetry-add-pypi-token:
@${POETRY} config pypi-token.pypi ${PYPI_TOKEN}

.PHONY: pre-commit-install
pre-commit-install: venv-install
${VENV}/bin/pre-commit install

.PHONY: pre-commit-uninstall
pre-commit-uninstall:
${VENV}/bin/pre-commit uninstall

.PHONY: pre-commit-run
pre-commit-run: venv-install
${venv-activate} && pre-commit run --all-files

.PHONY: mypy
mypy: venv-install
${VENV}/bin/mypy --config pyproject.toml

.PHONY: lint
lint: mypy pre-commit-run

.PHONY: unit-tests
unit-tests: venv-install
${POETRY} run pytest ./tests/unit_tests -n auto

.PHONY: unit-tests-with-cov
unit-tests-with-cov: venv-install
${POETRY} run pytest ./tests/unit_tests --cov \
--cov-config=setup.cfg --cov-report xml --cov-report html -n auto

.PHONY: open-coverage-report
open-coverage-report:
cd coverage_report/coverage.html && \
open -a index.html

.PHONY: coverage-badge
coverage-badge:
${POETRY} run genbadge coverage \
-i coverage_report/coverage.xml \
-o coverage_report/coverage-badge.svg

.PHONY: serve-docs
serve-docs:
${POETRY} run mkdocs serve

.PHONY: build-docs
build-docs:
${POETRY} run mkdocs build

.PHONY: add-cov-to-docs
add-cov-to-docs:
@if [ -d "coverage_report" ]; then \
rsync -avz --progress coverage_report/ docs/coverage/; \
else \
echo "coverage_report directory not found, skipping report copy"; \
fi;

.PHONY: deploy-docs
deploy-docs: add-cov-to-docs
${POETRY} run mkdocs gh-deploy --force
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ The key features are:
* **Include Custom Exceptions**: Optionally find and document any custom Exception types if using custom Exception handlers in your FastAPI application.
* **Generate Exception schemas**: A default `HTTPExceptionSchema` will be added to the OpenAPI specification. The default can be modified to use any other [Pydantic](*https://github.com/pydantic/pydantic) model. An additional schema for app-specific custom Exceptions can also be included.


##### Dependencies
* [Pydantic V2](https://github.com/pydantic/pydantic): From version 1.0 of fastapi-docx, Pydantic V2 is required. For Pydantic V1 support, use fastapi-docx version 0.2.

##### License
This project is licensed under the terms of the MIT license.
2 changes: 1 addition & 1 deletion fastapi_docx/exception_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def is_subclass_of_any(klass: type, classes: Iterable[type] | Iterable[str]) ->
class_names = classes
else:
class_names = [cls.__name__ for cls in classes if hasattr(cls, "__name__")]
return any(base_name in class_names for base_name in base_names)
return any(base_name in class_names for base_name in base_names) # type: ignore


def is_callable_instance(obj: object) -> bool:
Expand Down
11 changes: 7 additions & 4 deletions fastapi_docx/response_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any, TypeVar

from fastapi.openapi.constants import REF_PREFIX
from fastapi.openapi.constants import REF_TEMPLATE
from fastapi.routing import APIRoute
from pydantic import BaseModel
from pydantic.schema import model_schema
from pydantic.json_schema import GenerateJsonSchema
from starlette.exceptions import HTTPException

from fastapi_docx.exception_finder import ErrType
Expand All @@ -17,13 +17,16 @@ class HTTPExceptionSchema(BaseModel):

def get_model_definition(model: type[BaseModel]) -> tuple[str, dict[str, Any]]:
model_name = model.__name__
m_schema = model_schema(model, ref_prefix=REF_PREFIX)
schema_generator = GenerateJsonSchema(by_alias=True, ref_template=REF_TEMPLATE)
m_schema = schema_generator.generate(
model.__pydantic_core_schema__, mode="serialization"
)
if "description" in m_schema:
m_schema["description"] = m_schema["description"].split("\f")[0]
return model_name, m_schema


def add_model_to_openapi(api_schema: dict[str, Any], model: BaseModel) -> None:
def add_model_to_openapi(api_schema: dict[str, Any], model: type[BaseModel]) -> None:
model_name, m_schema = get_model_definition(model)
if "components" not in api_schema:
api_schema["components"] = {"schemas": {}}
Expand Down
Loading

0 comments on commit c41118c

Please sign in to comment.