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
@@ -74,6 +74,24 @@ class TestApiParameter:
|
||||
)
|
||||
assert param.py_name == 'param_in'
|
||||
|
||||
def test_api_parameter_uses_location_default_when_name_missing(self):
|
||||
schema = Schema(type='string')
|
||||
param = ApiParameter(
|
||||
original_name='',
|
||||
param_location='body',
|
||||
param_schema=schema,
|
||||
)
|
||||
assert param.py_name == 'body'
|
||||
|
||||
def test_api_parameter_uses_value_default_when_location_unknown(self):
|
||||
schema = Schema(type='integer')
|
||||
param = ApiParameter(
|
||||
original_name='',
|
||||
param_location='',
|
||||
param_schema=schema,
|
||||
)
|
||||
assert param.py_name == 'value'
|
||||
|
||||
def test_api_parameter_custom_py_name(self):
|
||||
schema = Schema(type='integer')
|
||||
param = ApiParameter(
|
||||
|
||||
@@ -164,6 +164,40 @@ def test_process_request_body_no_name():
|
||||
assert parser._params[0].param_location == 'body'
|
||||
|
||||
|
||||
def test_process_request_body_one_of_schema_assigns_name():
|
||||
"""Ensures oneOf bodies result in a named parameter."""
|
||||
operation = Operation(
|
||||
operationId='one_of_request',
|
||||
requestBody=RequestBody(
|
||||
content={
|
||||
'application/json': MediaType(
|
||||
schema=Schema(
|
||||
oneOf=[
|
||||
Schema(
|
||||
type='object',
|
||||
properties={
|
||||
'type': Schema(type='string'),
|
||||
'stage': Schema(type='string'),
|
||||
},
|
||||
)
|
||||
],
|
||||
discriminator={'propertyName': 'type'},
|
||||
)
|
||||
)
|
||||
}
|
||||
),
|
||||
responses={'200': Response(description='ok')},
|
||||
)
|
||||
parser = OperationParser(operation)
|
||||
params = parser.get_parameters()
|
||||
assert len(params) == 1
|
||||
assert params[0].original_name == 'body'
|
||||
assert params[0].py_name == 'body'
|
||||
schema = parser.get_json_schema()
|
||||
assert 'body' in schema['properties']
|
||||
assert '' not in schema['properties']
|
||||
|
||||
|
||||
def test_process_request_body_empty_object():
|
||||
"""Test _process_request_body with a schema that is of type object but with no properties."""
|
||||
operation = Operation(
|
||||
|
||||
Reference in New Issue
Block a user