ari:Add application/json parameter support

The patch allows ARI to parse request parameters from an incoming JSON
request body, instead of requiring the request to come in as query
parameters (which is just weird for POST and DELETE) or form
parameters (which is okay, but a bit asymmetric given that all of our
responses are JSON).

For any operation that does _not_ have a parameter defined of type
body (i.e. "paramType": "body" in the API declaration), if a request
provides a request body with a Content type of "application/json", the
provided JSON document is parsed and searched for parameters.

The expected fields in the provided JSON document should match the
query parameters defined for the operation. If the parameter has
'allowMultiple' set, then the field in the JSON document may
optionally be an array of values.

(closes issue ASTERISK-22685)
Review: https://reviewboard.asterisk.org/r/2994/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@403177 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
David M. Lee
2013-11-27 15:48:39 +00:00
parent fd33969240
commit fccb427c88
18 changed files with 1170 additions and 29 deletions

View File

@@ -146,6 +146,15 @@ class AsteriskProcessor(SwaggerPostProcessor):
'boolean': 'ast_true',
}
#: JSON conversion functions
json_convert_mapping = {
'string': 'ast_json_string_get',
'int': 'ast_json_integer_get',
'long': 'ast_json_integer_get',
'double': 'ast_json_real_get',
'boolean': 'ast_json_is_true',
}
def __init__(self, wiki_prefix):
self.wiki_prefix = wiki_prefix
@@ -190,15 +199,22 @@ class AsteriskProcessor(SwaggerPostProcessor):
raise SwaggerError("Summary should end with .", context)
operation.wiki_summary = wikify(operation.summary or "")
operation.wiki_notes = wikify(operation.notes or "")
operation.parse_body = (operation.body_parameter or operation.has_query_parameters) and True
def process_parameter(self, parameter, context):
if not parameter.data_type in self.type_mapping:
raise SwaggerError(
"Invalid parameter type %s" % parameter.data_type, context)
if parameter.param_type == 'body':
parameter.c_data_type = 'struct ast_json *'
else:
if not parameter.data_type in self.type_mapping:
raise SwaggerError(
"Invalid parameter type %s" % parameter.data_type, context)
# Type conversions
parameter.c_data_type = self.type_mapping[parameter.data_type]
parameter.c_convert = self.convert_mapping[parameter.data_type]
parameter.json_convert = self.json_convert_mapping[parameter.data_type]
# Parameter names are camelcase, Asterisk convention is snake case
parameter.c_name = snakify(parameter.name)
parameter.c_data_type = self.type_mapping[parameter.data_type]
parameter.c_convert = self.convert_mapping[parameter.data_type]
# You shouldn't put a space between 'char *' and the variable
if parameter.c_data_type.endswith('*'):
parameter.c_space = ''