Skip to content

fix: support path level parameters for open_api_spec_parser #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -108,6 +108,12 @@ def _collect_operations(
operation_dict = path_item.get(method)
if operation_dict is None:
continue

# Append path-level parameters
operation_dict['parameters'] = (
operation_dict.get("parameters", [])
+ path_item.get("parameters", [])
)

# 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 @@ -626,3 +626,51 @@ def test_parse_spec_with_duplicate_parameter_names(openapi_spec_generator):
assert body_param is not None
assert body_param.original_name == "name"
assert body_param.py_name == "name_0"


def test_parse_spec_with_path_level_parameters(openapi_spec_generator):
"""Test that operation parameters are correctly combined with path-level parameters."""
openapi_spec = {
"openapi": "3.1.0",
"info": {"title": "Combine Parameters API", "version": "1.0.0"},
"paths": {
"/test": {
"parameters": [
{"name": "global_param", "in": "query", "schema": {"type": "string"}}
],
"get": {
"parameters": [
{"name": "local_param", "in": "header", "schema": {"type": "integer"}}
],
"operationId": "testGet",
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {"schema": {"type": "string"}}
},
}
},
},
}
},
}

parsed_operations = openapi_spec_generator.parse(openapi_spec)
assert len(parsed_operations) == 1

operation = parsed_operations[0]
assert len(operation.parameters) == 2

# Verify the combined parameters
global_param = next((p for p in operation.parameters if p.original_name == "global_param"), None)
local_param = next((p for p in operation.parameters if p.original_name == "local_param"), None)

assert global_param is not None
assert global_param.param_location == "query"
assert global_param.type_value is str

assert local_param is not None
assert local_param.param_location == "header"
assert local_param.type_value is int

Loading