File: validator12.py

package info (click to toggle)
swagger-spec-validator 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 696 kB
  • sloc: python: 2,321; makefile: 28; sh: 2
file content (279 lines) | stat: -rw-r--r-- 9,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Validate Swagger Specs against the Swagger 1.2 Specification.  The
validator aims to check for full compliance with the Specification.

The validator uses the published jsonschema files for basic structural
validation, augmented with custom validation code where necessary.

https://github.com/swagger-api/swagger-spec/blob/master/versions/1.2.md
"""
from __future__ import annotations

import logging
import os
from typing import Any
from urllib.parse import urlparse

import jsonschema
from jsonschema import RefResolver

from swagger_spec_validator.common import get_uri_from_file_path
from swagger_spec_validator.common import read_resource_file
from swagger_spec_validator.common import read_url
from swagger_spec_validator.common import SwaggerValidationError
from swagger_spec_validator.common import wrap_exception
from swagger_spec_validator.ref_validators import default_handlers

log = logging.getLogger(__name__)

# Primitives (§4.3.1)
PRIMITIVE_TYPES = ["integer", "number", "string", "boolean"]


def get_model_ids(api_declaration: dict[str, Any]) -> list[str]:
    models = api_declaration.get("models", {})
    return [model["id"] for model in models.values()]


def get_resource_path(url: str, resource: str) -> str:
    """Fetch the complete resource path to get the api declaration.

    :param url: A file or http uri hosting the resource listing.
    :type url: string
    :param resource: Resource path starting with a '/'. eg. '/pet'
    :type resource: string
    :returns: Complete resource path hosting the api declaration.
    """
    if urlparse(url).scheme == "file":
        parent_dir = os.path.dirname(url)

        def resource_file_name(resource: str) -> str:
            assert resource.startswith("/")
            return resource[1:] + ".json"

        path = os.path.join(parent_dir, resource_file_name(resource))
    else:
        path = url + resource

    return path


@wrap_exception
def validate_spec_url(url: str) -> None:
    """Simple utility function to perform recursive validation of a Resource
    Listing and all associated API Declarations.

    This is trivial wrapper function around
    :py:func:`swagger_spec_validator.validate_resource_listing` and
    :py:func:`swagger_spec_validator.validate_api_declaration`.  You are
    encouraged to write your own version of this if required.

    :param url: the URL of the Resource Listing.

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    """

    log.info("Validating %s", url)
    validate_spec(read_url(url), url)


def validate_spec(resource_listing: dict[str, Any], url: str) -> None:
    """
    Validates the resource listing, fetches the api declarations and
    consequently validates them as well.

    :type resource_listing: dict
    :param url: url serving the resource listing; needed to resolve api
                declaration path.
    :type url: string

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    """
    validate_resource_listing(resource_listing)

    for api in resource_listing["apis"]:
        path = get_resource_path(url, api["path"])
        log.info("Validating %s", path)
        validate_api_declaration(read_url(path))


def validate_data_type(
    obj: dict[str, Any],
    model_ids: list[str],
    allow_arrays: bool = True,
    allow_voids: bool = False,
    allow_refs: bool = True,
    allow_file: bool = False,
) -> None:
    """Validate an object that contains a data type (§4.3.3).

    Params:
    - obj: the dictionary containing the data type to validate
    - model_ids: a list of model ids
    - allow_arrays: whether an array is permitted in the data type.  This is
      used to prevent nested arrays.
    - allow_voids: whether a void type is permitted.  This is used when
      validating Operation Objects (§5.2.3).
    - allow_refs: whether '$ref's are permitted.  If true, then 'type's
      are not allowed to reference model IDs.
    """

    typ = obj.get("type")
    ref = obj.get("$ref")

    # TODO Use a custom jsonschema.Validator to Validate defaultValue
    # enum, minimum, maximum, uniqueItems
    if typ is not None:
        if typ in PRIMITIVE_TYPES:
            return
        if allow_voids and typ == "void":
            return
        if typ == "array":
            if not allow_arrays:
                raise SwaggerValidationError('"array" not allowed')
            # Items Object (§4.3.4)
            items = obj.get("items")
            if items is None:
                raise SwaggerValidationError('"items" not found')
            validate_data_type(items, model_ids, allow_arrays=False)
            return
        if typ == "File":
            if not allow_file:
                raise SwaggerValidationError(
                    'Type "File" is only valid for form parameters'
                )
            return
        if typ in model_ids:
            if allow_refs:
                raise SwaggerValidationError(
                    'must use "$ref" for referencing "%s"' % typ
                )
            return
        raise SwaggerValidationError('unknown type "%s"' % typ)

    if ref is not None:
        if not allow_refs:
            raise SwaggerValidationError('"$ref" not allowed')
        if ref not in model_ids:
            raise SwaggerValidationError('unknown model id "%s"' % ref)
        return

    raise SwaggerValidationError('no "$ref" or "type" present')


def validate_model(
    model: dict[str, Any], model_name: str, model_ids: list[str]
) -> None:
    """Validate a Model Object (§5.2.7)."""
    # TODO Validate 'sub-types' and 'discriminator' fields
    for required in model.get("required", []):
        if required not in model["properties"]:
            raise SwaggerValidationError(
                'Model "{}": required property "{}" not found'.format(
                    model_name, required
                )
            )

    if model_name != model["id"]:
        error = "model name: {} does not match model id: {}".format(
            model_name, model["id"]
        )
        raise SwaggerValidationError(error)

    for prop_name, prop in model.get("properties", {}).items():
        try:
            validate_data_type(prop, model_ids, allow_refs=True)
        except SwaggerValidationError as e:
            # Add more context to the exception and re-raise
            raise SwaggerValidationError(
                f'Model "{model_name}", property "{prop_name}": {str(e)}'
            )


def validate_parameter(parameter: dict[str, Any], model_ids: list[str]) -> None:
    """Validate a Parameter Object (§5.2.4)."""
    allow_file = parameter.get("paramType") == "form"
    validate_data_type(parameter, model_ids, allow_refs=False, allow_file=allow_file)


def validate_operation(operation: dict[str, Any], model_ids: list[str]) -> None:
    """Validate an Operation Object (§5.2.3)."""
    try:
        validate_data_type(operation, model_ids, allow_refs=False, allow_voids=True)
    except SwaggerValidationError as e:
        raise SwaggerValidationError(
            'Operation "{}": {}'.format(operation["nickname"], str(e))
        )

    for parameter in operation["parameters"]:
        try:
            validate_parameter(parameter, model_ids)
        except SwaggerValidationError as e:
            raise SwaggerValidationError(
                'Operation "%s", parameter "%s": %s'
                % (operation["nickname"], parameter["name"], str(e))
            )


def validate_api(api: dict[str, Any], model_ids: list[str]) -> None:
    """Validate an API Object (§5.2.2)."""
    for operation in api["operations"]:
        validate_operation(operation, model_ids)


def validate_api_declaration(api_declaration: dict[str, Any]) -> None:
    """Validate an API Declaration (§5.2).

    :param api_declaration: a dictionary respresentation of an API Declaration.

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    :raises: :py:class:`jsonschema.exceptions.ValidationError`
    """
    validate_json(api_declaration, "schemas/v1.2/apiDeclaration.json")

    model_ids = get_model_ids(api_declaration)

    for api in api_declaration["apis"]:
        validate_api(api, model_ids)

    for model_name, model in api_declaration.get("models", {}).items():
        validate_model(model, model_name, model_ids)


def validate_resource_listing(resource_listing: dict[str, Any]) -> None:
    """Validate a Resource Listing (§5.1).

    :param resource_listing: a dictionary respresentation of a Resource Listing.

    Note that you will have to invoke `validate_api_declaration` on each
    linked API Declaration.

    :returns: `None` in case of success, otherwise raises an exception.

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    :raises: :py:class:`jsonschema.exceptions.ValidationError`
    """
    validate_json(resource_listing, "schemas/v1.2/resourceListing.json")


@wrap_exception
def validate_json(json_document: list[Any] | dict[str, Any], schema_path: str) -> None:
    """Validate a json document against a json schema.

    :param json_document: json document in the form of a list or dict.
    :param schema_path: package relative path of the json schema file.
    """
    schema, schema_path = read_resource_file(schema_path)

    resolver = RefResolver(
        base_uri=get_uri_from_file_path(schema_path),
        referrer=schema,
        handlers=default_handlers,
    )
    jsonschema.validate(json_document, schema, resolver=resolver)