[CORD-1796] Generate swagger spec from xproto
Change-Id: Ia5697b386b216d677bef73f08c36c022e28038ca
diff --git a/lib/xos-genx/xosgenx/jinja2_extensions/base.py b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
index 84f0a97..431de3f 100644
--- a/lib/xos-genx/xosgenx/jinja2_extensions/base.py
+++ b/lib/xos-genx/xosgenx/jinja2_extensions/base.py
@@ -247,3 +247,35 @@
return "boolean"
else:
return type
+
+def xproto_type_to_swagger_type(f):
+ try:
+ content_type = f['options']['content_type']
+ content_type = eval(content_type)
+ except:
+ content_type = None
+ pass
+
+ if 'choices' in f['options']:
+ return 'string'
+ elif content_type == 'date':
+ return 'string'
+ elif f['type'] == 'bool':
+ return 'boolean'
+ elif f['type'] == 'string':
+ return 'string'
+ elif f['type'] in ['int','uint32','int32'] or 'link' in f:
+ return 'integer'
+ elif f['type'] in ['double','float']:
+ return 'string'
+
+def xproto_field_to_swagger_enum(f):
+ if 'choices' in f['options']:
+ list = []
+
+ for c in eval(xproto_unquote(f['options']['choices'])):
+ list.append(c[0])
+
+ return list
+ else:
+ return False
\ No newline at end of file
diff --git a/lib/xos-genx/xosgenx/targets/swagger.xtarget b/lib/xos-genx/xosgenx/targets/swagger.xtarget
new file mode 100644
index 0000000..3d089b9
--- /dev/null
+++ b/lib/xos-genx/xosgenx/targets/swagger.xtarget
@@ -0,0 +1,135 @@
+swagger: "2.0"
+info:
+ title: "XOS REST APIs"
+ description: "XOS - REST API Docs"
+ version: "1.0.0"
+ contact:
+ email: "cord-dev@opencord.org"
+
+{#tags:#}
+{#{%- for object in proto.messages %}#}
+{#- name: "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"#}
+{# description: "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }} Models"#}
+{# externalDocs:#}
+{# description: "Find out more"#}
+{# url: "https://guide.opencord.org/xos/core_models.html"#}
+{#{% endfor %}#}
+
+paths:
+{%- for object in proto.messages %}
+ /xosapi/v1/{{ xproto_unquote(xproto_first_non_empty([object.options.name, object.options.app_label, options.name, context.app_label])) }}/{{ xproto_pluralize(object) | lower }}/:
+ get:
+ tags:
+ - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+ summary: "List {{xproto_pluralize(object)}}"
+ responses:
+ 200:
+ description: OK
+ 401:
+ description: "Unauthorized"
+ 403:
+ description: "Forbidden"
+ 500:
+ description: "Internal Server Error"
+ post:
+ tags:
+ - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+ summary: "Create {{object.name}}"
+ parameters:
+ - in: "body"
+ name: "body"
+ description: "{{object.name}} model properties"
+ required: true
+ schema:
+ $ref: "#/definitions/{{object.name}}"
+ responses:
+ 200:
+ description: OK
+ 401:
+ description: "Unauthorized"
+ 403:
+ description: "Forbidden"
+ 500:
+ description: "Internal Server Error"
+ /xosapi/v1/{{ xproto_unquote(xproto_first_non_empty([object.options.name, object.options.app_label, options.name, context.app_label])) }}/{{ xproto_pluralize(object) | lower }}/{id}:
+ get:
+ tags:
+ - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+ summary: "Get {{object.name}}"
+ parameters:
+ - in: "path"
+ name: "id"
+ description: "ID of {{ object.name }} to return"
+ required: true
+ type: "integer"
+ format: "int64"
+ responses:
+ 200:
+ description: OK
+ 401:
+ description: "Unauthorized"
+ 403:
+ description: "Forbidden"
+ 500:
+ description: "Internal Server Error"
+ put:
+ tags:
+ - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+ summary: "Get {{object.name}}"
+ parameters:
+ - in: "path"
+ name: "id"
+ description: "ID of {{ object.name }} to return"
+ required: true
+ type: "integer"
+ format: "int64"
+ responses:
+ 200:
+ description: OK
+ 401:
+ description: "Unauthorized"
+ 403:
+ description: "Forbidden"
+ 500:
+ description: "Internal Server Error"
+ delete:
+ tags:
+ - "{{ xproto_unquote(xproto_first_non_empty([object.options.app_label, object.options.name])) }}"
+ summary: "Delete {{object.name}}"
+ parameters:
+ - in: "path"
+ name: "id"
+ description: "ID of {{ object.name }} to return"
+ required: true
+ type: "integer"
+ format: "int64"
+ responses:
+ 200:
+ description: OK
+ 401:
+ description: "Unauthorized"
+ 403:
+ description: "Forbidden"
+ 500:
+ description: "Internal Server Error"
+{% endfor %}
+
+definitions:
+{%- for object in proto.messages %}
+ {{ object.name }}:
+ type: "object"
+ properties:
+{%- for f in object.fields %}
+ {{ f.name }}:
+ type: {{ xproto_type_to_swagger_type(f) }}
+{%- if xproto_field_to_swagger_enum(f) %}
+ enum:
+{%- for e in xproto_field_to_swagger_enum(f) %}
+ - {{ e }}
+{%- endfor %}
+{%- endif %}
+{%- if f.options.help_text %}
+ description: "{{ xproto_unquote(f.options.help_text) }}"
+{% endif %}
+{%- endfor %}
+{% endfor %}