Fix request example field

-Updated swagger_to_rst to read request example field.
-Updated wadl_to_swagger_valid to generate request example
 in schema property field.  Removed instances of
 x-examples field.

Closes-Bug: #1535122

Change-Id: I5aa2a5d6eac67dcf20a54922555ed7d9325cfcb3
This commit is contained in:
Karen Bradshaw 2016-01-28 14:13:07 -05:00
parent 877a0877d3
commit d57fa5841d
3 changed files with 55 additions and 39 deletions

View File

@ -29,6 +29,18 @@ from jinja2 import Environment
log = logging.getLogger(__name__)
TMPL_TAG = """
{%- for tag in swagger.tags -%}
.. swagger:tag:: {{tag.name}}
:synopsis: {{tag.description}}
{% for line in tag['x-summary'].split('\n') %}
{{line}}
{%- endfor %}
{% endfor %}
"""
TMPL_API = """
{%- for path, methods in swagger['paths'].items() -%}
{%- for method_name, request in methods.items() -%}
@ -41,12 +53,6 @@ TMPL_API = """
{{line}}
{%- endfor %}
{%- endif %}
{% if request['x-examples']['application/json'] %}
:requestexample: {{version}}/examples/{{request['operationId']}}_req.json
{%- endif -%}
{% if request['x-examples']['text/plain'] %}
:requestexample: {{version}}/examples/{{request['operationId']}}_req.txt
{%- endif -%}
{% for status_code, response in request.responses.items() %}
{%- if response['examples']['application/json'] %}
:responseexample {{status_code}}: {{version}}/examples/{{request['operationId']}}_resp_{{status_code}}.json
@ -71,6 +77,18 @@ TMPL_API = """
{% if parameter.in == 'body' -%}
{% if parameter.schema %}
:requestschema: {{version}}/{{request['operationId']}}.json
{%- for id, schema in swagger['definitions'].items() -%}
{%- if id == request['operationId'] -%}
{%- if 'example' in schema -%}
{%- if schema['example']['application/json'] %}
:requestexample: {{version}}/examples/{{request['operationId']}}_req.json
{%- endif -%}
{%- if schema['example']['text/plain'] %}
:requestexample: {{version}}/examples/{{request['operationId']}}_req.txt
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- elif parameter.in == 'path' %}
{{ parameter|format_param('parameter') }}
@ -89,17 +107,6 @@ TMPL_API = """
{%- endfor %}
""" # noqa
TMPL_TAG = """
{%- for tag in swagger.tags -%}
.. swagger:tag:: {{tag.name}}
:synopsis: {{tag.description}}
{% for line in tag['x-summary'].split('\n') %}
{{line}}
{%- endfor %}
{% endfor %}
"""
environment = Environment()
@ -232,22 +239,6 @@ def write_examples(swagger, output_dir):
for paths in swagger['paths'].values():
for operation in paths.values():
if 'x-examples' in operation:
for mime, example in operation['x-examples'].items():
filename = '%s' % '_'.join(
[operation['operationId'], 'req'])
if mime == 'application/json':
filepath = path.join(full_path, filename + '.json')
log.info("Writing %s", filepath)
file = open(filepath, 'w')
json.dump(example, file, indent=2)
if mime == 'text/plain':
filepath = path.join(full_path, filename + '.txt')
log.info("Writing %s", filepath)
example = example.strip()
example = example + '\n'
file = open(filepath, 'w')
file.write(example)
for status_code, response in operation['responses'].items():
for mime, example in response['examples'].items():
filename = '%s' % '_'.join([operation['operationId'],
@ -266,6 +257,24 @@ def write_examples(swagger, output_dir):
file = open(filepath, 'w')
file.write(example)
for ids, schemas in swagger['definitions'].items():
if 'example' in schemas:
for mime, example in schemas['example'].items():
filename = '%s' % '_'.join(
[ids, 'req'])
if mime == 'application/json':
filepath = path.join(full_path, filename + '.json')
log.info("Writing %s", filepath)
file = open(filepath, 'w')
json.dump(example, file, indent=2)
if mime == 'text/plain':
filepath = path.join(full_path, filename + '.txt')
log.info("Writing %s", filepath)
example = example.strip()
example = example + '\n'
file = open(filepath, 'w')
file.write(example)
def main():
import argparse

View File

@ -521,6 +521,7 @@ class WADLHandler(xml.sax.ContentHandler):
self.apis = {}
self.current_api = None
self.schemas = {}
self.req_example_schemas = {}
# Resource Mapping
self.resource_map = {}
@ -648,7 +649,6 @@ class WADLHandler(xml.sax.ContentHandler):
self.current_api = {
'produces': set(),
'consumes': set(),
'x-examples': {},
'responses': {},
'parameters': {},
}
@ -665,7 +665,6 @@ class WADLHandler(xml.sax.ContentHandler):
'method': name,
'produces': set(),
'consumes': set(),
'x-examples': {},
'parameters': [{'in': "body",
'name': "body",
'description': "",
@ -766,8 +765,12 @@ class WADLHandler(xml.sax.ContentHandler):
response = self.current_api['responses'][status_code]
response['examples'][media_type] = sample
elif sample and type == 'request':
# Swagger: request examples added as extension
self.current_api['x-examples'][media_type] = sample
schema_name = self.current_api.get('operationId')
if schema_name and schema_name not in self.req_example_schemas:
self.req_example_schemas[schema_name] \
= {'example': {}}
req_schema = self.req_example_schemas[schema_name]
req_schema['example'][media_type] = sample
if name == 'response':
if 'status' not in attrs:
@ -818,6 +821,11 @@ class WADLHandler(xml.sax.ContentHandler):
if name not in schema_required:
schema_required.append(name)
schema_properties[parameter['name']] = parameter
# Request example
if schema_name in self.req_example_schemas:
schema = self.req_example_schemas[schema_name]
self.schemas[schema_name]['example'] = schema['example']
del parameter['required']
del parameter['name']
del parameter['in']

View File

@ -120,7 +120,7 @@ function migrate_docbkx {
fi
generate_all=
if [[ -z $docbkx2json && -z $wadl2swagger && -z $swagger2rst ]]; then
if [[ -z $docbkx2json && -z $wadl2swagger && -z $wadl2swaggervalid && -z $swagger2rst ]]; then
generate_all=1
fi
@ -141,7 +141,6 @@ function migrate_docbkx {
fi
}
if [ -z $docs_only ]; then
install_fairy_slipper
install_api_site