2
0

generate_openapi.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. #!/bin/env python3
  2. import argparse
  3. import esprima
  4. import json
  5. import logging
  6. import os
  7. import re
  8. import sys
  9. import traceback
  10. logger = logging.getLogger(__name__)
  11. err_context = 3
  12. def get_req_body_elems(obj, elems):
  13. if obj.type == 'FunctionExpression':
  14. get_req_body_elems(obj.body, elems)
  15. elif obj.type == 'BlockStatement':
  16. for s in obj.body:
  17. get_req_body_elems(s, elems)
  18. elif obj.type == 'TryStatement':
  19. get_req_body_elems(obj.block, elems)
  20. elif obj.type == 'ExpressionStatement':
  21. get_req_body_elems(obj.expression, elems)
  22. elif obj.type == 'MemberExpression':
  23. left = get_req_body_elems(obj.object, elems)
  24. right = obj.property.name
  25. if left == 'req.body' and right not in elems:
  26. elems.append(right)
  27. return '{}.{}'.format(left, right)
  28. elif obj.type == 'VariableDeclaration':
  29. for s in obj.declarations:
  30. get_req_body_elems(s, elems)
  31. elif obj.type == 'VariableDeclarator':
  32. if obj.id.type == 'ObjectPattern':
  33. # get_req_body_elems() can't be called directly here:
  34. # const {isAdmin, isNoComments, isCommentOnly} = req.body;
  35. right = get_req_body_elems(obj.init, elems)
  36. if right == 'req.body':
  37. for p in obj.id.properties:
  38. name = p.key.name
  39. if name not in elems:
  40. elems.append(name)
  41. else:
  42. get_req_body_elems(obj.init, elems)
  43. elif obj.type == 'Property':
  44. get_req_body_elems(obj.value, elems)
  45. elif obj.type == 'ObjectExpression':
  46. for s in obj.properties:
  47. get_req_body_elems(s, elems)
  48. elif obj.type == 'CallExpression':
  49. for s in obj.arguments:
  50. get_req_body_elems(s, elems)
  51. elif obj.type == 'ArrayExpression':
  52. for s in obj.elements:
  53. get_req_body_elems(s, elems)
  54. elif obj.type == 'IfStatement':
  55. get_req_body_elems(obj.test, elems)
  56. if obj.consequent is not None:
  57. get_req_body_elems(obj.consequent, elems)
  58. if obj.alternate is not None:
  59. get_req_body_elems(obj.alternate, elems)
  60. elif obj.type in ('LogicalExpression', 'BinaryExpression', 'AssignmentExpression'):
  61. get_req_body_elems(obj.left, elems)
  62. get_req_body_elems(obj.right, elems)
  63. elif obj.type in ('ReturnStatement', 'UnaryExpression'):
  64. get_req_body_elems(obj.argument, elems)
  65. elif obj.type == 'Literal':
  66. pass
  67. elif obj.type == 'Identifier':
  68. return obj.name
  69. elif obj.type == 'FunctionDeclaration':
  70. pass
  71. else:
  72. print(obj)
  73. return ''
  74. def cleanup_jsdocs(jsdoc):
  75. # remove leading spaces before the first '*'
  76. doc = [s.lstrip() for s in jsdoc.value.split('\n')]
  77. # remove leading stars
  78. doc = [s.lstrip('*') for s in doc]
  79. # remove leading empty lines
  80. while len(doc) and not doc[0].strip():
  81. doc.pop(0)
  82. # remove terminating empty lines
  83. while len(doc) and not doc[-1].strip():
  84. doc.pop(-1)
  85. return doc
  86. class JS2jsonDecoder(json.JSONDecoder):
  87. def decode(self, s):
  88. result = super().decode(s) # result = super(Decoder, self).decode(s) for Python 2.x
  89. return self._decode(result)
  90. def _decode(self, o):
  91. if isinstance(o, str) or isinstance(o, unicode):
  92. try:
  93. return int(o)
  94. except ValueError:
  95. return o
  96. elif isinstance(o, dict):
  97. return {k: self._decode(v) for k, v in o.items()}
  98. elif isinstance(o, list):
  99. return [self._decode(v) for v in o]
  100. else:
  101. return o
  102. def load_return_type_jsdoc_json(data):
  103. regex_replace = [(r'\n', r' '), # replace new lines by spaces
  104. (r'([\{\s,])(\w+)(:)', r'\1"\2"\3'), # insert double quotes in keys
  105. (r'(:)\s*([^:\},\]]+)\s*([\},\]])', r'\1"\2"\3'), # insert double quotes in values
  106. (r'(\[)\s*([^{].+)\s*(\])', r'\1"\2"\3'), # insert double quotes in array items
  107. (r'^\s*([^\[{].+)\s*', r'"\1"')] # insert double quotes in single item
  108. for r, s in regex_replace:
  109. data = re.sub(r, s, data)
  110. return json.loads(data)
  111. class EntryPoint(object):
  112. def __init__(self, schema, statements):
  113. self.schema = schema
  114. self.method, self._path, self.body = statements
  115. self._jsdoc = None
  116. self._doc = {}
  117. self._raw_doc = None
  118. self.path = self.compute_path()
  119. self.method_name = self.method.value.lower()
  120. self.body_params = []
  121. if self.method_name in ('post', 'put'):
  122. get_req_body_elems(self.body, self.body_params)
  123. # replace the :parameter in path by {parameter}
  124. self.url = re.sub(r':([^/]*)Id', r'{\1}', self.path)
  125. self.url = re.sub(r':([^/]*)', r'{\1}', self.url)
  126. # reduce the api name
  127. # get_boards_board_cards() should be get_board_cards()
  128. tokens = self.url.split('/')
  129. reduced_function_name = []
  130. for i, token in enumerate(tokens):
  131. if token in ('api'):
  132. continue
  133. if (i < len(tokens) - 1 and # not the last item
  134. tokens[i + 1].startswith('{')): # and the next token is a parameter
  135. continue
  136. reduced_function_name.append(token.strip('{}'))
  137. self.reduced_function_name = '_'.join(reduced_function_name)
  138. # mark the schema as used
  139. schema.used = True
  140. def compute_path(self):
  141. return self._path.value.rstrip('/')
  142. def log(self, message, level):
  143. if self._raw_doc is None:
  144. logger.log(level, 'in {},'.format(self.schema.name))
  145. logger.log(level, message)
  146. return
  147. logger.log(level, 'in {}, lines {}-{}'.format(self.schema.name,
  148. self._raw_doc.loc.start.line,
  149. self._raw_doc.loc.end.line))
  150. logger.log(level, self._raw_doc.value)
  151. logger.log(level, message)
  152. def error(self, message):
  153. return self.log(message, logging.ERROR)
  154. def warn(self, message):
  155. return self.log(message, logging.WARNING)
  156. def info(self, message):
  157. return self.log(message, logging.INFO)
  158. @property
  159. def doc(self):
  160. return self._doc
  161. @doc.setter
  162. def doc(self, doc):
  163. '''Parse the JSDoc attached to an entry point.
  164. `jsdoc` will not get these right as they are not attached to a method.
  165. So instead, we do our custom parsing here (yes, subject to errors).
  166. The expected format is the following (empty lines between entries
  167. are ignored):
  168. /**
  169. * @operation name_of_entry_point
  170. * @tag: a_tag_to_add
  171. * @tag: an_other_tag_to_add
  172. * @summary A nice summary, better in one line.
  173. *
  174. * @description This is a quite long description.
  175. * We can use *mardown* as the final rendering is done
  176. * by slate.
  177. *
  178. * indentation doesn't matter.
  179. *
  180. * @param param_0 description of param 0
  181. * @param {string} param_1 we can also put the type of the parameter
  182. * before its name, like in JSDoc
  183. * @param {boolean} [param_2] we can also tell if the parameter is
  184. * optional by adding square brackets around its name
  185. *
  186. * @return Documents a return value
  187. */
  188. Notes:
  189. - name_of_entry_point will be referenced in the ToC of the generated
  190. document. This is also the operationId used in the resulting openapi
  191. file. It needs to be uniq in the namesapce (the current schema.js
  192. file)
  193. - tags are appended to the current Schema attached to the file
  194. '''
  195. self._raw_doc = doc
  196. self._jsdoc = cleanup_jsdocs(doc)
  197. def store_tag(tag, data):
  198. # check that there is something to store first
  199. if not data.strip():
  200. return
  201. # remove terminating whitespaces and empty lines
  202. data = data.rstrip()
  203. # parameters are handled specially
  204. if tag == 'param':
  205. if 'params' not in self._doc:
  206. self._doc['params'] = {}
  207. params = self._doc['params']
  208. param_type = None
  209. try:
  210. name, desc = data.split(maxsplit=1)
  211. except ValueError:
  212. desc = ''
  213. if name.startswith('{'):
  214. param_type = name.strip('{}')
  215. if param_type not in ['string', 'number', 'boolean', 'integer', 'array', 'file']:
  216. self.warn('unknown type {}\n allowed values: string, number, boolean, integer, array, file'.format(param_type))
  217. try:
  218. name, desc = desc.split(maxsplit=1)
  219. except ValueError:
  220. desc = ''
  221. optional = name.startswith('[') and name.endswith(']')
  222. if optional:
  223. name = name[1:-1]
  224. # we should not have 2 identical parameter names
  225. if tag in params:
  226. self.warn('overwriting parameter {}'.format(name))
  227. params[name] = (param_type, optional, desc)
  228. if name.endswith('Id'):
  229. # we strip out the 'Id' from the form parameters, we need
  230. # to keep the actual description around
  231. name = name[:-2]
  232. if name not in params:
  233. params[name] = (param_type, optional, desc)
  234. return
  235. # 'tag' can be set several times
  236. if tag == 'tag':
  237. if tag not in self._doc:
  238. self._doc[tag] = []
  239. self._doc[tag].append(data)
  240. return
  241. # 'return' tag is json
  242. if tag == 'return_type':
  243. try:
  244. data = load_return_type_jsdoc_json(data)
  245. except json.decoder.JSONDecodeError:
  246. pass
  247. # we should not have 2 identical tags but @param or @tag
  248. if tag in self._doc:
  249. self.warn('overwriting tag {}'.format(tag))
  250. self._doc[tag] = data
  251. # reset the current doc fields
  252. self._doc = {}
  253. # first item is supposed to be the description
  254. current_tag = 'description'
  255. current_data = ''
  256. for line in self._jsdoc:
  257. if line.lstrip().startswith('@'):
  258. tag, data = line.lstrip().split(maxsplit=1)
  259. if tag in ['@operation', '@summary', '@description', '@param', '@return_type', '@tag']:
  260. # store the current data
  261. store_tag(current_tag, current_data)
  262. current_tag = tag.lstrip('@')
  263. current_data = ''
  264. line = data
  265. else:
  266. self.info('Unknown tag {}, ignoring'.format(tag))
  267. current_data += line + '\n'
  268. store_tag(current_tag, current_data)
  269. @property
  270. def summary(self):
  271. if 'summary' in self._doc:
  272. # new lines are not allowed
  273. return self._doc['summary'].replace('\n', ' ')
  274. return None
  275. def doc_param(self, name):
  276. if 'params' in self._doc and name in self._doc['params']:
  277. return self._doc['params'][name]
  278. return None, None, None
  279. def print_openapi_param(self, name, indent):
  280. ptype, poptional, pdesc = self.doc_param(name)
  281. if pdesc is not None:
  282. print('{}description: |'.format(' ' * indent))
  283. print('{}{}'.format(' ' * (indent + 2), pdesc))
  284. else:
  285. print('{}description: the {} value'.format(' ' * indent, name))
  286. if ptype is not None:
  287. print('{}type: {}'.format(' ' * indent, ptype))
  288. else:
  289. print('{}type: string'.format(' ' * indent))
  290. if poptional:
  291. print('{}required: false'.format(' ' * indent))
  292. else:
  293. print('{}required: true'.format(' ' * indent))
  294. @property
  295. def operationId(self):
  296. if 'operation' in self._doc:
  297. return self._doc['operation']
  298. return '{}_{}'.format(self.method_name, self.reduced_function_name)
  299. @property
  300. def description(self):
  301. if 'description' in self._doc:
  302. return self._doc['description']
  303. return None
  304. @property
  305. def returns(self):
  306. if 'return_type' in self._doc:
  307. return self._doc['return_type']
  308. return None
  309. @property
  310. def tags(self):
  311. tags = []
  312. if self.schema.fields is not None:
  313. tags.append(self.schema.name)
  314. if 'tag' in self._doc:
  315. tags.extend(self._doc['tag'])
  316. return tags
  317. def print_openapi_return(self, obj, indent):
  318. if isinstance(obj, dict):
  319. print('{}type: object'.format(' ' * indent))
  320. print('{}properties:'.format(' ' * indent))
  321. for k, v in obj.items():
  322. print('{}{}:'.format(' ' * (indent + 2), k))
  323. self.print_openapi_return(v, indent + 4)
  324. elif isinstance(obj, list):
  325. if len(obj) > 1:
  326. self.error('Error while parsing @return tag, an array should have only one type')
  327. print('{}type: array'.format(' ' * indent))
  328. print('{}items:'.format(' ' * indent))
  329. self.print_openapi_return(obj[0], indent + 2)
  330. elif isinstance(obj, str) or isinstance(obj, unicode):
  331. rtype = 'type: ' + obj
  332. if obj == self.schema.name:
  333. rtype = '$ref: "#/definitions/{}"'.format(obj)
  334. print('{}{}'.format(' ' * indent, rtype))
  335. def print_openapi(self):
  336. parameters = [token[1:-2] if token.endswith('Id') else token[1:]
  337. for token in self.path.split('/')
  338. if token.startswith(':')]
  339. print(' {}:'.format(self.method_name))
  340. print(' operationId: {}'.format(self.operationId))
  341. if self.summary is not None:
  342. print(' summary: {}'.format(self.summary))
  343. if self.description is not None:
  344. print(' description: |')
  345. for line in self.description.split('\n'):
  346. if line.strip():
  347. print(' {}'.format(line))
  348. else:
  349. print('')
  350. if len(self.tags) > 0:
  351. print(' tags:')
  352. for tag in self.tags:
  353. print(' - {}'.format(tag))
  354. # export the parameters
  355. if self.method_name in ('post', 'put'):
  356. print(''' consumes:
  357. - multipart/form-data
  358. - application/json''')
  359. if len(parameters) > 0 or self.method_name in ('post', 'put'):
  360. print(' parameters:')
  361. if self.method_name in ('post', 'put'):
  362. for f in self.body_params:
  363. print(''' - name: {}
  364. in: formData'''.format(f))
  365. self.print_openapi_param(f, 10)
  366. for p in parameters:
  367. if p in self.body_params:
  368. self.error(' '.join((p, self.path, self.method_name)))
  369. print(''' - name: {}
  370. in: path'''.format(p))
  371. self.print_openapi_param(p, 10)
  372. print(''' produces:
  373. - application/json
  374. security:
  375. - UserSecurity: []
  376. responses:
  377. '200':
  378. description: |-
  379. 200 response''')
  380. if self.returns is not None:
  381. print(' schema:')
  382. self.print_openapi_return(self.returns, 12)
  383. class SchemaProperty(object):
  384. def __init__(self, statement, schema, context):
  385. self.schema = schema
  386. self.statement = statement
  387. self.name = statement.key.name or statement.key.value
  388. self.type = 'object'
  389. self.blackbox = False
  390. self.required = True
  391. for p in statement.value.properties:
  392. try:
  393. if p.key.name == 'type':
  394. if p.value.type == 'Identifier':
  395. self.type = p.value.name.lower()
  396. elif p.value.type == 'ArrayExpression':
  397. self.type = 'array'
  398. self.elements = [e.name.lower() for e in p.value.elements]
  399. elif p.key.name == 'allowedValues':
  400. self.type = 'enum'
  401. self.enum = [e.value.lower() for e in p.value.elements]
  402. elif p.key.name == 'blackbox':
  403. self.blackbox = True
  404. elif p.key.name == 'optional' and p.value.value:
  405. self.required = False
  406. except Exception:
  407. input = ''
  408. for line in range(p.loc.start.line - err_context, p.loc.end.line + 1 + err_context):
  409. if line < p.loc.start.line or line > p.loc.end.line:
  410. input += '. '
  411. else:
  412. input += '>>'
  413. input += context.text_at(line, line)
  414. input = ''.join(input)
  415. logger.error('{}:{}-{} can not parse {}:\n{}'.format(context.path,
  416. p.loc.start.line,
  417. p.loc.end.line,
  418. p.type,
  419. input))
  420. logger.error('esprima tree:\n{}'.format(p))
  421. logger.error(traceback.format_exc())
  422. sys.exit(1)
  423. self._doc = None
  424. self._raw_doc = None
  425. @property
  426. def doc(self):
  427. return self._doc
  428. @doc.setter
  429. def doc(self, jsdoc):
  430. self._raw_doc = jsdoc
  431. self._doc = cleanup_jsdocs(jsdoc)
  432. def process_jsdocs(self, jsdocs):
  433. start = self.statement.key.loc.start.line
  434. for index, doc in enumerate(jsdocs):
  435. if start + 1 == doc.loc.start.line:
  436. self.doc = doc
  437. jsdocs.pop(index)
  438. return
  439. def __repr__(self):
  440. return 'SchemaProperty({}{}, {})'.format(self.name,
  441. '*' if self.required else '',
  442. self.doc)
  443. def print_openapi(self, indent, current_schema, required_properties):
  444. schema_name = self.schema.name
  445. name = self.name
  446. # deal with subschemas
  447. if '.' in name:
  448. if name.endswith('$'):
  449. # reference in reference
  450. subschema = ''.join([n.capitalize() for n in self.name.split('.')[:-1]])
  451. subschema = self.schema.name + subschema
  452. if current_schema != subschema:
  453. if required_properties is not None and required_properties:
  454. print(' required:')
  455. for f in required_properties:
  456. print(' - {}'.format(f))
  457. required_properties.clear()
  458. print(''' {}:
  459. type: object'''.format(subschema))
  460. return current_schema
  461. subschema = name.split('.')[0]
  462. schema_name = self.schema.name + subschema.capitalize()
  463. name = name.split('.')[-1]
  464. if current_schema != schema_name:
  465. if required_properties is not None and required_properties:
  466. print(' required:')
  467. for f in required_properties:
  468. print(' - {}'.format(f))
  469. required_properties.clear()
  470. print(''' {}:
  471. type: object
  472. properties:'''.format(schema_name))
  473. if required_properties is not None and self.required:
  474. required_properties.append(name)
  475. print('{}{}:'.format(' ' * indent, name))
  476. if self.doc is not None:
  477. print('{} description: |'.format(' ' * indent))
  478. for line in self.doc:
  479. if line.strip():
  480. print('{} {}'.format(' ' * indent, line))
  481. else:
  482. print('')
  483. ptype = self.type
  484. if ptype in ('enum', 'date'):
  485. ptype = 'string'
  486. if ptype != 'object':
  487. print('{} type: {}'.format(' ' * indent, ptype))
  488. if self.type == 'array':
  489. print('{} items:'.format(' ' * indent))
  490. for elem in self.elements:
  491. if elem == 'object':
  492. print('{} $ref: "#/definitions/{}"'.format(' ' * indent, schema_name + name.capitalize()))
  493. else:
  494. print('{} type: {}'.format(' ' * indent, elem))
  495. if not self.required:
  496. print('{} x-nullable: true'.format(' ' * indent))
  497. elif self.type == 'object':
  498. if self.blackbox:
  499. print('{} type: object'.format(' ' * indent))
  500. else:
  501. print('{} $ref: "#/definitions/{}"'.format(' ' * indent, schema_name + name.capitalize()))
  502. elif self.type == 'enum':
  503. print('{} enum:'.format(' ' * indent))
  504. for enum in self.enum:
  505. print('{} - {}'.format(' ' * indent, enum))
  506. if '.' not in self.name and not self.required:
  507. print('{} x-nullable: true'.format(' ' * indent))
  508. return schema_name
  509. class Schemas(object):
  510. def __init__(self, context, data=None, jsdocs=None, name=None):
  511. self.name = name
  512. self._data = data
  513. self.fields = None
  514. self.used = False
  515. if data is not None:
  516. if self.name is None:
  517. self.name = data.expression.callee.object.name
  518. content = data.expression.arguments[0].arguments[0]
  519. self.fields = [SchemaProperty(p, self, context) for p in content.properties]
  520. self._doc = None
  521. self._raw_doc = None
  522. if jsdocs is not None:
  523. self.process_jsdocs(jsdocs)
  524. @property
  525. def doc(self):
  526. if self._doc is None:
  527. return None
  528. return ' '.join(self._doc)
  529. @doc.setter
  530. def doc(self, jsdoc):
  531. self._raw_doc = jsdoc
  532. self._doc = cleanup_jsdocs(jsdoc)
  533. def process_jsdocs(self, jsdocs):
  534. start = self._data.loc.start.line
  535. end = self._data.loc.end.line
  536. for doc in jsdocs:
  537. if doc.loc.end.line + 1 == start:
  538. self.doc = doc
  539. docs = [doc
  540. for doc in jsdocs
  541. if doc.loc.start.line >= start and doc.loc.end.line <= end]
  542. for field in self.fields:
  543. field.process_jsdocs(docs)
  544. def print_openapi(self):
  545. # empty schemas are skipped
  546. if self.fields is None:
  547. return
  548. print(' {}:'.format(self.name))
  549. print(' type: object')
  550. if self.doc is not None:
  551. print(' description: {}'.format(self.doc))
  552. print(' properties:')
  553. # first print out the object itself
  554. properties = [field for field in self.fields if '.' not in field.name]
  555. for prop in properties:
  556. prop.print_openapi(6, None, None)
  557. required_properties = [f.name for f in properties if f.required]
  558. if required_properties:
  559. print(' required:')
  560. for f in required_properties:
  561. print(' - {}'.format(f))
  562. # then print the references
  563. current = None
  564. required_properties = []
  565. properties = [f for f in self.fields if '.' in f.name and not f.name.endswith('$')]
  566. for prop in properties:
  567. current = prop.print_openapi(6, current, required_properties)
  568. if required_properties:
  569. print(' required:')
  570. for f in required_properties:
  571. print(' - {}'.format(f))
  572. required_properties = []
  573. # then print the references in the references
  574. for prop in [f for f in self.fields if '.' in f.name and f.name.endswith('$')]:
  575. current = prop.print_openapi(6, current, required_properties)
  576. if required_properties:
  577. print(' required:')
  578. for f in required_properties:
  579. print(' - {}'.format(f))
  580. class Context(object):
  581. def __init__(self, path):
  582. self.path = path
  583. with open(path) as f:
  584. self._txt = f.readlines()
  585. data = ''.join(self._txt)
  586. self.program = esprima.parseModule(data,
  587. options={
  588. 'comment': True,
  589. 'loc': True
  590. })
  591. def txt_for(self, statement):
  592. return self.text_at(statement.loc.start.line, statement.loc.end.line)
  593. def text_at(self, begin, end):
  594. return ''.join(self._txt[begin - 1:end])
  595. def parse_schemas(schemas_dir):
  596. schemas = {}
  597. entry_points = []
  598. for root, dirs, files in os.walk(schemas_dir):
  599. files.sort()
  600. for filename in files:
  601. path = os.path.join(root, filename)
  602. try:
  603. # if the file failed, it's likely it doesn't contain a schema
  604. context = Context(path)
  605. except:
  606. continue
  607. program = context.program
  608. current_schema = None
  609. jsdocs = [c for c in program.comments
  610. if c.type == 'Block' and c.value.startswith('*\n')]
  611. try:
  612. for statement in program.body:
  613. # find the '<ITEM>.attachSchema(new SimpleSchema(<data>)'
  614. # those are the schemas
  615. if (statement.type == 'ExpressionStatement' and
  616. statement.expression.callee is not None and
  617. statement.expression.callee.property is not None and
  618. statement.expression.callee.property.name == 'attachSchema' and
  619. statement.expression.arguments[0].type == 'NewExpression' and
  620. statement.expression.arguments[0].callee.name == 'SimpleSchema'):
  621. schema = Schemas(context, statement, jsdocs)
  622. current_schema = schema.name
  623. schemas[current_schema] = schema
  624. # find all the 'if (Meteor.isServer) { JsonRoutes.add('
  625. # those are the entry points of the API
  626. elif (statement.type == 'IfStatement' and
  627. statement.test.type == 'MemberExpression' and
  628. statement.test.object.name == 'Meteor' and
  629. statement.test.property.name == 'isServer'):
  630. data = [s.expression.arguments
  631. for s in statement.consequent.body
  632. if (s.type == 'ExpressionStatement' and
  633. s.expression.type == 'CallExpression' and
  634. s.expression.callee.object.name == 'JsonRoutes')]
  635. # we found at least one entry point, keep them
  636. if len(data) > 0:
  637. if current_schema is None:
  638. current_schema = filename
  639. schemas[current_schema] = Schemas(context, name=current_schema)
  640. schema_entry_points = [EntryPoint(schemas[current_schema], d)
  641. for d in data]
  642. entry_points.extend(schema_entry_points)
  643. # try to match JSDoc to the operations
  644. for entry_point in schema_entry_points:
  645. operation = entry_point.method # POST/GET/PUT/DELETE
  646. jsdoc = [j for j in jsdocs
  647. if j.loc.end.line + 1 == operation.loc.start.line]
  648. if bool(jsdoc):
  649. entry_point.doc = jsdoc[0]
  650. except TypeError:
  651. logger.warning(context.txt_for(statement))
  652. logger.error('{}:{}-{} can not parse {}'.format(path,
  653. statement.loc.start.line,
  654. statement.loc.end.line,
  655. statement.type))
  656. raise
  657. return schemas, entry_points
  658. def generate_openapi(schemas, entry_points, version):
  659. print('''swagger: '2.0'
  660. info:
  661. title: Wekan REST API
  662. version: {0}
  663. description: |
  664. The REST API allows you to control and extend Wekan with ease.
  665. 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.
  666. > 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.
  667. # Production Security Concerns
  668. 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:
  669. * Only call via HTTPS
  670. * Implement a timed authorization token expiration strategy
  671. * Ensure the calling user only has permissions for what they are calling and no more
  672. schemes:
  673. - http
  674. securityDefinitions:
  675. UserSecurity:
  676. type: apiKey
  677. in: header
  678. name: Authorization
  679. paths:
  680. /users/login:
  681. post:
  682. operationId: login
  683. summary: Login with REST API
  684. consumes:
  685. - application/x-www-form-urlencoded
  686. - application/json
  687. tags:
  688. - Login
  689. parameters:
  690. - name: username
  691. in: formData
  692. required: true
  693. description: |
  694. Your username
  695. type: string
  696. - name: password
  697. in: formData
  698. required: true
  699. description: |
  700. Your password
  701. type: string
  702. format: password
  703. responses:
  704. 200:
  705. description: |-
  706. Successful authentication
  707. schema:
  708. items:
  709. properties:
  710. id:
  711. type: string
  712. token:
  713. type: string
  714. tokenExpires:
  715. type: string
  716. 400:
  717. description: |
  718. Error in authentication
  719. schema:
  720. items:
  721. properties:
  722. error:
  723. type: number
  724. reason:
  725. type: string
  726. default:
  727. description: |
  728. Error in authentication
  729. /users/register:
  730. post:
  731. operationId: register
  732. summary: Register with REST API
  733. description: |
  734. Notes:
  735. - You will need to provide the token for any of the authenticated methods.
  736. consumes:
  737. - application/x-www-form-urlencoded
  738. - application/json
  739. tags:
  740. - Login
  741. parameters:
  742. - name: username
  743. in: formData
  744. required: true
  745. description: |
  746. Your username
  747. type: string
  748. - name: password
  749. in: formData
  750. required: true
  751. description: |
  752. Your password
  753. type: string
  754. format: password
  755. - name: email
  756. in: formData
  757. required: true
  758. description: |
  759. Your email
  760. type: string
  761. responses:
  762. 200:
  763. description: |-
  764. Successful registration
  765. schema:
  766. items:
  767. properties:
  768. id:
  769. type: string
  770. token:
  771. type: string
  772. tokenExpires:
  773. type: string
  774. 400:
  775. description: |
  776. Error in registration
  777. schema:
  778. items:
  779. properties:
  780. error:
  781. type: number
  782. reason:
  783. type: string
  784. default:
  785. description: |
  786. Error in registration
  787. '''.format(version))
  788. # GET and POST on the same path are valid, we need to reshuffle the paths
  789. # with the path as the sorting key
  790. methods = {}
  791. for ep in entry_points:
  792. if ep.path not in methods:
  793. methods[ep.path] = []
  794. methods[ep.path].append(ep)
  795. sorted_paths = list(methods.keys())
  796. sorted_paths.sort()
  797. for path in sorted_paths:
  798. print(' {}:'.format(methods[path][0].url))
  799. for ep in methods[path]:
  800. ep.print_openapi()
  801. print('definitions:')
  802. for schema in schemas.values():
  803. # do not export the objects if there is no API attached
  804. if not schema.used:
  805. continue
  806. schema.print_openapi()
  807. def main():
  808. parser = argparse.ArgumentParser(description='Generate an OpenAPI 2.0 from the given JS schemas.')
  809. script_dir = os.path.dirname(os.path.realpath(__file__))
  810. parser.add_argument('--release', default='git-master', nargs=1,
  811. help='the current version of the API, can be retrieved by running `git describe --tags --abbrev=0`')
  812. parser.add_argument('dir', default='{}/../models'.format(script_dir), nargs='?',
  813. help='the directory where to look for schemas')
  814. args = parser.parse_args()
  815. schemas, entry_points = parse_schemas(args.dir)
  816. generate_openapi(schemas, entry_points, args.release[0])
  817. if __name__ == '__main__':
  818. main()