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:
George Weale
2025-11-20 11:48:46 -08:00
committed by Copybara-Service
parent bf8b85da52
commit 084c2de0da
4 changed files with 76 additions and 7 deletions
@@ -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(