Trailing slash in parametrized path
This branch is based on https://github.com/snok/drf-openapi-tester/pull/284 that has the required drf setup to show where this is failing.
I have removed the trailing slash on paths in /drf-openapi-tester/tests/schemas/sample-schemas/content_types.yaml to represent our OAS defined paths.
This reproduces the error we are seeing, where the Undocumented route parametrized path has a trailing slash:
schema = {'/api/pet': {'post': {'description': 'Add a new pet to the store', 'operationId': 'addPet', 'requestBody': {'content'..., 'name': 'status', 'schema': {'type': 'string'}}], 'responses': {'405': {'description': 'Invalid input'}}, ...}}, ...}
key = '/api/pet/{petId}/'
error_addon = '\n\nUndocumented route /api/pet/{petId}/.\n\nDocumented routes: /api/pet\n\t• /api/pet/findByStatus\n\t• /api/pet/fin...rId}\n\t• /api/user\n\t• /api/user/createWithList\n\t• /api/user/login\n\t• /api/user/logout\n\t• /api/user/{username}'
@staticmethod
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "") -> dict:
"""
Returns the value of a given key
"""
try:
return schema[key]
except KeyError as e:
> raise UndocumentedSchemaSectionError(
UNDOCUMENTED_SCHEMA_SECTION_ERROR.format(key=key, error_addon=error_addon)
) from e
E openapi_tester.exceptions.UndocumentedSchemaSectionError: Error: Unsuccessfully tried to index the OpenAPI schema by `/api/pet/{petId}/`.
E
E Undocumented route /api/pet/{petId}/.
E
E Documented routes: /api/pet
E • /api/pet/findByStatus
E • /api/pet/findByTags
E • /api/pet/{petId}
E • /api/pet/{petId}/uploadImage
E • /api/store/inventory
E • /api/store/order
E • /api/store/order/{orderId}
E • /api/user
E • /api/user/createWithList
E • /api/user/login
E • /api/user/logout
E • /api/user/{username}
openapi_tester/schema_tester.py:99: UndocumentedSchemaSectionError
Removing + "/" from this line of code solves the issue for us, and is part of the discussion from this issue:
File: third_party/drf-openapi-tester/openapi_tester/loaders.py
146: parsed_path = url_object.path if url_object.path.endswith("/") else url_object.path + "/"
Codecov Report
Merging #288 (ba788da) into master (1929ad3) will not change coverage. The diff coverage is
100.0%.
:exclamation: Current head ba788da differs from pull request most recent head 18eaf97. Consider uploading reports for the commit 18eaf97 to get more accurate results
@@ Coverage Diff @@
## master #288 +/- ##
======================================
Coverage 98.7% 98.7%
======================================
Files 7 7
Lines 474 474
Branches 77 77
======================================
Hits 468 468
Misses 4 4
Partials 2 2
| Impacted Files | Coverage Δ | |
|---|---|---|
| openapi_tester/loaders.py | 95.9% <100.0%> (ø) |