From de0b4e7c0eee7741da89fa1714ff85f49230e0c2 Mon Sep 17 00:00:00 2001 From: Darragh Duffy Date: Thu, 1 Dec 2022 16:07:38 +0000 Subject: [PATCH 1/2] example of failing test with vnd.api+json content type --- .../schemas/sample-schemas/content_types.yaml | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/schemas/sample-schemas/content_types.yaml b/tests/schemas/sample-schemas/content_types.yaml index 67172b2e..6588fd20 100644 --- a/tests/schemas/sample-schemas/content_types.yaml +++ b/tests/schemas/sample-schemas/content_types.yaml @@ -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: @@ -62,7 +62,7 @@ paths: '200': description: Successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -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: @@ -101,7 +101,7 @@ paths: '200': description: Successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -137,7 +137,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: type: array items: @@ -174,7 +174,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: type: array items: @@ -209,7 +209,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/Pet' application/xml: @@ -313,7 +313,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/ApiResponse' security: @@ -331,7 +331,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: type: object additionalProperties: @@ -348,7 +348,7 @@ paths: operationId: placeOrder requestBody: content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/Order' application/xml: @@ -361,7 +361,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/Order' '405': @@ -385,7 +385,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/Order' application/xml: @@ -424,7 +424,7 @@ paths: requestBody: description: Created user object content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/User' application/xml: @@ -437,7 +437,7 @@ paths: default: description: successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/User' application/xml: @@ -452,7 +452,7 @@ paths: operationId: createUsersWithListInput requestBody: content: - application/json: + application/vnd.api+json: schema: type: array items: @@ -461,7 +461,7 @@ paths: '200': description: Successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/User' application/xml: @@ -507,7 +507,7 @@ paths: application/xml: schema: type: string - application/json: + application/vnd.api+json: schema: type: string '400': @@ -541,7 +541,7 @@ paths: '200': description: successful operation content: - application/json: + application/vnd.api+json: schema: $ref: '#/components/schemas/User' application/xml: @@ -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: @@ -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: @@ -782,7 +782,7 @@ components: UserArray: description: List of user object content: - application/json: + application/vnd.api+json: schema: type: array items: From 3524c9a3c146613575b23919423484870cb31dc3 Mon Sep 17 00:00:00 2001 From: Darragh Duffy Date: Thu, 1 Dec 2022 16:10:29 +0000 Subject: [PATCH 2/2] fix for vnd.api+json content type using regex --- openapi_tester/schema_tester.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/openapi_tester/schema_tester.py b/openapi_tester/schema_tester.py index 331df48e..89ce17f7 100644 --- a/openapi_tester/schema_tester.py +++ b/openapi_tester/schema_tester.py @@ -1,6 +1,7 @@ """ Schema Tester """ from __future__ import annotations +import re from itertools import chain from typing import TYPE_CHECKING, Any, Callable, cast @@ -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( @@ -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")