From aa0ff90e12e52be2f0e6be0b8e2988275b505677 Mon Sep 17 00:00:00 2001 From: John Boreiko Date: Mon, 1 Sep 2025 13:56:23 +0100 Subject: [PATCH 1/4] adjust templates --- .../templates/endpoint_module.py.jinja | 12 +++++++++++- .../property_templates/model_property.py.jinja | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index 35090614c..579574732 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -3,12 +3,22 @@ from typing import Any, Optional, Union, cast import httpx +{% macro transform_import(original_string) -%} + {%- if original_string.startswith('from ...models') -%} + {%- set parts = original_string.split(' ') -%} + {%- set new_string = 'from ...models import ' + parts[3] -%} + {{ new_string }} + {%- else -%} + {{ original_string }} + {%- endif -%} +{%- endmacro %} + from ...client import AuthenticatedClient, Client from ...types import Response, UNSET from ... import errors {% for relative in endpoint.relative_imports | sort %} -{{ relative }} +{{ transform_import(relative) }} {% endfor %} {% from "endpoint_macros.py.jinja" import header_params, cookie_params, query_params, diff --git a/openapi_python_client/templates/property_templates/model_property.py.jinja b/openapi_python_client/templates/property_templates/model_property.py.jinja index 308b7478b..9ad28f799 100644 --- a/openapi_python_client/templates/property_templates/model_property.py.jinja +++ b/openapi_python_client/templates/property_templates/model_property.py.jinja @@ -1,5 +1,5 @@ {% macro construct_function(property, source) %} -{{ property.class_info.name }}.from_dict({{ source }}) +{{ property.class_info.name }}.model_validate({{ source }}) {% endmacro %} {% from "property_templates/property_macros.py.jinja" import construct_template %} From 3256b1314e9bbbc2bbbd4c4ca6437f5b4eb5a4dc Mon Sep 17 00:00:00 2001 From: John Boreiko Date: Thu, 4 Sep 2025 16:22:33 +0100 Subject: [PATCH 2/4] adopt the library to function with datamodel-codegen pydantic types --- openapi_python_client/parser/properties/enum_property.py | 5 ++++- openapi_python_client/parser/properties/model_property.py | 3 ++- .../templates/property_templates/model_property.py.jinja | 2 +- pyproject.toml | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/openapi_python_client/parser/properties/enum_property.py b/openapi_python_client/parser/properties/enum_property.py index 32389c12b..a7d1b0d04 100644 --- a/openapi_python_client/parser/properties/enum_property.py +++ b/openapi_python_client/parser/properties/enum_property.py @@ -118,7 +118,10 @@ def build( # noqa: PLR0911 ) class_name = data.title or name - if parent_name: + if class_name in ["request_currency_type_0", "response_currency_type_0"]: + # special bypass for two query param that exists on every single endpoint + pass + elif parent_name: class_name = f"{utils.pascal_case(parent_name)}{utils.pascal_case(class_name)}" class_info = Class.from_string(string=class_name, config=config) var_names = data.model_extra.get("x-enum-varnames", []) if data.model_extra else [] diff --git a/openapi_python_client/parser/properties/model_property.py b/openapi_python_client/parser/properties/model_property.py index c7070494a..f08db52f9 100644 --- a/openapi_python_client/parser/properties/model_property.py +++ b/openapi_python_client/parser/properties/model_property.py @@ -66,7 +66,8 @@ def build( process_properties: Determines whether the new ModelProperty will be initialized with property data """ if not config.use_path_prefixes_for_title_model_names and data.title: - class_string = data.title + # biased towards name to avoid collisions with other models and align with the practice in datamodel-codegen + class_string = name else: title = data.title or name if parent_name: diff --git a/openapi_python_client/templates/property_templates/model_property.py.jinja b/openapi_python_client/templates/property_templates/model_property.py.jinja index 9ad28f799..122683f34 100644 --- a/openapi_python_client/templates/property_templates/model_property.py.jinja +++ b/openapi_python_client/templates/property_templates/model_property.py.jinja @@ -11,7 +11,7 @@ {% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %} {% macro transform(property, source, destination, declare_type=True) %} -{% set transformed = source + ".to_dict()" %} +{% set transformed = source + ".model_dump(mode=\"json\")" %} {% set type_string = property.get_type_string(json=True) %} {% if property.required %} {{ destination }} = {{ transformed }} diff --git a/pyproject.toml b/pyproject.toml index ac668fd78..f4d1a4835 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = [ "typing-extensions>=4.8.0,<5.0.0", ] name = "openapi-python-client" -version = "0.24.3" +version = "0.24.3+fork" description = "Generate modern Python clients from OpenAPI" keywords = [ "OpenAPI", From c0f75335891ca0e080449a7c99a6cc5f1b9e600b Mon Sep 17 00:00:00 2001 From: John Boreiko Date: Fri, 5 Sep 2025 13:13:26 +0100 Subject: [PATCH 3/4] add by_alias=True to ensure proper serialisation --- .../templates/property_templates/model_property.py.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_python_client/templates/property_templates/model_property.py.jinja b/openapi_python_client/templates/property_templates/model_property.py.jinja index 122683f34..8d796c4c4 100644 --- a/openapi_python_client/templates/property_templates/model_property.py.jinja +++ b/openapi_python_client/templates/property_templates/model_property.py.jinja @@ -11,7 +11,7 @@ {% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %} {% macro transform(property, source, destination, declare_type=True) %} -{% set transformed = source + ".model_dump(mode=\"json\")" %} +{% set transformed = source + ".model_dump(mode=\"json\", by_alias=True)" %} {% set type_string = property.get_type_string(json=True) %} {% if property.required %} {{ destination }} = {{ transformed }} From 06272ea72e94f379878973c9d3dc933d0ec5fa6c Mon Sep 17 00:00:00 2001 From: John Boreiko Date: Mon, 15 Sep 2025 13:29:59 +0100 Subject: [PATCH 4/4] revert global param path override --- openapi_python_client/parser/properties/enum_property.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/openapi_python_client/parser/properties/enum_property.py b/openapi_python_client/parser/properties/enum_property.py index a7d1b0d04..32389c12b 100644 --- a/openapi_python_client/parser/properties/enum_property.py +++ b/openapi_python_client/parser/properties/enum_property.py @@ -118,10 +118,7 @@ def build( # noqa: PLR0911 ) class_name = data.title or name - if class_name in ["request_currency_type_0", "response_currency_type_0"]: - # special bypass for two query param that exists on every single endpoint - pass - elif parent_name: + if parent_name: class_name = f"{utils.pascal_case(parent_name)}{utils.pascal_case(class_name)}" class_info = Class.from_string(string=class_name, config=config) var_names = data.model_extra.get("x-enum-varnames", []) if data.model_extra else []