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

Commit 07bf094

Browse files
authored
Merge pull request #290 from snok/pr/288
Trailing slash fix
2 parents ff24e32 + ba788da commit 07bf094

File tree

4 files changed

+38
-48
lines changed

4 files changed

+38
-48
lines changed

openapi_tester/loaders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def resolve_path(self, endpoint_path: str, method: str) -> tuple[str, ResolverMa
143143
Resolves a Django path.
144144
"""
145145
url_object = urlparse(endpoint_path)
146-
parsed_path = url_object.path if url_object.path.endswith("/") else url_object.path + "/"
146+
parsed_path = url_object.path
147147
if not parsed_path.startswith("/"):
148148
parsed_path = "/" + parsed_path
149149
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
166166
message = f"Could not resolve path `{endpoint_path}`."
167167
close_matches = difflib.get_close_matches(endpoint_path, self.endpoints)
168168
if close_matches:
169-
message += "\n\nDid you mean one of these?" + "\n- ".join(close_matches)
169+
message += "\n\nDid you mean one of these?\n\n- " + "\n- ".join(close_matches)
170170
raise ValueError(message)
171171

172172
@staticmethod

openapi_tester/schema_tester.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
""" Schema Tester """
22
from __future__ import annotations
33

4-
import re
54
from itertools import chain
65
from typing import TYPE_CHECKING, Any, Callable, cast
76

@@ -90,16 +89,11 @@ def __init__(
9089
raise ImproperlyConfigured(INIT_ERROR)
9190

9291
@staticmethod
93-
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "", use_regex=False) -> dict:
92+
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "") -> dict:
9493
"""
9594
Returns the value of a given key
9695
"""
9796
try:
98-
if use_regex:
99-
compiled_pattern = re.compile(key)
100-
for key_ in schema.keys():
101-
if compiled_pattern.match(key_):
102-
return schema[key_]
10397
return schema[key]
10498
except KeyError as e:
10599
raise UndocumentedSchemaSectionError(
@@ -174,10 +168,9 @@ def get_response_schema_section(self, response: Response) -> dict[str, Any]:
174168
)
175169
json_object = self.get_key_value(
176170
content_object,
177-
r"^application\/.*json$",
171+
"application/json",
178172
"\n\nNo `application/json` responses documented for method: "
179173
f"{response_method}, path: {parameterized_path}",
180-
use_regex=True,
181174
)
182175
return self.get_key_value(json_object, "schema")
183176

tests/schemas/sample-schemas/content_types.yaml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tags:
3838
- name: user
3939
description: Operations about user
4040
paths:
41-
/api/pet/:
41+
/api/pet:
4242
put:
4343
tags:
4444
- pet
@@ -48,7 +48,7 @@ paths:
4848
requestBody:
4949
description: Update an existent pet in the store
5050
content:
51-
application/vnd.api+json:
51+
application/json:
5252
schema:
5353
$ref: '#/components/schemas/Pet'
5454
application/xml:
@@ -62,7 +62,7 @@ paths:
6262
'200':
6363
description: Successful operation
6464
content:
65-
application/vnd.api+json:
65+
application/json:
6666
schema:
6767
$ref: '#/components/schemas/Pet'
6868
application/xml:
@@ -87,7 +87,7 @@ paths:
8787
requestBody:
8888
description: Create a new pet in the store
8989
content:
90-
application/vnd.api+json:
90+
application/json:
9191
schema:
9292
$ref: '#/components/schemas/Pet'
9393
application/xml:
@@ -101,7 +101,7 @@ paths:
101101
'200':
102102
description: Successful operation
103103
content:
104-
application/vnd.api+json:
104+
application/json:
105105
schema:
106106
$ref: '#/components/schemas/Pet'
107107
application/xml:
@@ -113,7 +113,7 @@ paths:
113113
- petstore_auth:
114114
- write:pets
115115
- read:pets
116-
/api/pet/findByStatus/:
116+
/api/pet/findByStatus:
117117
get:
118118
tags:
119119
- pet
@@ -137,7 +137,7 @@ paths:
137137
'200':
138138
description: successful operation
139139
content:
140-
application/vnd.api+json:
140+
application/json:
141141
schema:
142142
type: array
143143
items:
@@ -153,7 +153,7 @@ paths:
153153
- petstore_auth:
154154
- write:pets
155155
- read:pets
156-
/api/pet/findByTags/:
156+
/api/pet/findByTags:
157157
get:
158158
tags:
159159
- pet
@@ -174,7 +174,7 @@ paths:
174174
'200':
175175
description: successful operation
176176
content:
177-
application/vnd.api+json:
177+
application/json:
178178
schema:
179179
type: array
180180
items:
@@ -190,7 +190,7 @@ paths:
190190
- petstore_auth:
191191
- write:pets
192192
- read:pets
193-
/api/pet/{petId}/:
193+
/api/pet/{petId}:
194194
get:
195195
tags:
196196
- pet
@@ -209,7 +209,7 @@ paths:
209209
'200':
210210
description: successful operation
211211
content:
212-
application/vnd.api+json:
212+
application/json:
213213
schema:
214214
$ref: '#/components/schemas/Pet'
215215
application/xml:
@@ -282,7 +282,7 @@ paths:
282282
- petstore_auth:
283283
- write:pets
284284
- read:pets
285-
/api/pet/{petId}/uploadImage/:
285+
/api/pet/{petId}/uploadImage:
286286
post:
287287
tags:
288288
- pet
@@ -313,14 +313,14 @@ paths:
313313
'200':
314314
description: successful operation
315315
content:
316-
application/vnd.api+json:
316+
application/json:
317317
schema:
318318
$ref: '#/components/schemas/ApiResponse'
319319
security:
320320
- petstore_auth:
321321
- write:pets
322322
- read:pets
323-
/api/store/inventory/:
323+
/api/store/inventory:
324324
get:
325325
tags:
326326
- store
@@ -331,15 +331,15 @@ paths:
331331
'200':
332332
description: successful operation
333333
content:
334-
application/vnd.api+json:
334+
application/json:
335335
schema:
336336
type: object
337337
additionalProperties:
338338
type: integer
339339
format: int32
340340
security:
341341
- api_key: []
342-
/api/store/order/:
342+
/api/store/order:
343343
post:
344344
tags:
345345
- store
@@ -348,7 +348,7 @@ paths:
348348
operationId: placeOrder
349349
requestBody:
350350
content:
351-
application/vnd.api+json:
351+
application/json:
352352
schema:
353353
$ref: '#/components/schemas/Order'
354354
application/xml:
@@ -361,12 +361,12 @@ paths:
361361
'200':
362362
description: successful operation
363363
content:
364-
application/vnd.api+json:
364+
application/json:
365365
schema:
366366
$ref: '#/components/schemas/Order'
367367
'405':
368368
description: Invalid input
369-
/api/store/order/{orderId}/:
369+
/api/store/order/{orderId}:
370370
get:
371371
tags:
372372
- store
@@ -385,7 +385,7 @@ paths:
385385
'200':
386386
description: successful operation
387387
content:
388-
application/vnd.api+json:
388+
application/json:
389389
schema:
390390
$ref: '#/components/schemas/Order'
391391
application/xml:
@@ -414,7 +414,7 @@ paths:
414414
description: Invalid ID supplied
415415
'404':
416416
description: Order not found
417-
/api/user/:
417+
/api/user:
418418
post:
419419
tags:
420420
- user
@@ -424,7 +424,7 @@ paths:
424424
requestBody:
425425
description: Created user object
426426
content:
427-
application/vnd.api+json:
427+
application/json:
428428
schema:
429429
$ref: '#/components/schemas/User'
430430
application/xml:
@@ -437,13 +437,13 @@ paths:
437437
default:
438438
description: successful operation
439439
content:
440-
application/vnd.api+json:
440+
application/json:
441441
schema:
442442
$ref: '#/components/schemas/User'
443443
application/xml:
444444
schema:
445445
$ref: '#/components/schemas/User'
446-
/api/user/createWithList/:
446+
/api/user/createWithList:
447447
post:
448448
tags:
449449
- user
@@ -452,7 +452,7 @@ paths:
452452
operationId: createUsersWithListInput
453453
requestBody:
454454
content:
455-
application/vnd.api+json:
455+
application/json:
456456
schema:
457457
type: array
458458
items:
@@ -461,15 +461,15 @@ paths:
461461
'200':
462462
description: Successful operation
463463
content:
464-
application/vnd.api+json:
464+
application/json:
465465
schema:
466466
$ref: '#/components/schemas/User'
467467
application/xml:
468468
schema:
469469
$ref: '#/components/schemas/User'
470470
default:
471471
description: successful operation
472-
/api/user/login/:
472+
/api/user/login:
473473
get:
474474
tags:
475475
- user
@@ -507,12 +507,12 @@ paths:
507507
application/xml:
508508
schema:
509509
type: string
510-
application/vnd.api+json:
510+
application/json:
511511
schema:
512512
type: string
513513
'400':
514514
description: Invalid username/password supplied
515-
/api/user/logout/:
515+
/api/user/logout:
516516
get:
517517
tags:
518518
- user
@@ -523,7 +523,7 @@ paths:
523523
responses:
524524
default:
525525
description: successful operation
526-
/api/user/{username}/:
526+
/api/user/{username}:
527527
get:
528528
tags:
529529
- user
@@ -541,7 +541,7 @@ paths:
541541
'200':
542542
description: successful operation
543543
content:
544-
application/vnd.api+json:
544+
application/json:
545545
schema:
546546
$ref: '#/components/schemas/User'
547547
application/xml:
@@ -567,7 +567,7 @@ paths:
567567
requestBody:
568568
description: Update an existent user in the store
569569
content:
570-
application/vnd.api+json:
570+
application/json:
571571
schema:
572572
$ref: '#/components/schemas/User'
573573
application/xml:
@@ -773,7 +773,7 @@ components:
773773
Pet:
774774
description: Pet object that needs to be added to the store
775775
content:
776-
application/vnd.api+json:
776+
application/json:
777777
schema:
778778
$ref: '#/components/schemas/Pet'
779779
application/xml:
@@ -782,7 +782,7 @@ components:
782782
UserArray:
783783
description: List of user object
784784
content:
785-
application/vnd.api+json:
785+
application/json:
786786
schema:
787787
type: array
788788
items:

tests/test_loaders.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,10 @@ def test_url_schema_loader():
4949
@pytest.mark.parametrize("loader", loaders)
5050
def test_loader_get_route(loader):
5151
assert loader.resolve_path("/api/v1/items/", "get")[0] == "/api/{version}/items"
52-
assert loader.resolve_path("/api/v1/items", "get")[0] == "/api/{version}/items"
5352
assert loader.resolve_path("api/v1/items/", "get")[0] == "/api/{version}/items"
5453
assert loader.resolve_path("api/v1/items", "get")[0] == "/api/{version}/items"
5554
assert loader.resolve_path("/api/v1/snake-case/", "get")[0] == "/api/{version}/snake-case/"
56-
assert loader.resolve_path("/api/v1/snake-case", "get")[0] == "/api/{version}/snake-case/"
5755
assert loader.resolve_path("api/v1/snake-case/", "get")[0] == "/api/{version}/snake-case/"
58-
assert loader.resolve_path("api/v1/snake-case", "get")[0] == "/api/{version}/snake-case/"
5956
with pytest.raises(ValueError, match="Could not resolve path `test`"):
6057
assert loader.resolve_path("test", "get")
6158

0 commit comments

Comments
 (0)