- Fixed Query and Headers parameters parsing
This commit is contained in:
Jakub Malek 2016-06-02 13:12:16 +02:00 committed by Jakub Malek
parent 9c8373aea2
commit 460f8130db
4 changed files with 40 additions and 10 deletions

View File

@ -26,7 +26,7 @@ namespace {{packageName}}.{{packageContext}}.Modules
{ {{#operation}}
{{httpMethod}}["{{path}}"] = parameters =>
{
{{#allParams}}{{#isBodyParam}}var {{paramName}} = this.Bind<{{&dataType}}>();{{/isBodyParam}}{{^isBodyParam}}{{#isEnum}}var {{paramName}} = Parameters.ValueOf<{{>innerApiEnumName}}?>(parameters, "{{paramName}}");{{/isEnum}}{{^isEnum}}var {{paramName}} = Parameters.ValueOf<{{&dataType}}>(parameters, "{{paramName}}");{{/isEnum}}{{#hasMore}}
{{#allParams}}{{#isBodyParam}}var {{paramName}} = this.Bind<{{&dataType}}>();{{/isBodyParam}}{{^isBodyParam}}{{#isEnum}}var {{paramName}} = Parameters.ValueOf<{{>innerApiEnumName}}?>({{>innerParameterValueOfArgs}});{{/isEnum}}{{^isEnum}}var {{paramName}} = Parameters.ValueOf<{{&dataType}}>({{>innerParameterValueOfArgs}});{{/isEnum}}{{#hasMore}}
{{/hasMore}}{{/isBodyParam}}{{/allParams}}{{#allParams}}{{#required}}
Preconditions.IsNotNull({{paramName}}, "Required parameter: '{{paramName}}' is missing at '{{operationId}}'");
{{/required}}{{/allParams}}

View File

@ -0,0 +1 @@
{{#isQueryParam}}ParameterType.Query{{/isQueryParam}}{{#isPathParam}}ParameterType.Path{{/isPathParam}}{{#isHeaderParam}}ParameterType.Header{{/isHeaderParam}}{{^isQueryParam}}{{^isPathParam}}{{^isHeaderParam}}ParameterType.Undefined{{/isHeaderParam}}{{/isPathParam}}{{/isQueryParam}}

View File

@ -0,0 +1 @@
parameters, Context.Request, "{{paramName}}", {{>innerParameterType}}

View File

@ -13,12 +13,12 @@ namespace {{packageName}}.{{packageContext}}.Utils
{
private static readonly IDictionary<Type, Func<Parameter, object>> Parsers = CreateParsers();
internal static TValue ValueOf<TValue>(dynamic parameters, string name)
internal static TValue ValueOf<TValue>(dynamic parameters, Request request, string name, ParameterType parameterType)
{
var valueType = typeof (TValue);
var valueUnderlyingType = Nullable.GetUnderlyingType(valueType);
var isNullable = default(TValue) == null;
string value = RawValue(parameters, name);
string value = RawValueOf(parameters, request, name, parameterType);
Preconditions.Evaluate(!string.IsNullOrEmpty(value) || isNullable, string.Format("Required parameter: '{0}' is missing", name));
if (value == null && isNullable)
{
@ -28,19 +28,31 @@ namespace {{packageName}}.{{packageContext}}.Utils
{
return EnumValueOf<TValue>(name, value);
}
return ValueOf<TValue>(parameters, name, value, valueType);
return ValueOf<TValue>(parameters, name, value, valueType, request, parameterType);
}
private static string RawValue(dynamic parameters, string name)
private static string RawValueOf(dynamic parameters, Request request, string name, ParameterType parameterType)
{
try
{
return parameters[name];
switch (parameterType)
{
case ParameterType.Query:
string querValue = request.Query[name];
return querValue;
case ParameterType.Path:
string pathValue = parameters[name];
return pathValue;
case ParameterType.Header:
var headerValue = request.Headers[name];
return headerValue != null ? string.Join(",", headerValue) : null;
}
}
catch (Exception e)
{
throw new InvalidOperationException(string.Format("Could not obtain value of '{0}' parameter", name), e);
}
throw new InvalidOperationException(string.Format("Parameter with type: {0} is not supported", parameterType));
}
private static TValue EnumValueOf<TValue>(string name, string value)
@ -60,14 +72,22 @@ namespace {{packageName}}.{{packageContext}}.Utils
name, value, value.ToComparable()));
}
private static TValue ValueOf<TValue>(dynamic parameters, string name, string value, Type valueType)
private static TValue ValueOf<TValue>(dynamic parameters, string name, string value, Type valueType, Request request, ParameterType parameterType)
{
var parser = Parsers.GetIfPresent(valueType);
if (parser != null)
{
return ParseValueUsing<TValue>(name, value, valueType, parser);
}
return DynamicValueOf<TValue>(parameters, name);
if (parameterType == ParameterType.Path)
{
return DynamicValueOf<TValue>(parameters, name);
}
if (parameterType == ParameterType.Query)
{
return DynamicValueOf<TValue>(request.Query, name);
}
throw new InvalidOperationException(string.Format("Could not get value for {0} with type {1}", name, valueType));
}
private static TValue ParseValueUsing<TValue>(string name, string value, Type valueType, Func<Parameter, object> parser)
@ -88,7 +108,7 @@ namespace {{packageName}}.{{packageContext}}.Utils
private static TValue DynamicValueOf<TValue>(dynamic parameters, string name)
{
string value = RawValue(parameters, name);
string value = parameters[name];
try
{
TValue result = parameters[name];
@ -102,7 +122,7 @@ namespace {{packageName}}.{{packageContext}}.Utils
}
catch (Exception e)
{
throw new InvalidOperationException(string.Format("Could not get '{0}' value of '{1}' type dynamicly",
throw new InvalidOperationException(string.Format("Could not get '{0}' value of '{1}' type dynamicly",
name, typeof(TValue)), e);
}
}
@ -364,4 +384,12 @@ namespace {{packageName}}.{{packageContext}}.Utils
}
}
}
internal enum ParameterType
{
Undefined,
Query,
Path,
Header
}
}