Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork258
[PoC] Add support forpydantic models viadatamodel-code-generator#1049
-
Hi! I really like this library and use it for all my clients. Unfortunately, I use While To make this possible, only two important changes are necessary:
--- model_property.py.original.jinja2024-06-06 17:54:48+++ model_property.py.jinja2024-06-06 19:07:31@@ -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 %}
--- endpoint_module.py.original.jinja2024-06-06 17:45:47+++ endpoint_module.py.jinja2024-06-06 18:25:13@@ -3,12 +3,22 @@ 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 %}-{{ relative }}+{{ transform_import(relative) }} {% endfor %} {% from "endpoint_macros.py.jinja" import header_params, cookie_params, query_params, To generate our modified client, we have to:
I created a small shell script to do this # openapi-python-client generate --path openapi.json --custom-template-path=templatesopenapi-python-client update --path openapi.json --custom-template-path=templatesrm -r openapi-client/openapi_client/models/*datamodel-codegen --input openapi.json \ --output-model-type pydantic_v2.BaseModel \ --strict-nullable --snake-case-field --capitalize-enum-members \ --use-union-operator --use-standard-collections \> openapi-client/openapi_client/models/__init__.py The Another thing is that Hope this helps someone. Would be amazing to see this as a feature! |
BetaWas this translation helpful?Give feedback.
All reactions
👍 9
Replies: 3 comments 1 reply
-
Yes wondering why thepydantic models are not used by default maybe there's some reason to make it more general purpose. Ran into this when using some endpoints that are pass through and was hoping to reuse the model that was generated. But like you mentioned an option to choose to generate the pydantic models would be great. |
BetaWas this translation helpful?Give feedback.
All reactions
-
This was a very helpful post, thanks@felixatmaltego! I found I had to monkey patch the pydantic Otherwise it fails at the line: |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
this should help alleviate the issue you mention above - I would also note I choose --- 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\", by_alias=True)" %} {% set type_string = property.get_type_string(json=True) %} {% if property.required %} {{ destination }} = {{ transformed }} |
BetaWas this translation helpful?Give feedback.
All reactions
-
Seconding this feature, this capability would be a huge help! It would be nice if this was an extra flag that could be passed into the client generation script. |
BetaWas this translation helpful?Give feedback.