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

Update pydantic and fastapi #34

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- name: Checkout 🔁
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ router = SpecRouter(specs, validators=[MyValidator])

You will need:

- Python 3.7+ (though 3.11+ might have some issues with dependencies)
- Python 3.8+ (though 3.11+ might have some issues with dependencies)
- [pre-commit](https://pre-commit.com/#install)

Before working on the project, make sure you run:
Expand Down
11 changes: 7 additions & 4 deletions openapi_to_fastapi/model_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

from datamodel_code_generator import PythonVersion
from datamodel_code_generator.model import pydantic as pydantic_model
from datamodel_code_generator.model import pydantic_v2 as pydantic_model
from datamodel_code_generator.parser.openapi import OpenAPIParser

from openapi_to_fastapi.logger import logger
Expand All @@ -23,15 +23,18 @@ def generate_model_from_schema(schema: str, format_code: bool = False) -> str:
parser = OpenAPIParser(
source=schema,
data_model_type=pydantic_model.BaseModel,
data_model_root_type=pydantic_model.CustomRootType,
data_model_root_type=pydantic_model.RootModel,
data_type_manager_type=pydantic_model.DataTypeManager,
data_model_field_type=pydantic_model.DataModelField,
base_class="pydantic.BaseModel",
custom_template_dir=None,
extra_template_data=None,
target_python_version=PythonVersion.PY_37,
target_python_version=PythonVersion.PY_38,
dump_resolve_reference_action=None,
validation=True,
# Validation of OpenAPI 3.1 specs isn't supported right now. Issues:
# https://github.com/koxudaxi/datamodel-code-generator/issues/1649 and
# https://github.com/RonnyPfannschmidt/prance/issues/95
validation=False,
omarudolley marked this conversation as resolved.
Show resolved Hide resolved
field_constraints=False,
snake_case_field=False,
strip_default_none=False,
Expand Down
9 changes: 4 additions & 5 deletions openapi_to_fastapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
from typing import Dict, Optional

from fastapi.openapi import models as oas
from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field


class Header(oas.Header):
class Config:
extra = "ignore"
model_config = ConfigDict(extra="ignore")


class ParsedResponse(BaseModel):
description: Optional[str]
model_name: Optional[str]
description: Optional[str] = None
name: Optional[str] = None


class Operation(oas.Operation):
Expand Down
2 changes: 1 addition & 1 deletion openapi_to_fastapi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def parse_operation(spec: dict, name: str) -> Optional[Operation]:

operation.parsedResponses[code] = ParsedResponse(
description=resp_data.get("description"),
model_name=get_model_name_from_ref(resp_data),
name=get_model_name_from_ref(resp_data),
)

operation.headers = {
Expand Down
4 changes: 2 additions & 2 deletions openapi_to_fastapi/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _validate_and_parse_specs(self, cleanup=True):
)

for status_code, parsed_response in post.parsedResponses.items():
resp_model = getattr(models, parsed_response.model_name)
resp_model = getattr(models, parsed_response.name)
description = parsed_response.description
if status_code == 200:
route_info.response_model = resp_model
Expand All @@ -144,7 +144,7 @@ def _validate_and_parse_specs(self, cleanup=True):
additional_response = {}
if parsed_response.description:
additional_response["description"] = description
if parsed_response.model_name:
if parsed_response.name:
additional_response["model"] = resp_model

route_info.responses[status_code] = additional_response
Expand Down
Loading