Skip to content

Commit 64df18e

Browse files
authored
fix(generator): omit unset fields in generated request bodies (#676)
* fix(generator): omit unset fields in generated request bodies - Serialize Pydantic bodies with exclude_unset=True so absent optional values are not sent as null. - Generate omissible fields with None defaults and alias same-name model refs to avoid import/class collisions. Signed-off-by: Jessie Frazelle <github@jessfraz.com> * chore(release): bump kittycad to 1.3.8 Signed-off-by: Jessie Frazelle <github@jessfraz.com> * fix(client): tolerate extra fields in API responses - Generate Pydantic validation with extra="ignore" for models and TypeAdapter paths. - Add a regression test so additive response fields avoid surprise ValidationErrors. Signed-off-by: Jessie Frazelle <github@jessfraz.com> --------- Signed-off-by: Jessie Frazelle <github@jessfraz.com>
1 parent 2148e99 commit 64df18e

106 files changed

Lines changed: 1545 additions & 723 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎generate/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""SDK generator package."""

‎generate/function_generators.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
def _generate_docstring_with_examples(
13-
endpoint: dict, file_info: dict, request_body_type: str
13+
endpoint: dict, file_info: dict, request_body_type: str | None
1414
) -> str:
1515
"""Generate docstring with usage examples for endpoints."""
1616
base_docs = endpoint.get("description", endpoint.get("summary", ""))

‎generate/post_processing.py‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@
88

99
import jsonpatch
1010

11-
try:
12-
from .utils import (
13-
extract_imports_from_examples,
14-
format_file_with_ruff,
15-
to_pascal_case,
16-
)
17-
except ImportError:
18-
from utils import (
19-
extract_imports_from_examples,
20-
format_file_with_ruff,
21-
to_pascal_case,
22-
)
11+
from .utils import extract_imports_from_examples, format_file_with_ruff, to_pascal_case
2312

2413

2514
def generate_examples_tests(cwd: str, examples: List[str]):

‎generate/templates/__init__.py.jinja2‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ from websockets.sync.client import (
2020
from .client import AsyncClient, Client
2121
from .pagination import AsyncPageIterator, SyncPageIterator
2222
from .response_helpers import raise_for_status
23+
from .types import serialize_request_body
2324
from .exceptions import (
2425
KittyCADError,
2526
KittyCADAPIError,

‎generate/templates/async_function.py.jinja2‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
response = await _client.{{ method }}(
4848
url=url,
4949
headers=self.client.get_headers(),
50-
{% if has_request_body %}{% if request_body_type != "bytes" %}content=body.model_dump_json(),{% else %}content=body,{% endif %}{% endif %}
50+
{% if has_request_body %}{% if request_body_type != "bytes" %}content=serialize_request_body(body),{% else %}content=body,{% endif %}{% endif %}
5151
)
5252
{% endif %}
5353

@@ -63,10 +63,10 @@
6363
{% if response_type.startswith('Union') or response_type.startswith('Optional[Union') or response_type.startswith('Dict') or response_type.startswith('List') or response_type.startswith('dict') or response_type.startswith('list') %}
6464
# Validate into annotated/collection/union types using TypeAdapter
6565
from pydantic import TypeAdapter
66-
return TypeAdapter({{ response_type }}).validate_python(json_data)
66+
return TypeAdapter({{ response_type }}).validate_python(json_data, extra="ignore")
6767
{% else %}
6868
# Validate into a Pydantic model (works for BaseModel and RootModel)
69-
return {{ response_type }}.model_validate(json_data)
69+
return {{ response_type }}.model_validate(json_data, extra="ignore")
7070
{% endif %}
7171
{% else %}
7272
return response.json() if response.content else None

‎generate/templates/async_paginated_function.py.jinja2‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
response = await _client.{{ method }}(
8686
url=url,
8787
headers=self.client.get_headers(),
88-
{% if has_request_body %}{% if request_body_type != "bytes" %}content=body.model_dump_json(),{% else %}content=body,{% endif %}{% endif %}
88+
{% if has_request_body %}{% if request_body_type != "bytes" %}content=serialize_request_body(body),{% else %}content=body,{% endif %}{% endif %}
8989
)
9090

9191
if not response.is_success:
@@ -97,4 +97,4 @@
9797

9898
json_data = response.json()
9999
# Validate into a Pydantic model (supports BaseModel/RootModel)
100-
return {{ response_type }}.model_validate(json_data)
100+
return {{ response_type }}.model_validate(json_data, extra="ignore")

‎generate/templates/shared_macros.jinja2‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
response = {% if is_async %}await {% endif %}_client.{{ method }}(
108108
url=url,
109109
headers=self.client.get_headers(),
110-
{% if has_request_body %}{% if request_body_type != "bytes" %}content=body.model_dump_json(),{% else %}content=body,{% endif %}{% endif %}
110+
{% if has_request_body %}{% if request_body_type != "bytes" %}content=serialize_request_body(body),{% else %}content=body,{% endif %}{% endif %}
111111
)
112112
{% endif %}
113113
{%- endmacro %}
@@ -130,7 +130,7 @@
130130

131131
json_data = response.json()
132132
{% if not response_type.startswith(('dict', 'list', 'Union', 'RootModel')) %}
133-
return {{ response_type }}(**json_data)
133+
return {{ response_type }}.model_validate(json_data, extra="ignore")
134134
{% else %}
135135
return json_data
136136
{% endif %}

‎generate/templates/sync_function.py.jinja2‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
response = _client.{{ method }}(
4848
url=url,
4949
headers=self.client.get_headers(),
50-
{% if has_request_body %}{% if request_body_type != "bytes" %}content=body.model_dump_json(),{% else %}content=body,{% endif %}{% endif %}
50+
{% if has_request_body %}{% if request_body_type != "bytes" %}content=serialize_request_body(body),{% else %}content=body,{% endif %}{% endif %}
5151
)
5252
{% endif %}
5353

@@ -63,10 +63,10 @@
6363
{% if response_type.startswith('Union') or response_type.startswith('Optional[Union') or response_type.startswith('Dict') or response_type.startswith('List') or response_type.startswith('dict') or response_type.startswith('list') %}
6464
# Validate into annotated/collection/union types using TypeAdapter
6565
from pydantic import TypeAdapter
66-
return TypeAdapter({{ response_type }}).validate_python(json_data)
66+
return TypeAdapter({{ response_type }}).validate_python(json_data, extra="ignore")
6767
{% else %}
6868
# Validate into a Pydantic model (works for BaseModel and RootModel)
69-
return {{ response_type }}.model_validate(json_data)
69+
return {{ response_type }}.model_validate(json_data, extra="ignore")
7070
{% endif %}
7171
{% else %}
7272
return response.json() if response.content else None

‎generate/templates/sync_paginated_function.py.jinja2‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
response = _client.{{ method }}(
8686
url=url,
8787
headers=self.client.get_headers(),
88-
{% if has_request_body %}{% if request_body_type != "bytes" %}content=body.model_dump_json(),{% else %}content=body,{% endif %}{% endif %}
88+
{% if has_request_body %}{% if request_body_type != "bytes" %}content=serialize_request_body(body),{% else %}content=body,{% endif %}{% endif %}
8989
)
9090

9191
if not response.is_success:
@@ -97,4 +97,4 @@
9797

9898
json_data = response.json()
9999
# Validate into a Pydantic model (supports BaseModel/RootModel)
100-
return {{ response_type }}.model_validate(json_data)
100+
return {{ response_type }}.model_validate(json_data, extra="ignore")

‎generate/tests/test_acronym_fix.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
#!/usr/bin/env python3
22
"""Test script to verify the improved camel_to_snake function handles acronyms correctly."""
33

4-
import os
5-
import sys
6-
74
import pytest
85

9-
# Add the parent directory to the path to import generate modules
10-
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
11-
from utils import camel_to_snake
6+
from generate.utils import camel_to_snake
127

138

149
class TestAcronymHandling:

0 commit comments

Comments
 (0)