Added ability to specify a RouteResponse and RouteRequest as a dynamic. Improved documentation and fixed some null checks that were missing with this change. Bumped package version to 1.7.6

This commit is contained in:
MattMo 2023-12-14 11:33:42 -08:00
parent 38ef135b8a
commit 5f83b30cb2
7 changed files with 37 additions and 15 deletions

View File

@ -211,7 +211,7 @@ namespace MontoyaTech.Rest.Net.Example
}
[RouteGroup("Auth")]
[RouteResponse(typeof(JObject))]
[RouteResponse(Dynamic = true)]
public static HttpListenerResponse Dynamic(HttpListenerContext context)
{
return context.Response.WithStatus(HttpStatusCode.OK).WithJson(777);

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.7.5</Version>
<Version>1.7.6</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -378,9 +378,9 @@ namespace MontoyaTech.Rest.Net
//Generate the route function header
if (this.StaticCode)
writer.Write($"public static {(routeResponse == null ? "void" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
writer.Write($"public static {(routeResponse == null ? "void" : (routeResponse.Dynamic ? "dynamic" : this.GetTypeFullyResolvedName(routeResponse.ResponseType)))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
else
writer.Write($"public {(routeResponse == null ? "void" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
writer.Write($"public {(routeResponse == null ? "void" : (routeResponse.Dynamic ? "dynamic" : this.GetTypeFullyResolvedName(routeResponse.ResponseType)))} {(routeName == null ? methodInfo.Name : routeName.Name)}(");
//Generate the functions parameters
var parameters = methodInfo.GetParameters();
@ -397,13 +397,25 @@ namespace MontoyaTech.Rest.Net
if (routeRequest != null)
{
writer.WriteSeparator();
writer.Write(this.GetTypeFullyResolvedName(routeRequest.RequestType)).Write(" request");
if (routeRequest.Dynamic)
writer.Write("dynamic");
else
writer.Write(this.GetTypeFullyResolvedName(routeRequest.RequestType));
writer.Write(" request");
}
if (routeResponse != null && routeResponse.Parameter)
{
writer.WriteSeparator();
writer.Write(this.GetTypeFullyResolvedName(routeResponse.ResponseType)).Write(" input");
if (routeResponse.Dynamic)
writer.Write("dynamic");
else
writer.Write(this.GetTypeFullyResolvedName(routeResponse.ResponseType));
writer.Write(" input");
}
writer.WriteLine(")").WriteLine("{").Indent();
@ -507,7 +519,7 @@ namespace MontoyaTech.Rest.Net
{
writer.WriteBreak().WriteLine("if (response.IsSuccessStatusCode)").WriteLine("{").Indent();
if (routeResponse.ResponseType.IsAssignableTo(typeof(Stream)))
if (routeResponse.ResponseType != null && routeResponse.ResponseType.IsAssignableTo(typeof(Stream)))
{
if (routeResponse.Parameter)
{
@ -532,7 +544,7 @@ namespace MontoyaTech.Rest.Net
if (routeResponse.Json)
{
writer.WriteBreak().WriteLine($"return JsonConvert.DeserializeObject<{this.GetTypeFullyResolvedName(routeResponse.ResponseType)}>(content);");
writer.WriteBreak().WriteLine($"return JsonConvert.DeserializeObject<{(routeResponse.Dynamic ? "dynamic" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))}>(content);");
}
else
{
@ -599,7 +611,7 @@ namespace MontoyaTech.Rest.Net
break;
case TypeCode.Object:
throw new NotSupportedException("ResponseType isn't JSON but is an object.");
throw new NotSupportedException("RouteResponse has JSON=false but ResponseType is an object.");
}
}
}

View File

@ -166,7 +166,7 @@ namespace MontoyaTech.Rest.Net
var routeRequest = methodInfo.GetCustomAttribute<RouteRequest>();
if (routeRequest != null)
if (routeRequest != null && routeRequest.RequestType != null)
{
var types = this.FindTypeDependencies(routeRequest.RequestType);
@ -177,7 +177,7 @@ namespace MontoyaTech.Rest.Net
var routeResponse = methodInfo.GetCustomAttribute<RouteResponse>();
if (routeResponse != null)
if (routeResponse != null && routeResponse.ResponseType != null)
{
var types = this.FindTypeDependencies(routeResponse.ResponseType);

View File

@ -496,11 +496,11 @@ namespace MontoyaTech.Rest.Net
//Generate request doc if any
if (routeRequest != null)
writer.WriteLine($"@param {{{this.GetTypeFullyResolvedName(routeRequest.RequestType)}}} request");
writer.WriteLine($"@param {{{(routeRequest.Dynamic ? "Any" : this.GetTypeFullyResolvedName(routeRequest.RequestType))}}} request");
//Generate response doc if any
if (routeResponse != null)
writer.WriteLine($"@returns {{{this.GetTypeFullyResolvedName(routeResponse.ResponseType)}}}");
writer.WriteLine($"@returns {{{(routeResponse.Dynamic ? "Any" : this.GetTypeFullyResolvedName(routeResponse.ResponseType))}}}");
writer.WriteLine("@throws {Response} If response status was not ok.");
@ -610,7 +610,7 @@ namespace MontoyaTech.Rest.Net
{
writer.WriteBreak().WriteLine("if (response.ok) {").Indent();
if (routeResponse.ResponseType.IsAssignableTo(typeof(Stream)))
if (routeResponse.ResponseType != null && routeResponse.ResponseType.IsAssignableTo(typeof(Stream)))
{
if (routeResponse.Parameter)
{

View File

@ -22,6 +22,11 @@ namespace MontoyaTech.Rest.Net
/// </summary>
public bool Json = true;
/// <summary>
/// Whether or not the request is a dynamic type. Default is false.
/// </summary>
public bool Dynamic = false;
/// <summary>
/// Creates a default route request.
/// </summary>

View File

@ -23,10 +23,15 @@ namespace MontoyaTech.Rest.Net
public bool Json = true;
/// <summary>
/// Whether or not the response is passed as a parameter to the route function.
/// Whether or not the response is passed as a parameter to the route function. Default is false.
/// </summary>
public bool Parameter = false;
/// <summary>
/// Whether or not the response is a dynamic type. Default is false.
/// </summary>
public bool Dynamic = false;
/// <summary>
/// Creates a default route response.
/// </summary>