~singpolyma/asterisk

ref: upstream/16.2.1_dfsg asterisk/rest-api-templates/swagger_model.py -rw-r--r-- 25.5 KiB
fe6d69c0Stephen Paul Weber import-tar 7 months ago
                                                                                
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# Asterisk -- An open source telephony toolkit.
#
# Copyright (C) 2013, Digium, Inc.
#
# David M. Lee, II <dlee@digium.com>
#
# See http://www.asterisk.org for more information about
# the Asterisk project. Please do not directly contact
# any of the maintainers of this project for assistance;
# the project provides a web site, mailing lists and IRC
# channels for your use.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LICENSE file
# at the top of the source tree.
#

"""Swagger data model objects.

These objects should map directly to the Swagger api-docs, without a lot of
additional fields. In the process of translation, it should also validate the
model for consistency against the Swagger spec (i.e., fail if fields are
missing, or have incorrect values).

See https://github.com/wordnik/swagger-core/wiki/API-Declaration for the spec.
"""

from __future__ import print_function
import json
import os.path
import pprint
import re
import sys
import traceback

# We don't fully support Swagger 1.2, but we need it for subtyping
SWAGGER_VERSIONS = ["1.1", "1.2"]

SWAGGER_PRIMITIVES = [
    'void',
    'string',
    'boolean',
    'number',
    'int',
    'long',
    'double',
    'float',
    'Date',
]


class Stringify(object):
    """Simple mix-in to make the repr of the model classes more meaningful.
    """
    def __repr__(self):
        return "%s(%s)" % (self.__class__, pprint.saferepr(self.__dict__))


def compare_versions(lhs, rhs):
    '''Performs a lexicographical comparison between two version numbers.

    This properly handles simple major.minor.whatever.sure.why.not version
    numbers, but fails miserably if there's any letters in there.

    For reference:
      1.0 == 1.0
      1.0 < 1.0.1
      1.2 < 1.10

    @param lhs Left hand side of the comparison
    @param rhs Right hand side of the comparison
    @return  < 0 if lhs  < rhs
    @return == 0 if lhs == rhs
    @return  > 0 if lhs  > rhs
    '''
    lhs = [int(v) for v in lhs.split('.')]
    rhs = [int(v) for v in rhs.split('.')]
    return (lhs > rhs) - (lhs < rhs)


class ParsingContext(object):
    """Context information for parsing.

    This object is immutable. To change contexts (like adding an item to the
    stack), use the next() and next_stack() functions to build a new one.
    """

    def __init__(self, swagger_version, stack):
        self.__swagger_version = swagger_version
        self.__stack = stack

    def __repr__(self):
        return "ParsingContext(swagger_version=%s, stack=%s)" % (
            self.swagger_version, self.stack)

    def get_swagger_version(self):
        return self.__swagger_version

    def get_stack(self):
        return self.__stack

    swagger_version = property(get_swagger_version)

    stack = property(get_stack)

    def version_less_than(self, ver):
        return compare_versions(self.swagger_version, ver) < 0

    def next_stack(self, json, id_field):
        """Returns a new item pushed to the stack.

        @param json: Current JSON object.
        @param id_field: Field identifying this object.
        @return New context with additional item in the stack.
        """
        if not id_field in json:
            raise SwaggerError("Missing id_field: %s" % id_field, self)
        new_stack = self.stack + ['%s=%s' % (id_field, str(json[id_field]))]
        return ParsingContext(self.swagger_version, new_stack)

    def next(self, version=None, stack=None):
        if version is None:
            version = self.version
        if stack is None:
            stack = self.stack
        return ParsingContext(version, stack)


class SwaggerError(Exception):
    """Raised when an error is encountered mapping the JSON objects into the
    model.
    """

    def __init__(self, msg, context, cause=None):
        """Ctor.

        @param msg: String message for the error.
        @param context: ParsingContext object
        @param cause: Optional exception that caused this one.
        """
        super(Exception, self).__init__(msg, context, cause)


class SwaggerPostProcessor(object):
    """Post processing interface for model objects. This processor can add
    fields to model objects for additional information to use in the
    templates.
    """
    def process_resource_api(self, resource_api, context):
        """Post process a ResourceApi object.

        @param resource_api: ResourceApi object.
        @param context: Current context in the API.
        """
        pass

    def process_api(self, api, context):
        """Post process an Api object.

        @param api: Api object.
        @param context: Current context in the API.
        """
        pass

    def process_operation(self, operation, context):
        """Post process a Operation object.

        @param operation: Operation object.
        @param context: Current context in the API.
        """
        pass

    def process_parameter(self, parameter, context):
        """Post process a Parameter object.

        @param parameter: Parameter object.
        @param context: Current context in the API.
        """
        pass

    def process_model(self, model, context):
        """Post process a Model object.

        @param model: Model object.
        @param context: Current context in the API.
        """
        pass

    def process_property(self, property, context):
        """Post process a Property object.

        @param property: Property object.
        @param context: Current context in the API.
        """
        pass

    def process_type(self, swagger_type, context):
        """Post process a SwaggerType object.

        @param swagger_type: ResourceListing object.
        @param context: Current context in the API.
        """
        pass

    def process_resource_listing(self, resource_listing, context):
        """Post process the overall ResourceListing object.

        @param resource_listing: ResourceListing object.
        @param context: Current context in the API.
        """
        pass


class AllowableRange(Stringify):
    """Model of a allowableValues of type RANGE

    See https://github.com/wordnik/swagger-core/wiki/datatypes#complex-types
    """
    def __init__(self, min_value, max_value):
        self.min_value = min_value
        self.max_value = max_value

    def to_wiki(self):
        return "Allowed range: Min: {0}; Max: {1}".format(self.min_value, self.max_value)


class AllowableList(Stringify):
    """Model of a allowableValues of type LIST

    See https://github.com/wordnik/swagger-core/wiki/datatypes#complex-types
    """
    def __init__(self, values):
        self.values = values

    def to_wiki(self):
        return "Allowed values: {0}".format(", ".join(self.values))


def load_allowable_values(json, context):
    """Parse a JSON allowableValues object.

    This returns None, AllowableList or AllowableRange, depending on the
    valueType in the JSON. If the valueType is not recognized, a SwaggerError
    is raised.
    """
    if not json:
        return None

    if not 'valueType' in json:
        raise SwaggerError("Missing valueType field", context)

    value_type = json['valueType']

    if value_type == 'RANGE':
        if not 'min' in json and not 'max' in json:
            raise SwaggerError("Missing fields min/max", context)
        return AllowableRange(json.get('min'), json.get('max'))
    if value_type == 'LIST':
        if not 'values' in json:
            raise SwaggerError("Missing field values", context)
        return AllowableList(json['values'])
    raise SwaggerError("Unkown valueType %s" % value_type, context)


class Parameter(Stringify):
    """Model of an operation's parameter.

    See https://github.com/wordnik/swagger-core/wiki/parameters
    """

    required_fields = ['name', 'paramType', 'dataType']

    def __init__(self):
        self.param_type = None
        self.name = None
        self.description = None
        self.data_type = None
        self.required = None
        self.allowable_values = None
        self.allow_multiple = None

    def load(self, parameter_json, processor, context):
        context = context.next_stack(parameter_json, 'name')
        validate_required_fields(parameter_json, self.required_fields, context)
        self.name = parameter_json.get('name')
        self.param_type = parameter_json.get('paramType')
        self.description = parameter_json.get('description') or ''
        self.data_type = parameter_json.get('dataType')
        self.required = parameter_json.get('required') or False
        self.default_value = parameter_json.get('defaultValue')
        self.allowable_values = load_allowable_values(
            parameter_json.get('allowableValues'), context)
        self.allow_multiple = parameter_json.get('allowMultiple') or False
        processor.process_parameter(self, context)
        if parameter_json.get('allowedValues'):
            raise SwaggerError(
                "Field 'allowedValues' invalid; use 'allowableValues'",
                context)
        return self

    def is_type(self, other_type):
        return self.param_type == other_type


class ErrorResponse(Stringify):
    """Model of an error response.

    See https://github.com/wordnik/swagger-core/wiki/errors
    """

    required_fields = ['code', 'reason']

    def __init__(self):
        self.code = None
        self.reason = None

    def load(self, err_json, processor, context):
        context = context.next_stack(err_json, 'code')
        validate_required_fields(err_json, self.required_fields, context)
        self.code = err_json.get('code')
        self.reason = err_json.get('reason')
        return self


class SwaggerType(Stringify):
    """Model of a data type.
    """

    def __init__(self):
        self.name = None
        self.is_discriminator = None
        self.is_list = None
        self.singular_name = None
        self.is_primitive = None
        self.is_binary = None

    def load(self, type_name, processor, context):
        # Some common errors
        if type_name == 'integer':
            raise SwaggerError("The type for integer should be 'int'", context)

        self.name = type_name
        type_param = get_list_parameter_type(self.name)
        self.is_list = type_param is not None
        if self.is_list:
            self.singular_name = type_param
        else:
            self.singular_name = self.name
        self.is_primitive = self.singular_name in SWAGGER_PRIMITIVES
        self.is_binary = (self.singular_name == 'binary')
        processor.process_type(self, context)
        return self


class Operation(Stringify):
    """Model of an operation on an API

    See https://github.com/wordnik/swagger-core/wiki/API-Declaration#apis
    """

    required_fields = ['httpMethod', 'nickname', 'responseClass', 'summary']

    def __init__(self):
        self.http_method = None
        self.nickname = None
        self.response_class = None
        self.parameters = []
        self.summary = None
        self.notes = None
        self.error_responses = []

    def load(self, op_json, processor, context):
        context = context.next_stack(op_json, 'nickname')
        validate_required_fields(op_json, self.required_fields, context)
        self.http_method = op_json.get('httpMethod')
        self.nickname = op_json.get('nickname')
        response_class = op_json.get('responseClass')
        self.response_class = response_class and SwaggerType().load(
            response_class, processor, context)

        # Specifying WebSocket URL's is our own extension
        self.is_websocket = op_json.get('upgrade') == 'websocket'
        self.is_req = not self.is_websocket

        if self.is_websocket:
            self.websocket_protocol = op_json.get('websocketProtocol')
            if self.http_method != 'GET':
                raise SwaggerError(
                    "upgrade: websocket is only valid on GET operations",
                    context)

        params_json = op_json.get('parameters') or []
        self.parameters = [
            Parameter().load(j, processor, context) for j in params_json]
        self.query_parameters = [
            p for p in self.parameters if p.is_type('query')]
        self.has_query_parameters = self.query_parameters and True
        self.path_parameters = [
            p for p in self.parameters if p.is_type('path')]
        self.has_path_parameters = self.path_parameters and True
        self.header_parameters = [
            p for p in self.parameters if p.is_type('header')]
        self.has_header_parameters = self.header_parameters and True
        self.has_parameters = self.has_query_parameters or \
            self.has_path_parameters or self.has_header_parameters
        self.is_binary_response = self.response_class.is_binary

        # Body param is different, since there's at most one
        self.body_parameter = [
            p for p in self.parameters if p.is_type('body')]
        if len(self.body_parameter) > 1:
            raise SwaggerError("Cannot have more than one body param", context)
        self.body_parameter = self.body_parameter and self.body_parameter[0]
        self.has_body_parameter = self.body_parameter and True

        self.summary = op_json.get('summary')
        self.notes = op_json.get('notes')
        err_json = op_json.get('errorResponses') or []
        self.error_responses = [
            ErrorResponse().load(j, processor, context) for j in err_json]
        self.has_error_responses = self.error_responses != []
        processor.process_operation(self, context)
        return self


class Api(Stringify):
    """Model of a single API in an API declaration.

    See https://github.com/wordnik/swagger-core/wiki/API-Declaration
    """

    required_fields = ['path', 'operations']

    def __init__(self,):
        self.path = None
        self.description = None
        self.operations = []

    def load(self, api_json, processor, context):
        context = context.next_stack(api_json, 'path')
        validate_required_fields(api_json, self.required_fields, context)
        self.path = api_json.get('path')
        self.description = api_json.get('description')
        op_json = api_json.get('operations')
        self.operations = [
            Operation().load(j, processor, context) for j in op_json]
        self.has_websocket = any(op.is_websocket for op in self.operations)
        processor.process_api(self, context)
        return self


def get_list_parameter_type(type_string):
    """Returns the type parameter if the given type_string is List[].

    @param type_string: Type string to parse
    @returns Type parameter of the list, or None if not a List.
    """
    list_match = re.match('^List\[(.*)\]$', type_string)
    return list_match and list_match.group(1)


class Property(Stringify):
    """Model of a Swagger property.

    See https://github.com/wordnik/swagger-core/wiki/datatypes
    """

    required_fields = ['type']

    def __init__(self, name):
        self.name = name
        self.type = None
        self.description = None
        self.required = None

    def load(self, property_json, processor, context):
        validate_required_fields(property_json, self.required_fields, context)
        # Bit of a hack, but properties do not self-identify
        context = context.next_stack({'name': self.name}, 'name')
        self.description = property_json.get('description') or ''
        self.required = property_json.get('required') or False

        type = property_json.get('type')
        self.type = type and SwaggerType().load(type, processor, context)

        processor.process_property(self, context)
        return self


class Model(Stringify):
    """Model of a Swagger model.

    See https://github.com/wordnik/swagger-core/wiki/datatypes
    """

    required_fields = ['description', 'properties']

    def __init__(self):
        self.id = None
        self.subtypes = []
        self.__subtype_types = []
        self.notes = None
        self.description = None
        self.__properties = None
        self.__discriminator = None
        self.__extends_type = None

    def load(self, id, model_json, processor, context):
        context = context.next_stack(model_json, 'id')
        validate_required_fields(model_json, self.required_fields, context)
        # The duplication of the model's id is required by the Swagger spec.
        self.id = model_json.get('id')
        if id != self.id:
            raise SwaggerError("Model id doesn't match name", context)
        self.subtypes = model_json.get('subTypes') or []
        if self.subtypes and context.version_less_than("1.2"):
            raise SwaggerError("Type extension support added in Swagger 1.2",
                               context)
        self.description = model_json.get('description')
        props = model_json.get('properties').items() or []
        self.__properties = [
            Property(k).load(j, processor, context) for (k, j) in props]
        self.__properties = sorted(self.__properties, key=lambda p: p.name)

        discriminator = model_json.get('discriminator')

        if discriminator:
            if context.version_less_than("1.2"):
                raise SwaggerError("Discriminator support added in Swagger 1.2",
                                   context)

            discr_props = [p for p in self.__properties if p.name == discriminator]
            if not discr_props:
                raise SwaggerError(
                    "Discriminator '%s' does not name a property of '%s'" % (
                        discriminator, self.id),
                    context)

            self.__discriminator = discr_props[0]

        self.model_json = json.dumps(model_json,
                                     indent=2, separators=(',', ': '))

        processor.process_model(self, context)
        return self

    def extends(self):
        return self.__extends_type and self.__extends_type.id

    def set_extends_type(self, extends_type):
        self.__extends_type = extends_type

    def set_subtype_types(self, subtype_types):
        self.__subtype_types = subtype_types

    def discriminator(self):
        """Returns the discriminator, digging through base types if needed.
        """
        return self.__discriminator or \
            self.__extends_type and self.__extends_type.discriminator()

    def properties(self):
        base_props = []
        if self.__extends_type:
            base_props = self.__extends_type.properties()
        return base_props + self.__properties

    def has_properties(self):
        return len(self.properties()) > 0

    def all_subtypes(self):
        """Returns the full list of all subtypes, including sub-subtypes.
        """
        res = self.__subtype_types + \
              [subsubtypes for subtype in self.__subtype_types
               for subsubtypes in subtype.all_subtypes()]
        return sorted(res, key=lambda m: m.id)

    def has_subtypes(self):
        """Returns True if type has any subtypes.
        """
        return len(self.subtypes) > 0


class ApiDeclaration(Stringify):
    """Model class for an API Declaration.

    See https://github.com/wordnik/swagger-core/wiki/API-Declaration
    """

    required_fields = [
        'swaggerVersion', '_author', '_copyright', 'apiVersion', 'basePath',
        'resourcePath', 'apis', 'models'
    ]

    def __init__(self):
        self.swagger_version = None
        self.author = None
        self.copyright = None
        self.api_version = None
        self.base_path = None
        self.resource_path = None
        self.apis = []
        self.models = []

    def load_file(self, api_declaration_file, processor):
        context = ParsingContext(None, [api_declaration_file])
        try:
            return self.__load_file(api_declaration_file, processor, context)
        except SwaggerError:
            raise
        except Exception as e:
            print("Error: ", traceback.format_exc(), file=sys.stderr)
            raise SwaggerError(
                "Error loading %s" % api_declaration_file, context, e)

    def __load_file(self, api_declaration_file, processor, context):
        with open(api_declaration_file) as fp:
            self.load(json.load(fp), processor, context)

        expected_resource_path = '/api-docs/' + \
            os.path.basename(api_declaration_file) \
            .replace(".json", ".{format}")

        if self.resource_path != expected_resource_path:
            print("%s != %s" % (self.resource_path, expected_resource_path),
                file=sys.stderr)
            raise SwaggerError("resourcePath has incorrect value", context)

        return self

    def load(self, api_decl_json, processor, context):
        """Loads a resource from a single Swagger resource.json file.
        """
        # If the version doesn't match, all bets are off.
        self.swagger_version = api_decl_json.get('swaggerVersion')
        context = context.next(version=self.swagger_version)
        if not self.swagger_version in SWAGGER_VERSIONS:
            raise SwaggerError(
                "Unsupported Swagger version %s" % self.swagger_version, context)

        validate_required_fields(api_decl_json, self.required_fields, context)

        self.author = api_decl_json.get('_author')
        self.copyright = api_decl_json.get('_copyright')
        self.api_version = api_decl_json.get('apiVersion')
        self.base_path = api_decl_json.get('basePath')
        self.resource_path = api_decl_json.get('resourcePath')
        self.requires_modules = api_decl_json.get('requiresModules') or []
        api_json = api_decl_json.get('apis') or []
        self.apis = [
            Api().load(j, processor, context) for j in api_json]
        paths = set()
        for api in self.apis:
            if api.path in paths:
                raise SwaggerError("API with duplicated path: %s" % api.path, context)
            paths.add(api.path)
        self.has_websocket = any(api.has_websocket for api in self.apis)
        models = api_decl_json.get('models').items() or []
        self.models = [Model().load(id, json, processor, context)
                       for (id, json) in models]
        self.models = sorted(self.models, key=lambda m: m.id)
        # Now link all base/extended types
        model_dict = dict((m.id, m) for m in self.models)
        for m in self.models:
            def link_subtype(name):
                res = model_dict.get(name)
                if not res:
                    raise SwaggerError("%s has non-existing subtype %s",
                                       m.id, name)
                res.set_extends_type(m)
                return res;
            if m.subtypes:
                m.set_subtype_types([
                    link_subtype(subtype) for subtype in m.subtypes])
        return self


class ResourceApi(Stringify):
    """Model of an API listing in the resources.json file.
    """

    required_fields = ['path', 'description']

    def __init__(self):
        self.path = None
        self.description = None
        self.api_declaration = None

    def load(self, api_json, processor, context):
        context = context.next_stack(api_json, 'path')
        validate_required_fields(api_json, self.required_fields, context)
        self.path = api_json['path'].replace('{format}', 'json')
        self.description = api_json['description']

        if not self.path or self.path[0] != '/':
            raise SwaggerError("Path must start with /", context)
        processor.process_resource_api(self, context)
        return self

    def load_api_declaration(self, base_dir, processor):
        self.file = (base_dir + self.path)
        self.api_declaration = ApiDeclaration().load_file(self.file, processor)
        processor.process_resource_api(self, [self.file])


class ResourceListing(Stringify):
    """Model of Swagger's resources.json file.
    """

    required_fields = ['apiVersion', 'basePath', 'apis']

    def __init__(self):
        self.swagger_version = None
        self.api_version = None
        self.base_path = None
        self.apis = None

    def load_file(self, resource_file, processor):
        context = ParsingContext(None, [resource_file])
        try:
            return self.__load_file(resource_file, processor, context)
        except SwaggerError:
            raise
        except Exception as e:
            print("Error: ", traceback.format_exc(), file=sys.stderr)
            raise SwaggerError(
                "Error loading %s" % resource_file, context, e)

    def __load_file(self, resource_file, processor, context):
        with open(resource_file) as fp:
            return self.load(json.load(fp), processor, context)

    def load(self, resources_json, processor, context):
        # If the version doesn't match, all bets are off.
        self.swagger_version = resources_json.get('swaggerVersion')
        if not self.swagger_version in SWAGGER_VERSIONS:
            raise SwaggerError(
                "Unsupported Swagger version %s" % self.swagger_version, context)

        validate_required_fields(resources_json, self.required_fields, context)
        self.api_version = resources_json['apiVersion']
        self.base_path = resources_json['basePath']
        apis_json = resources_json['apis']
        self.apis = [
            ResourceApi().load(j, processor, context) for j in apis_json]
        processor.process_resource_listing(self, context)
        return self


def validate_required_fields(json, required_fields, context):
    """Checks a JSON object for a set of required fields.

    If any required field is missing, a SwaggerError is raised.

    @param json: JSON object to check.
    @param required_fields: List of required fields.
    @param context: Current context in the API.
    """
    missing_fields = [f for f in required_fields if not f in json]

    if missing_fields:
        raise SwaggerError(
            "Missing fields: %s" % ', '.join(missing_fields), context)