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 #292 from darduf/support-alt-application-json-types
Browse files Browse the repository at this point in the history
fix: failing test with vnd.api+json content type
  • Loading branch information
sondrelg authored Dec 2, 2022
2 parents 98ed1f6 + 3524c9a commit b20460e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
11 changes: 9 additions & 2 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Schema Tester """
from __future__ import annotations

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

Expand Down Expand Up @@ -89,11 +90,16 @@ def __init__(
raise ImproperlyConfigured(INIT_ERROR)

@staticmethod
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "") -> dict:
def get_key_value(schema: dict[str, dict], key: str, error_addon: str = "", use_regex=False) -> 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 @@ -168,9 +174,10 @@ def get_response_schema_section(self, response: Response) -> dict[str, Any]:
)
json_object = self.get_key_value(
content_object,
"application/json",
r"^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
42 changes: 21 additions & 21 deletions tests/schemas/sample-schemas/content_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ paths:
requestBody:
description: Update an existent pet in the store
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand All @@ -62,7 +62,7 @@ paths:
'200':
description: Successful operation
content:
application/json:
application/vnd.api+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/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand All @@ -101,7 +101,7 @@ paths:
'200':
description: Successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand Down Expand Up @@ -137,7 +137,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
type: array
items:
Expand Down Expand Up @@ -174,7 +174,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
type: array
items:
Expand Down Expand Up @@ -209,7 +209,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand Down Expand Up @@ -313,7 +313,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/ApiResponse'
security:
Expand All @@ -331,7 +331,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
type: object
additionalProperties:
Expand All @@ -348,7 +348,7 @@ paths:
operationId: placeOrder
requestBody:
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Order'
application/xml:
Expand All @@ -361,7 +361,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Order'
'405':
Expand All @@ -385,7 +385,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Order'
application/xml:
Expand Down Expand Up @@ -424,7 +424,7 @@ paths:
requestBody:
description: Created user object
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/User'
application/xml:
Expand All @@ -437,7 +437,7 @@ paths:
default:
description: successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/User'
application/xml:
Expand All @@ -452,7 +452,7 @@ paths:
operationId: createUsersWithListInput
requestBody:
content:
application/json:
application/vnd.api+json:
schema:
type: array
items:
Expand All @@ -461,7 +461,7 @@ paths:
'200':
description: Successful operation
content:
application/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/User'
application/xml:
Expand Down Expand Up @@ -507,7 +507,7 @@ paths:
application/xml:
schema:
type: string
application/json:
application/vnd.api+json:
schema:
type: string
'400':
Expand Down Expand Up @@ -541,7 +541,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/vnd.api+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/json:
application/vnd.api+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/json:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/Pet'
application/xml:
Expand All @@ -782,7 +782,7 @@ components:
UserArray:
description: List of user object
content:
application/json:
application/vnd.api+json:
schema:
type: array
items:
Expand Down

0 comments on commit b20460e

Please sign in to comment.