generate_openapi.py 36 KB

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