Recognize trace operations in docstrings#1059
Conversation
load_operations_from_docstring filters docstring keys against PATH_KEYS, which omitted "trace". A trace: operation defined in a view docstring was silently dropped, even though OpenAPI v3 (VALID_METHODS_OPENAPI_V3) and the rest of apispec (e.g. resolve_refs_in_path) treat trace as a first-class operation. Add "trace" to PATH_KEYS.
|
Thanks for contributing. Wondering if we should make this v3 only like The non-reg test could be broadened to test all methods. Currently, tests pass if we remove options from the list. Maybe using a parametrized test. |
|
Thanks for the guidance. I pushed What changed:
Verification:
|
|
|
||
|
|
||
| PATH_KEYS = {"get", "put", "post", "delete", "options", "head", "patch"} | ||
| PATH_KEYS = set(VALID_METHODS_OPENAPI_V3) |
There was a problem hiding this comment.
While I understand the benefit of avoiding redundancy, I believe this set just "happens" to be equal to the v3 list.
To be precise, it should be the union of the valid methods for all OpenAPI versions. Frankly, we can keep it as it was defined before. It is not really duplication. And it doesn't change every other day.
| spec.path(path="/path", operations={method: {}}) | ||
| assert list(spec.to_dict()["paths"]["/path"]) == methods | ||
|
|
||
| def test_trace_path_method_is_openapi3_only(self): |
There was a problem hiding this comment.
I wouldn't even test that.
Also, my comment about PATH_KEYS not being adapted to OpenAPI was kinda pointless: we don't really mind since VALID_METHODS is where the validation happens.
| assert yaml_utils.load_operations_from_docstring(docstring) == {} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( |
Summary
load_operations_from_docstringfilters the keys of a view docstring againstPATH_KEYS, which omitted"trace":So a
trace:operation defined in a docstring is silently dropped:This is inconsistent with the rest of apispec, which treats TRACE as a first-class OpenAPI v3 operation:
core.py:VALID_METHODS_OPENAPI_V3 = VALID_METHODS_OPENAPI_V2 + ["trace"]core.py:resolve_refs_in_pathiterates all eight methods, includingtraceOnly
PATH_KEYSinyaml_utils.pywas missed. The same docstring'sget:operation is kept, confirming this is an oversight rather than intent.Fix
Add
"trace"toPATH_KEYS, mirroringVALID_METHODS_OPENAPI_V3.Verification
dev(41784ce):traceis dropped fromload_operations_from_docstring(and end-to-end, absent fromspec.to_dict()["paths"]); with the fix it is preserved.test_load_operations_from_docstring_trace; it fails before the fix and passes after.ruff checkandruff format --checkclean.This pull request was prepared with the assistance of AI, under my direction and review.