summaryrefslogtreecommitdiffstats
path: root/openapi/generate_openapi.py
blob: 54526416fc0a0e0ae2346678de068d4eaa184a6d (plain)
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
#!/bin/env python3

import argparse
import esprima
import json
import logging
import os
import re
import sys
import traceback


logger = logging.getLogger(__name__)
err_context = 3


def get_req_body_elems(obj, elems):
    if obj.type == 'FunctionExpression':
        get_req_body_elems(obj.body, elems)
    elif obj.type == 'BlockStatement':
        for s in obj.body:
            get_req_body_elems(s, elems)
    elif obj.type == 'TryStatement':
        get_req_body_elems(obj.block, elems)
    elif obj.type == 'ExpressionStatement':
        get_req_body_elems(obj.expression, elems)
    elif obj.type == 'MemberExpression':
        left = get_req_body_elems(obj.object, elems)
        right = obj.property.name
        if left == 'req.body' and right not in elems:
            elems.append(right)
        return '{}.{}'.format(left, right)
    elif obj.type == 'VariableDeclaration':
        for s in obj.declarations:
            get_req_body_elems(s, elems)
    elif obj.type == 'VariableDeclarator':
        if obj.id.type == 'ObjectPattern':
            # get_req_body_elems() can't be called directly here:
            # const {isAdmin, isNoComments, isCommentOnly} = req.body;
            right = get_req_body_elems(obj.init, elems)
            if right == 'req.body':
                for p in obj.id.properties:
                    name = p.key.name
                    if name not in elems:
                        elems.append(name)
        else:
            get_req_body_elems(obj.init, elems)
    elif obj.type == 'Property':
        get_req_body_elems(obj.value, elems)
    elif obj.type == 'ObjectExpression':
        for s in obj.properties:
            get_req_body_elems(s, elems)
    elif obj.type == 'CallExpression':
        for s in obj.arguments:
            get_req_body_elems(s, elems)
    elif obj.type == 'ArrayExpression':
        for s in obj.elements:
            get_req_body_elems(s, elems)
    elif obj.type == 'IfStatement':
        get_req_body_elems(obj.test, elems)
        if obj.consequent is not None:
            get_req_body_elems(obj.consequent, elems)
        if obj.alternate is not None:
            get_req_body_elems(obj.alternate, elems)
    elif obj.type in ('LogicalExpression', 'BinaryExpression', 'AssignmentExpression'):
        get_req_body_elems(obj.left, elems)
        get_req_body_elems(obj.right, elems)
    elif obj.type in ('ReturnStatement', 'UnaryExpression'):
        get_req_body_elems(obj.argument, elems)
    elif obj.type == 'Literal':
        pass
    elif obj.type == 'Identifier':
        return obj.name
    elif obj.type == 'FunctionDeclaration':
        pass
    else:
        print(obj)
    return ''


def cleanup_jsdocs(jsdoc):
    # remove leading spaces before the first '*'
    doc = [s.lstrip() for s in jsdoc.value.split('\n')]

    # remove leading stars
    doc = [s.lstrip('*') for s in doc]

    # remove leading empty lines
    while len(doc) and not doc[0].strip():
        doc.pop(0)

    # remove terminating empty lines
    while len(doc) and not doc[-1].strip():
        doc.pop(-1)

    return doc


class JS2jsonDecoder(json.JSONDecoder):
    def decode(self, s):
        result = super().decode(s)  # result = super(Decoder, self).decode(s) for Python 2.x
        return self._decode(result)

    def _decode(self, o):
        if isinstance(o, str) or isinstance(o, unicode):
            try:
                return int(o)
            except ValueError:
                return o
        elif isinstance(o, dict):
            return {k: self._decode(v) for k, v in o.items()}
        elif isinstance(o, list):
            return [self._decode(v) for v in o]
        else:
            return o


def load_return_type_jsdoc_json(data):
    regex_replace = [(r'\n', r' '),  # replace new lines by spaces
                     (r'([\{\s,])(\w+)(:)', r'\1"\2"\3'),  # insert double quotes in keys
                     (r'(:)\s*([^:\},\]]+)\s*([\},\]])', r'\1"\2"\3'),  # insert double quotes in values
                     (r'(\[)\s*([^{].+)\s*(\])', r'\1"\2"\3'),  # insert double quotes in array items
                     (r'^\s*([^\[{].+)\s*', r'"\1"')]  # insert double quotes in single item
    for r, s in regex_replace:
        data = re.sub(r, s, data)
    return json.loads(data)


class EntryPoint(object):
    def __init__(self, schema, statements):
        self.schema = schema
        self.method, self._path, self.body = statements
        self._jsdoc = None
        self._doc = {}
        self._raw_doc = None
        self.path = self.compute_path()
        self.method_name = self.method.value.lower()
        self.body_params = []
        if self.method_name in ('post', 'put'):
            get_req_body_elems(self.body, self.body_params)

        # replace the :parameter in path by {parameter}
        self.url = re.sub(r':([^/]*)Id', r'{\1}', self.path)
        self.url = re.sub(r':([^/]*)', r'{\1}', self.url)

        # reduce the api name
        # get_boards_board_cards() should be get_board_cards()
        tokens = self.url.split('/')
        reduced_function_name = []
        for i, token in enumerate(tokens):
            if token in ('api'):
                continue
            if (i < len(tokens) - 1 and  # not the last item
               tokens[i + 1].startswith('{')):  # and the next token is a parameter
                continue
            reduced_function_name.append(token.strip('{}'))
        self.reduced_function_name = '_'.join(reduced_function_name)

        # mark the schema as used
        schema.used = True

    def compute_path(self):
        return self._path.value.rstrip('/')

    def log(self, message, level):
        if self._raw_doc is None:
            logger.log(level, 'in {},'.format(self.schema.name))
            logger.log(level, message)
            return
        logger.log(level, 'in {}, lines {}-{}'.format(self.schema.name,
                                                      self._raw_doc.loc.start.line,
                                                      self._raw_doc.loc.end.line))
        logger.log(level, self._raw_doc.value)
        logger.log(level, message)

    def error(self, message):
        return self.log(message, logging.ERROR)

    def warn(self, message):
        return self.log(message, logging.WARNING)

    def info(self, message):
        return self.log(message, logging.INFO)

    @property
    def doc(self):
        return self._doc

    @doc.setter
    def doc(self, doc):
        '''Parse the JSDoc attached to an entry point.
        `jsdoc` will not get these right as they are not attached to a method.
        So instead, we do our custom parsing here (yes, subject to errors).

        The expected format is the following (empty lines between entries
        are ignored):
        /**
         * @operation name_of_entry_point
         * @tag: a_tag_to_add
         * @tag: an_other_tag_to_add
         * @summary A nice summary, better in one line.
         *
         * @description This is a quite long description.
         *              We can use *mardown* as the final rendering is done
         *              by slate.
         *
         *     indentation doesn't matter.
         *
         * @param param_0 description of param 0
         * @param {string} param_1 we can also put the type of the parameter
         *    before its name, like in JSDoc
         * @param {boolean} [param_2] we can also tell if the parameter is
         *    optional by adding square brackets around its name
         *
         * @return Documents a return value
         */

        Notes:
         - name_of_entry_point will be referenced in the ToC of the generated
           document. This is also the operationId used in the resulting openapi
           file. It needs to be uniq in the namesapce (the current schema.js
           file)
         - tags are appended to the current Schema attached to the file
        '''

        self._raw_doc = doc

        self._jsdoc = cleanup_jsdocs(doc)

        def store_tag(tag, data):
            # check that there is something to store first
            if not data.strip():
                return

            # remove terminating whitespaces and empty lines
            data = data.rstrip()

            # parameters are handled specially
            if tag == 'param':
                if 'params' not in self._doc:
                    self._doc['params'] = {}
                params = self._doc['params']

                param_type = None
                try:
                    name, desc = data.split(maxsplit=1)
                except ValueError:
                    desc = ''

                if name.startswith('{'):
                    param_type = name.strip('{}')
                    if param_type not in ['string', 'number', 'boolean', 'integer', 'array', 'file']:
                        self.warn('unknown type {}\n allowed values: string, number, boolean, integer, array, file'.format(param_type))
                    try:
                        name, desc = desc.split(maxsplit=1)
                    except ValueError:
                        desc = ''

                optional = name.startswith('[') and name.endswith(']')

                if optional:
                    name = name[1:-1]

                # we should not have 2 identical parameter names
                if tag in params:
                    self.warn('overwriting parameter {}'.format(name))

                params[name] = (param_type, optional, desc)

                if name.endswith('Id'):
                    # we strip out the 'Id' from the form parameters, we need
                    # to keep the actual description around
                    name = name[:-2]
                    if name not in params:
                        params[name] = (param_type, optional, desc)

                return

            # 'tag' can be set several times
            if tag == 'tag':
                if tag not in self._doc:
                    self._doc[tag] = []
                self._doc[tag].append(data)

                return

            # 'return' tag is json
            if tag == 'return_type':
                try:
                    data = load_return_type_jsdoc_json(data)
                except json.decoder.JSONDecodeError:
                    pass

            # we should not have 2 identical tags but @param or @tag
            if tag in self._doc:
                self.warn('overwriting tag {}'.format(tag))

            self._doc[tag] = data

        # reset the current doc fields
        self._doc = {}

        # first item is supposed to be the description
        current_tag = 'description'
        current_data = ''

        for line in self._jsdoc:
            if line.lstrip().startswith('@'):
                tag, data = line.lstrip().split(maxsplit=1)

                if tag in ['@operation', '@summary', '@description', '@param', '@return_type', '@tag']:
                    # store the current data
                    store_tag(current_tag, current_data)

                    current_tag = tag.lstrip('@')
                    current_data = ''
                    line = data
                else:
                    self.info('Unknown tag {}, ignoring'.format(tag))

            current_data += line + '\n'

        store_tag(current_tag, current_data)

    @property
    def summary(self):
        if 'summary' in self._doc:
            # new lines are not allowed
            return self._doc['summary'].replace('\n', ' ')

        return None

    def doc_param(self, name):
        if 'params' in self._doc and name in self._doc['params']:
            return self._doc['params'][name]
        return None, None, None

    def print_openapi_param(self, name, indent):
        ptype, poptional, pdesc = self.doc_param(name)
        if pdesc is not None:
            print('{}description: |'.format(' ' * indent))
            print('{}{}'.format(' ' * (indent + 2), pdesc))
        else:
            print('{}description: the {} value'.format(' ' * indent, name))
        if ptype is not None:
            print('{}type: {}'.format(' ' * indent, ptype))
        else:
            print('{}type: string'.format(' ' * indent))
        if poptional:
            print('{}required: false'.format(' ' * indent))
        else:
            print('{}required: true'.format(' ' * indent))

    @property
    def operationId(self):
        if 'operation' in self._doc:
            return self._doc['operation']
        return '{}_{}'.format(self.method_name, self.reduced_function_name)

    @property
    def description(self):
        if 'description' in self._doc:
            return self._doc['description']
        return None

    @property
    def returns(self):
        if 'return_type' in self._doc:
            return self._doc['return_type']
        return None

    @property
    def tags(self):
        tags = []
        if self.schema.fields is not None:
            tags.append(self.schema.name)
        if 'tag' in self._doc:
            tags.extend(self._doc['tag'])
        return tags

    def print_openapi_return(self, obj, indent):
        if isinstance(obj, dict):
            print('{}type: object'.format(' ' * indent))
            print('{}properties:'.format(' ' * indent))
            for k, v in obj.items():
                print('{}{}:'.format(' ' * (indent + 2), k))
                self.print_openapi_return(v, indent + 4)

        elif isinstance(obj, list):
            if len(obj) > 1:
                self.error('Error while parsing @return tag, an array should have only one type')
            print('{}type: array'.format(' ' * indent))
            print('{}items:'.format(' ' * indent))
            self.print_openapi_return(obj[0], indent + 2)

        elif isinstance(obj, str) or isinstance(obj, unicode):
            rtype = 'type: ' + obj
            if obj == self.schema.name:
                rtype = '$ref: "#/definitions/{}"'.format(obj)
            print('{}{}'.format(' ' * indent, rtype))

    def print_openapi(self):
        parameters = [token[1:-2] if token.endswith('Id') else token[1:]
                      for token in self.path.split('/')
                      if token.startswith(':')]

        print('    {}:'.format(self.method_name))

        print('      operationId: {}'.format(self.operationId))

        if self.summary is not None:
            print('      summary: {}'.format(self.summary))

        if self.description is not None:
            print('      description: |')
            for line in self.description.split('\n'):
                if line.strip():
                    print('        {}'.format(line))
                else:
                    print('')

        if len(self.tags) > 0:
            print('      tags:')
            for tag in self.tags:
                print('        - {}'.format(tag))

        # export the parameters
        if self.method_name in ('post', 'put'):
            print('''      consumes:
        - multipart/form-data
        - application/json''')
        if len(parameters) > 0 or self.method_name in ('post', 'put'):
            print('      parameters:')
        if self.method_name in ('post', 'put'):
            for f in self.body_params:
                print('''        - name: {}
          in: formData'''.format(f))
                self.print_openapi_param(f, 10)
        for p in parameters:
            if p in self.body_params:
                self.error(' '.join((p, self.path, self.method_name)))
            print('''        - name: {}
          in: path'''.format(p))
            self.print_openapi_param(p, 10)
        print('''      produces:
        - application/json
      security:
          - UserSecurity: []
      responses:
        '200':
          description: |-
            200 response''')
        if self.returns is not None:
            print('          schema:')
            self.print_openapi_return(self.returns, 12)


class SchemaProperty(object):
    def __init__(self, statement, schema, context):
        self.schema = schema
        self.statement = statement
        self.name = statement.key.name or statement.key.value
        self.type = 'object'
        self.blackbox = False
        self.required = True
        for p in statement.value.properties:
            try:
                if p.key.name == 'type':
                    if p.value.type == 'Identifier':
                        self.type = p.value.name.lower()
                    elif p.value.type == 'ArrayExpression':
                        self.type = 'array'
                        self.elements = [e.name.lower() for e in p.value.elements]

                elif p.key.name == 'allowedValues':
                    self.type = 'enum'
                    if p.value.type == 'ArrayExpression':
                        self.enum = [e.value.lower() for e in p.value.elements]
                    elif p.value.type == 'Identifier':
                        # tree wide lookout for the identifier
                        def find_variable(elem, match):
                            if isinstance(elem, list):
                                for value in elem:
                                    ret = find_variable(value, match)
                                    if ret is not None:
                                        return ret

                            try:
                                items = elem.items()
                            except AttributeError:
                                return None
                            except TypeError:
                                return None

                            if (elem.type == 'VariableDeclarator' and
                               elem.id.name == match):
                                return elem

                            for type, value in items:
                                ret = find_variable(value, match)
                                if ret is not None:
                                    return ret

                            return None

                        elem = find_variable(context.program.body, p.value.name)

                        if elem.init.type != 'ArrayExpression':
                            raise TypeError('can not find "{}"'.format(p.value.name))

                        self.enum = [e.value.lower() for e in elem.init.elements]

                elif p.key.name == 'blackbox':
                    self.blackbox = True

                elif p.key.name == 'optional' and p.value.value:
                    self.required = False
            except Exception:
                input = ''
                for line in range(p.loc.start.line - err_context, p.loc.end.line + 1 + err_context):
                    if line < p.loc.start.line or line > p.loc.end.line:
                        input += '. '
                    else:
                        input += '>>'
                    input += context.text_at(line, line)
                input = ''.join(input)
                logger.error('{}:{}-{} can not parse {}:\n{}'.format(context.path,
                                                                     p.loc.start.line,
                                                                     p.loc.end.line,
                                                                     p.type,
                                                                     input))
                logger.error('esprima tree:\n{}'.format(p))

                logger.error(traceback.format_exc())
                sys.exit(1)

        self._doc = None
        self._raw_doc = None

    @property
    def doc(self):
        return self._doc

    @doc.setter
    def doc(self, jsdoc):
        self._raw_doc = jsdoc
        self._doc = cleanup_jsdocs(jsdoc)

    def process_jsdocs(self, jsdocs):
        start = self.statement.key.loc.start.line
        for index, doc in enumerate(jsdocs):
            if start + 1 == doc.loc.start.line:
                self.doc = doc
                jsdocs.pop(index)
                return

    def __repr__(self):
        return 'SchemaProperty({}{}, {})'.format(self.name,
                                                 '*' if self.required else '',
                                                 self.doc)

    def print_openapi(self, indent, current_schema, required_properties):
        schema_name = self.schema.name
        name = self.name

        # deal with subschemas
        if '.' in name:
            if name.endswith('$'):
                # reference in reference
                subschema = ''.join([n.capitalize() for n in self.name.split('.')[:-1]])
                subschema = self.schema.name + subschema
                if current_schema != subschema:
                    if required_properties is not None and required_properties:
                        print('    required:')
                        for f in required_properties:
                            print('      - {}'.format(f))
                        required_properties.clear()

                    print('''  {}:
    type: object'''.format(subschema))
                    return current_schema

            subschema = name.split('.')[0]
            schema_name = self.schema.name + subschema.capitalize()
            name = name.split('.')[-1]

            if current_schema != schema_name:
                if required_properties is not None and required_properties:
                    print('    required:')
                    for f in required_properties:
                        print('      - {}'.format(f))
                    required_properties.clear()

                print('''  {}:
    type: object
    properties:'''.format(schema_name))

        if required_properties is not None and self.required:
            required_properties.append(name)

        print('{}{}:'.format(' ' * indent, name))

        if self.doc is not None:
            print('{}  description: |'.format(' ' * indent))
            for line in self.doc:
                if line.strip():
                    print('{}    {}'.format(' ' * indent, line))
                else:
                    print('')

        ptype = self.type
        if ptype in ('enum', 'date'):
            ptype = 'string'
        if ptype != 'object':
            print('{}  type: {}'.format(' ' * indent, ptype))

        if self.type == 'array':
            print('{}  items:'.format(' ' * indent))
            for elem in self.elements:
                if elem == 'object':
                    print('{}    $ref: "#/definitions/{}"'.format(' ' * indent, schema_name + name.capitalize()))
                else:
                    print('{}    type: {}'.format(' ' * indent, elem))
                    if not self.required:
                        print('{}    x-nullable: true'.format(' ' * indent))

        elif self.type == 'object':
            if self.blackbox:
                print('{}  type: object'.format(' ' * indent))
            else:
                print('{}  $ref: "#/definitions/{}"'.format(' ' * indent, schema_name + name.capitalize()))

        elif self.type == 'enum':
            print('{}  enum:'.format(' ' * indent))
            for enum in self.enum:
                print('{}    - {}'.format(' ' * indent, enum))

        if '.' not in self.name and not self.required:
            print('{}  x-nullable: true'.format(' ' * indent))

        return schema_name


class Schemas(object):
    def __init__(self, context, data=None, jsdocs=None, name=None):
        self.name = name
        self._data = data
        self.fields = None
        self.used = False

        if data is not None:
            if self.name is None:
                self.name = data.expression.callee.object.name

            content = data.expression.arguments[0].arguments[0]
            self.fields = [SchemaProperty(p, self, context) for p in content.properties]

        self._doc = None
        self._raw_doc = None

        if jsdocs is not None:
            self.process_jsdocs(jsdocs)

    @property
    def doc(self):
        if self._doc is None:
            return None
        return ' '.join(self._doc)

    @doc.setter
    def doc(self, jsdoc):
        self._raw_doc = jsdoc
        self._doc = cleanup_jsdocs(jsdoc)

    def process_jsdocs(self, jsdocs):
        start = self._data.loc.start.line
        end = self._data.loc.end.line

        for doc in jsdocs:
            if doc.loc.end.line + 1 == start:
                self.doc = doc

        docs = [doc
                for doc in jsdocs
                if doc.loc.start.line >= start and doc.loc.end.line <= end]

        for field in self.fields:
            field.process_jsdocs(docs)

    def print_openapi(self):
        # empty schemas are skipped
        if self.fields is None:
            return

        print('  {}:'.format(self.name))
        print('    type: object')
        if self.doc is not None:
            print('    description: {}'.format(self.doc))

        print('    properties:')

        # first print out the object itself
        properties = [field for field in self.fields if '.' not in field.name]
        for prop in properties:
            prop.print_openapi(6, None, None)

        required_properties = [f.name for f in properties if f.required]
        if required_properties:
            print('    required:')
            for f in required_properties:
                print('      - {}'.format(f))

        # then print the references
        current = None
        required_properties = []
        properties = [f for f in self.fields if '.' in f.name and not f.name.endswith('$')]
        for prop in properties:
            current = prop.print_openapi(6, current, required_properties)

        if required_properties:
            print('    required:')
            for f in required_properties:
                print('      - {}'.format(f))

        required_properties = []
        # then print the references in the references
        for prop in [f for f in self.fields if '.' in f.name and f.name.endswith('$')]:
            current = prop.print_openapi(6, current, required_properties)

        if required_properties:
            print('    required:')
            for f in required_properties:
                print('      - {}'.format(f))


class Context(object):
    def __init__(self, path):
        self.path = path

        with open(path) as f:
            self._txt = f.readlines()

        data = ''.join(self._txt)
        self.program = esprima.parseModule(data,
                                           options={
                                               'comment': True,
                                               'loc': True
                                           })

    def txt_for(self, statement):
        return self.text_at(statement.loc.start.line, statement.loc.end.line)

    def text_at(self, begin, end):
        return ''.join(self._txt[begin - 1:end])


def parse_schemas(schemas_dir):

    schemas = {}
    entry_points = []

    for root, dirs, files in os.walk(schemas_dir):
        files.sort()
        for filename in files:
            path = os.path.join(root, filename)
            try:
                # if the file failed, it's likely it doesn't contain a schema
                context = Context(path)
            except:
                continue

            program = context.program

            current_schema = None
            jsdocs = [c for c in program.comments
                      if c.type == 'Block' and c.value.startswith('*\n')]

            try:

                for statement in program.body:

                    # find the '<ITEM>.attachSchema(new SimpleSchema(<data>)'
                    # those are the schemas
                    if (statement.type == 'ExpressionStatement' and
                       statement.expression.callee is not None and
                       statement.expression.callee.property is not None and
                       statement.expression.callee.property.name == 'attachSchema' and
                       statement.expression.arguments[0].type == 'NewExpression' and
                       statement.expression.arguments[0].callee.name == 'SimpleSchema'):

                        schema = Schemas(context, statement, jsdocs)
                        current_schema = schema.name
                        schemas[current_schema] = schema

                    # find all the 'if (Meteor.isServer) { JsonRoutes.add('
                    # those are the entry points of the API
                    elif (statement.type == 'IfStatement' and
                          statement.test.type == 'MemberExpression' and
                          statement.test.object.name == 'Meteor' and
                          statement.test.property.name == 'isServer'):
                            data = [s.expression.arguments
                                    for s in statement.consequent.body
                                    if (s.type == 'ExpressionStatement' and
                                        s.expression.type == 'CallExpression' and
                                        s.expression.callee.object.name == 'JsonRoutes')]

                            # we found at least one entry point, keep them
                            if len(data) > 0:
                                if current_schema is None:
                                    current_schema = filename
                                    schemas[current_schema] = Schemas(context, name=current_schema)

                                schema_entry_points = [EntryPoint(schemas[current_schema], d)
                                                       for d in data]
                                entry_points.extend(schema_entry_points)

                                # try to match JSDoc to the operations
                                for entry_point in schema_entry_points:
                                    operation = entry_point.method  # POST/GET/PUT/DELETE
                                    jsdoc = [j for j in jsdocs
                                             if j.loc.end.line + 1 == operation.loc.start.line]
                                    if bool(jsdoc):
                                        entry_point.doc = jsdoc[0]
            except TypeError:
                logger.warning(context.txt_for(statement))
                logger.error('{}:{}-{} can not parse {}'.format(path,
                                                                statement.loc.start.line,
                                                                statement.loc.end.line,
                                                                statement.type))
                raise

    return schemas, entry_points


def generate_openapi(schemas, entry_points, version):
    print('''swagger: '2.0'
info:
  title: Wekan REST API
  version: {0}
  description: |
    The REST API allows you to control and extend Wekan with ease.

    If you are an end-user and not a dev or a tester, [create an issue](https://github.com/wekan/wekan/issues/new) to request new APIs.

    > All API calls in the documentation are made using `curl`.  However, you are free to use Java / Python / PHP / Golang / Ruby / Swift / Objective-C / Rust / Scala / C# or any other programming languages.

    # Production Security Concerns
    When calling a production Wekan server, ensure it is running via HTTPS and has a valid SSL Certificate. The login method requires you to post your username and password in plaintext, which is why we highly suggest only calling the REST login api over HTTPS. Also, few things to note:

    * Only call via HTTPS
    * Implement a timed authorization token expiration strategy
    * Ensure the calling user only has permissions for what they are calling and no more

schemes:
  - http

securityDefinitions:
  UserSecurity:
    type: apiKey
    in: header
    name: Authorization

paths:
  /users/login:
    post:
      operationId: login
      summary: Login with REST API
      consumes:
        - application/x-www-form-urlencoded
        - application/json
      tags:
        - Login
      parameters:
        - name: username
          in: formData
          required: true
          description: |
            Your username
          type: string
        - name: password
          in: formData
          required: true
          description: |
            Your password
          type: string
          format: password
      responses:
        200:
          description: |-
            Successful authentication
          schema:
            items:
              properties:
                id:
                  type: string
                token:
                  type: string
                tokenExpires:
                  type: string
        400:
          description: |
            Error in authentication
          schema:
            items:
              properties:
                error:
                  type: number
                reason:
                  type: string
        default:
          description: |
            Error in authentication
  /users/register:
    post:
      operationId: register
      summary: Register with REST API
      description: |
        Notes:
          - You will need to provide the token for any of the authenticated methods.
      consumes:
        - application/x-www-form-urlencoded
        - application/json
      tags:
        - Login
      parameters:
        - name: username
          in: formData
          required: true
          description: |
            Your username
          type: string
        - name: password
          in: formData
          required: true
          description: |
            Your password
          type: string
          format: password
        - name: email
          in: formData
          required: true
          description: |
            Your email
          type: string
      responses:
        200:
          description: |-
            Successful registration
          schema:
            items:
              properties:
                id:
                  type: string
                token:
                  type: string
                tokenExpires:
                  type: string
        400:
          description: |
            Error in registration
          schema:
            items:
              properties:
                error:
                  type: number
                reason:
                  type: string
        default:
          description: |
            Error in registration
'''.format(version))

    # GET and POST on the same path are valid, we need to reshuffle the paths
    # with the path as the sorting key
    methods = {}
    for ep in entry_points:
        if ep.path not in methods:
            methods[ep.path] = []
        methods[ep.path].append(ep)

    sorted_paths = list(methods.keys())
    sorted_paths.sort()

    for path in sorted_paths:
        print('  {}:'.format(methods[path][0].url))

        for ep in methods[path]:
            ep.print_openapi()

    print('definitions:')
    for schema in schemas.values():
        # do not export the objects if there is no API attached
        if not schema.used:
            continue

        schema.print_openapi()


def main():
    parser = argparse.ArgumentParser(description='Generate an OpenAPI 2.0 from the given JS schemas.')
    script_dir = os.path.dirname(os.path.realpath(__file__))
    parser.add_argument('--release', default='git-master', nargs=1,
                        help='the current version of the API, can be retrieved by running `git describe --tags --abbrev=0`')
    parser.add_argument('dir', default='{}/../models'.format(script_dir), nargs='?',
                        help='the directory where to look for schemas')

    args = parser.parse_args()
    schemas, entry_points = parse_schemas(args.dir)
    generate_openapi(schemas, entry_points, args.release[0])


if __name__ == '__main__':
    main()