Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #290 from snok/pr/288
Browse files Browse the repository at this point in the history
Trailing slash fix
  • Loading branch information
sondrelg authored Dec 1, 2022
2 parents ff24e32 + ba788da commit 07bf094
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
4 changes: 2 additions & 2 deletions openapi_tester/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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
Expand Down
11 changes: 2 additions & 9 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" Schema Tester """
from __future__ import annotations

import re
from itertools import chain
from typing import TYPE_CHECKING, Any, Callable, cast

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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")

Expand Down
68 changes: 34 additions & 34 deletions tests/schemas/sample-schemas/content_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tags:
- name: user
description: Operations about user
paths:
/api/pet/:
/api/pet:
put:
tags:
- pet
Expand All @@ -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:
Expand All @@ -62,7 +62,7 @@ paths:
'200':
description: Successful operation
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand All @@ -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:
Expand All @@ -101,7 +101,7 @@ paths:
'200':
description: Successful operation
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand All @@ -113,7 +113,7 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/api/pet/findByStatus/:
/api/pet/findByStatus:
get:
tags:
- pet
Expand All @@ -137,7 +137,7 @@ paths:
'200':
description: successful operation
content:
application/vnd.api+json:
application/json:
schema:
type: array
items:
Expand All @@ -153,7 +153,7 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/api/pet/findByTags/:
/api/pet/findByTags:
get:
tags:
- pet
Expand All @@ -174,7 +174,7 @@ paths:
'200':
description: successful operation
content:
application/vnd.api+json:
application/json:
schema:
type: array
items:
Expand All @@ -190,7 +190,7 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/api/pet/{petId}/:
/api/pet/{petId}:
get:
tags:
- pet
Expand All @@ -209,7 +209,7 @@ paths:
'200':
description: successful operation
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand Down Expand Up @@ -282,7 +282,7 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/api/pet/{petId}/uploadImage/:
/api/pet/{petId}/uploadImage:
post:
tags:
- pet
Expand Down Expand Up @@ -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
Expand All @@ -331,15 +331,15 @@ paths:
'200':
description: successful operation
content:
application/vnd.api+json:
application/json:
schema:
type: object
additionalProperties:
type: integer
format: int32
security:
- api_key: []
/api/store/order/:
/api/store/order:
post:
tags:
- store
Expand All @@ -348,7 +348,7 @@ paths:
operationId: placeOrder
requestBody:
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/Order'
application/xml:
Expand All @@ -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
Expand All @@ -385,7 +385,7 @@ paths:
'200':
description: successful operation
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/Order'
application/xml:
Expand Down Expand Up @@ -414,7 +414,7 @@ paths:
description: Invalid ID supplied
'404':
description: Order not found
/api/user/:
/api/user:
post:
tags:
- user
Expand All @@ -424,7 +424,7 @@ paths:
requestBody:
description: Created user object
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/User'
application/xml:
Expand All @@ -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
Expand All @@ -452,7 +452,7 @@ paths:
operationId: createUsersWithListInput
requestBody:
content:
application/vnd.api+json:
application/json:
schema:
type: array
items:
Expand All @@ -461,15 +461,15 @@ paths:
'200':
description: Successful operation
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/User'
application/xml:
schema:
$ref: '#/components/schemas/User'
default:
description: successful operation
/api/user/login/:
/api/user/login:
get:
tags:
- user
Expand Down Expand Up @@ -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
Expand All @@ -523,7 +523,7 @@ paths:
responses:
default:
description: successful operation
/api/user/{username}/:
/api/user/{username}:
get:
tags:
- user
Expand All @@ -541,7 +541,7 @@ paths:
'200':
description: successful operation
content:
application/vnd.api+json:
application/json:
schema:
$ref: '#/components/schemas/User'
application/xml:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -782,7 +782,7 @@ components:
UserArray:
description: List of user object
content:
application/vnd.api+json:
application/json:
schema:
type: array
items:
Expand Down
3 changes: 0 additions & 3 deletions tests/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 07bf094

Please sign in to comment.