mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Make sure request bodies without explicit names are named 'body'
The `Parameter` class now provides default Python names based on the parameter location when the original name is empty. This prevents parameters from having an empty string as their Python name, especially for request bodies defined without a top-level name. Close #2213 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 834850255
This commit is contained in:
committed by
Copybara-Service
parent
bf8b85da52
commit
084c2de0da
@@ -64,11 +64,9 @@ class ApiParameter(BaseModel):
|
||||
required: bool = False
|
||||
|
||||
def model_post_init(self, _: Any):
|
||||
self.py_name = (
|
||||
self.py_name
|
||||
if self.py_name
|
||||
else rename_python_keywords(_to_snake_case(self.original_name))
|
||||
)
|
||||
if not self.py_name:
|
||||
inferred_name = rename_python_keywords(_to_snake_case(self.original_name))
|
||||
self.py_name = inferred_name or self._default_py_name()
|
||||
if isinstance(self.param_schema, str):
|
||||
self.param_schema = Schema.model_validate_json(self.param_schema)
|
||||
|
||||
@@ -77,6 +75,16 @@ class ApiParameter(BaseModel):
|
||||
self.type_hint = TypeHintHelper.get_type_hint(self.param_schema)
|
||||
return self
|
||||
|
||||
def _default_py_name(self) -> str:
|
||||
location_defaults = {
|
||||
'body': 'body',
|
||||
'query': 'query_param',
|
||||
'path': 'path_param',
|
||||
'header': 'header_param',
|
||||
'cookie': 'cookie_param',
|
||||
}
|
||||
return location_defaults.get(self.param_location or '', 'value')
|
||||
|
||||
@model_serializer
|
||||
def _serialize(self):
|
||||
return {
|
||||
|
||||
@@ -139,10 +139,19 @@ class OperationParser:
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Prefer explicit body name to avoid empty keys when schema lacks type
|
||||
# information (e.g., oneOf/anyOf/allOf) while retaining legacy behavior
|
||||
# for simple scalar types.
|
||||
if schema and (schema.oneOf or schema.anyOf or schema.allOf):
|
||||
param_name = 'body'
|
||||
elif not schema or not schema.type:
|
||||
param_name = 'body'
|
||||
else:
|
||||
param_name = ''
|
||||
|
||||
self._params.append(
|
||||
# Empty name for unnamed body param
|
||||
ApiParameter(
|
||||
original_name='',
|
||||
original_name=param_name,
|
||||
param_location='body',
|
||||
param_schema=schema,
|
||||
description=description,
|
||||
|
||||
Reference in New Issue
Block a user