Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,21 @@ def _collect_operations(
if operation_dict is None:
continue

# Append path-level parameters
operation_dict["parameters"] = operation_dict.get(
"parameters", []
) + path_item.get("parameters", [])
# Append path-level parameters. An operation-level parameter with the
# same name and location overrides the path-level one, so appending
# both would ask the model for the same value twice.
operation_parameters = operation_dict.get("parameters", [])
overridden = {
(parameter["name"], parameter.get("in"))
for parameter in operation_parameters
if isinstance(parameter, dict) and "name" in parameter
}
operation_dict["parameters"] = operation_parameters + [
parameter
for parameter in path_item.get("parameters", [])
if not isinstance(parameter, dict)
or (parameter.get("name"), parameter.get("in")) not in overridden
]

# If operation ID is missing, assign an operation id based on path
# and method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,56 @@ def test_parse_spec_with_path_level_parameters(openapi_spec_generator):
assert local_param.type_value is int


def test_parse_spec_operation_parameter_overrides_path_level_parameter(
openapi_spec_generator,
):
"""A same-named operation parameter replaces the path-level one."""
openapi_spec = {
"openapi": "3.1.0",
"info": {"title": "Accounts API", "version": "1.0.0"},
"paths": {
"/accounts/{accountId}": {
"parameters": [
{
"name": "accountId",
"in": "path",
"required": True,
"schema": {"type": "string"},
"description": "Shared account id",
},
# Same name, different location: not overridden.
{
"name": "accountId",
"in": "header",
"schema": {"type": "string"},
},
],
"get": {
"operationId": "getAccount",
"parameters": [{
"name": "accountId",
"in": "path",
"required": True,
"schema": {"type": "string"},
"description": "Account id, e.g. ACC-123",
}],
"responses": {"200": {"description": "ok"}},
},
}
},
}

operation = openapi_spec_generator.parse(openapi_spec)[0]

assert [
(p.original_name, p.param_location) for p in operation.parameters
] == [
("accountId", "path"),
("accountId", "header"),
]
assert operation.parameters[0].description == "Account id, e.g. ACC-123"


def test_parse_spec_with_invalid_type_any(openapi_spec_generator):
"""Test that schemas with type='Any' are sanitized for Pydantic 2.11+.

Expand Down
Loading