diff --git a/openapi_tester/loaders.py b/openapi_tester/loaders.py index 622a4636..78b3d148 100644 --- a/openapi_tester/loaders.py +++ b/openapi_tester/loaders.py @@ -143,7 +143,7 @@ def resolve_path(self, endpoint_path: str, method: str) -> tuple[str, ResolverMa Resolves a Django path. """ url_object = urlparse(endpoint_path) - parsed_path = url_object.path if url_object.path.endswith("/") else url_object.path + "/" + parsed_path = url_object.path if not parsed_path.startswith("/"): parsed_path = "/" + parsed_path for key, value in self.field_key_map.items(): @@ -166,7 +166,7 @@ def resolve_path(self, endpoint_path: str, method: str) -> tuple[str, ResolverMa message = f"Could not resolve path `{endpoint_path}`." close_matches = difflib.get_close_matches(endpoint_path, self.endpoints) if close_matches: - message += "\n\nDid you mean one of these?" + "\n- ".join(close_matches) + message += "\n\nDid you mean one of these?\n\n- " + "\n- ".join(close_matches) raise ValueError(message) @staticmethod diff --git a/openapi_tester/schema_tester.py b/openapi_tester/schema_tester.py index 89ce17f7..331df48e 100644 --- a/openapi_tester/schema_tester.py +++ b/openapi_tester/schema_tester.py @@ -1,7 +1,6 @@ """ Schema Tester """ from __future__ import annotations -import re from itertools import chain from typing import TYPE_CHECKING, Any, Callable, cast @@ -90,16 +89,11 @@ def __init__( raise ImproperlyConfigured(INIT_ERROR) @staticmethod - def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "", use_regex=False) -> dict: + def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "") -> dict: """ Returns the value of a given key """ try: - if use_regex: - compiled_pattern = re.compile(key) - for key_ in schema.keys(): - if compiled_pattern.match(key_): - return schema[key_] return schema[key] except KeyError as e: raise UndocumentedSchemaSectionError( @@ -174,10 +168,9 @@ def get_response_schema_section(self, response: Response) -> dict[str, Any]: ) json_object = self.get_key_value( content_object, - r"^application\/.*json$", + "application/json", "\n\nNo `application/json` responses documented for method: " f"{response_method}, path: {parameterized_path}", - use_regex=True, ) return self.get_key_value(json_object, "schema") diff --git a/tests/schemas/sample-schemas/content_types.yaml b/tests/schemas/sample-schemas/content_types.yaml index b0dd696d..67172b2e 100644 --- a/tests/schemas/sample-schemas/content_types.yaml +++ b/tests/schemas/sample-schemas/content_types.yaml @@ -38,7 +38,7 @@ tags: - name: user description: Operations about user paths: - /api/pet/: + /api/pet: put: tags: - pet @@ -48,7 +48,7 @@ paths: requestBody: description: Update an existent pet in the store content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -62,7 +62,7 @@ paths: '200': description: Successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -87,7 +87,7 @@ paths: requestBody: description: Create a new pet in the store content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -101,7 +101,7 @@ paths: '200': description: Successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -113,7 +113,7 @@ paths: - petstore_auth: - write:pets - read:pets - /api/pet/findByStatus/: + /api/pet/findByStatus: get: tags: - pet @@ -137,7 +137,7 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: type: array items: @@ -153,7 +153,7 @@ paths: - petstore_auth: - write:pets - read:pets - /api/pet/findByTags/: + /api/pet/findByTags: get: tags: - pet @@ -174,7 +174,7 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: type: array items: @@ -190,7 +190,7 @@ paths: - petstore_auth: - write:pets - read:pets - /api/pet/{petId}/: + /api/pet/{petId}: get: tags: - pet @@ -209,7 +209,7 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -282,7 +282,7 @@ paths: - petstore_auth: - write:pets - read:pets - /api/pet/{petId}/uploadImage/: + /api/pet/{petId}/uploadImage: post: tags: - pet @@ -313,14 +313,14 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/ApiResponse' security: - petstore_auth: - write:pets - read:pets - /api/store/inventory/: + /api/store/inventory: get: tags: - store @@ -331,7 +331,7 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: type: object additionalProperties: @@ -339,7 +339,7 @@ paths: format: int32 security: - api_key: [] - /api/store/order/: + /api/store/order: post: tags: - store @@ -348,7 +348,7 @@ paths: operationId: placeOrder requestBody: content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Order' application/xml: @@ -361,12 +361,12 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Order' '405': description: Invalid input - /api/store/order/{orderId}/: + /api/store/order/{orderId}: get: tags: - store @@ -385,7 +385,7 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Order' application/xml: @@ -414,7 +414,7 @@ paths: description: Invalid ID supplied '404': description: Order not found - /api/user/: + /api/user: post: tags: - user @@ -424,7 +424,7 @@ paths: requestBody: description: Created user object content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/User' application/xml: @@ -437,13 +437,13 @@ paths: default: description: successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/User' application/xml: schema: $ref: '#/components/schemas/User' - /api/user/createWithList/: + /api/user/createWithList: post: tags: - user @@ -452,7 +452,7 @@ paths: operationId: createUsersWithListInput requestBody: content: - application/vnd.api+json: + application/json: schema: type: array items: @@ -461,7 +461,7 @@ paths: '200': description: Successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/User' application/xml: @@ -469,7 +469,7 @@ paths: $ref: '#/components/schemas/User' default: description: successful operation - /api/user/login/: + /api/user/login: get: tags: - user @@ -507,12 +507,12 @@ paths: application/xml: schema: type: string - application/vnd.api+json: + application/json: schema: type: string '400': description: Invalid username/password supplied - /api/user/logout/: + /api/user/logout: get: tags: - user @@ -523,7 +523,7 @@ paths: responses: default: description: successful operation - /api/user/{username}/: + /api/user/{username}: get: tags: - user @@ -541,7 +541,7 @@ paths: '200': description: successful operation content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/User' application/xml: @@ -567,7 +567,7 @@ paths: requestBody: description: Update an existent user in the store content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/User' application/xml: @@ -773,7 +773,7 @@ components: Pet: description: Pet object that needs to be added to the store content: - application/vnd.api+json: + application/json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -782,7 +782,7 @@ components: UserArray: description: List of user object content: - application/vnd.api+json: + application/json: schema: type: array items: diff --git a/tests/test_loaders.py b/tests/test_loaders.py index 17eda4c9..4627352f 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -49,13 +49,10 @@ def test_url_schema_loader(): @pytest.mark.parametrize("loader", loaders) def test_loader_get_route(loader): assert loader.resolve_path("/api/v1/items/", "get")[0] == "/api/{version}/items" - assert loader.resolve_path("/api/v1/items", "get")[0] == "/api/{version}/items" assert loader.resolve_path("api/v1/items/", "get")[0] == "/api/{version}/items" assert loader.resolve_path("api/v1/items", "get")[0] == "/api/{version}/items" assert loader.resolve_path("/api/v1/snake-case/", "get")[0] == "/api/{version}/snake-case/" - assert loader.resolve_path("/api/v1/snake-case", "get")[0] == "/api/{version}/snake-case/" assert loader.resolve_path("api/v1/snake-case/", "get")[0] == "/api/{version}/snake-case/" - assert loader.resolve_path("api/v1/snake-case", "get")[0] == "/api/{version}/snake-case/" with pytest.raises(ValueError, match="Could not resolve path `test`"): assert loader.resolve_path("test", "get")