Skip to content

Commit

Permalink
Feature/response type issues (#76)
Browse files Browse the repository at this point in the history
#73
#74
0.17.1
  • Loading branch information
Beffyman committed Mar 5, 2019
1 parent 46d5e9c commit ccd87d5
Show file tree
Hide file tree
Showing 19 changed files with 761 additions and 22 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '0.17.1+{build}'
version: '0.17.2+{build}'
configuration: Release
image: Visual Studio 2017

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace AspNetCore.Client.Serializers
internal class BlazorSimpleJsonSerializer : IHttpContentSerializer
{
internal static readonly string CONTENT_TYPE = "application/json";
public string ContentType => CONTENT_TYPE;
internal static readonly string PROBLEM_TYPE = "application/problem+json";
public string[] ContentTypes => new string[] { CONTENT_TYPE, PROBLEM_TYPE };

private static readonly IDictionary<Type, Func<string, object>> _knownJsonPrimitives = new Dictionary<Type, Func<string, object>>
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public HttpContent Serialize<T>(T request)
//Can't use the same stream writing as AspNetCore.Client.Serializers.JsonHttpSerializer because Blazor's json doesn't expose those AFAIK

var json = Json.Serialize(request);
return new StringContent(json, Encoding.UTF8, ContentType);
return new StringContent(json, Encoding.UTF8, CONTENT_TYPE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,34 @@ public IEnumerable<Header> GetHeaders()
/// <returns></returns>
public IEnumerable<ResponseType> GetResponseTypes()
{
return GetChildren().OfType<ResponseType>().OrderBy(x => x.SortOrder);
var responseTypes = GetChildren().OfType<ResponseType>();

var groupedResponses = responseTypes.GroupBy(x => x.Status);

List<ResponseType> prioritizedResponseTypes = new List<ResponseType>();

foreach (var responseGroup in groupedResponses)
{
if (responseGroup.Count() > 1)
{
if (ResponseTypes.Count(x => x.Status == responseGroup.Key) == 1)
{
var endpointResponse = ResponseTypes.SingleOrDefault(x => x.Status == responseGroup.Key);
prioritizedResponseTypes.Add(endpointResponse);
}
else
{
//This will fail, we will let it.
prioritizedResponseTypes.AddRange(responseGroup);
}
}
else
{
prioritizedResponseTypes.Add(responseGroup.Single());
}
}

return prioritizedResponseTypes.OrderBy(x => x.SortOrder);
}

/// <summary>
Expand Down
35 changes: 35 additions & 0 deletions src/AspNetCore.Client.Generator/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
using System.Threading;
using System.Threading.Tasks;
using AspNetCore.Client.Generator.CSharp.AspNetCoreHttp;
using AspNetCore.Client.Generator.Framework;
using AspNetCore.Client.Generator.Framework.AspNetCoreHttp.ResponseTypes;
using AspNetCore.Client.Generator.Framework.AspNetCoreHttp.Routes;
using AspNetCore.Client.Generator.Framework.Navigation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing.Template;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -37,6 +40,38 @@ internal static class Helpers
return source.Where(x => !typeof(K).IsAssignableFrom(x.GetType()));
}

public static IEnumerable<IParameter> FilterResponseTypes(this IEnumerable<IParameter> source, IEnumerable<ResponseType> priorityResponseTypes)
{
var responseTypes = source.OfType<ResponseType>();

var groupedResponses = responseTypes.GroupBy(x => x.Status);

List<ResponseType> prioritizedResponseTypes = new List<ResponseType>();

foreach (var responseGroup in groupedResponses)
{
if (responseGroup.Count() > 1)
{
if (priorityResponseTypes.Count(x => x.Status == responseGroup.Key) == 1)
{
var endpointResponse = priorityResponseTypes.SingleOrDefault(x => x.Status == responseGroup.Key);
prioritizedResponseTypes.Add(endpointResponse);
}
else
{
//This will fail, we will let it.
prioritizedResponseTypes.AddRange(responseGroup);
}
}
else
{
prioritizedResponseTypes.Add(responseGroup.Single());
}
}

return source.NotOfType<IParameter, ResponseType>().Union(prioritizedResponseTypes).OrderBy(x => x.DefaultValue == null ? 0 : 1).ThenBy(x => x.SortOrder);
}

public static HttpMethod HttpMethodFromEnum(HttpAttributeType type)
{
switch (type)
Expand Down
8 changes: 4 additions & 4 deletions src/AspNetCore.Client.Generator/Output/ClassWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ public static string WriteEndpointInterface(AspNetCoreHttpEndpoint endpoint)
{SharedWriter.GetObsolete(endpoint)}
{SharedWriter.GetInterfaceReturnType(endpoint.ReturnType, false)} {endpoint.Name}
(
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter).NotNull())}
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().FilterResponseTypes(endpoint.ResponseTypes).Select(SharedWriter.GetParameter).NotNull())}
);
{SharedWriter.GetObsolete(endpoint)}
Expand All @@ -629,7 +629,7 @@ public static string WriteEndpointInterface(AspNetCoreHttpEndpoint endpoint)
{SharedWriter.GetObsolete(endpoint)}
{SharedWriter.GetInterfaceReturnType(endpoint.ReturnType, true)} {endpoint.Name}Async
(
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter).NotNull())}
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().FilterResponseTypes(endpoint.ResponseTypes).Select(SharedWriter.GetParameter).NotNull())}
);
{SharedWriter.GetObsolete(endpoint)}
Expand All @@ -649,7 +649,7 @@ public static string WriteEndpointImplementation(AspNetCoreHttpController contro
{SharedWriter.GetObsolete(endpoint)}
public {SharedWriter.GetImplementationReturnType(endpoint.ReturnType, false)} {endpoint.Name}
(
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter).NotNull())}
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().FilterResponseTypes(endpoint.ResponseTypes).Select(SharedWriter.GetParameter).NotNull())}
)
{{
{GetMethodDetails(controller, endpoint, false, false)}
Expand All @@ -667,7 +667,7 @@ public static string WriteEndpointImplementation(AspNetCoreHttpController contro
{SharedWriter.GetObsolete(endpoint)}
public {SharedWriter.GetImplementationReturnType(endpoint.ReturnType, true)} {endpoint.Name}Async
(
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().Select(SharedWriter.GetParameter).NotNull())}
{string.Join($",{Environment.NewLine}", endpoint.GetParameters().FilterResponseTypes(endpoint.ResponseTypes).Select(SharedWriter.GetParameter).NotNull())}
)
{{
{GetMethodDetails(controller, endpoint, true, false)}
Expand Down
5 changes: 3 additions & 2 deletions src/AspNetCore.Client.JSInterop/JSInteropJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace AspNetCore.Client.Serializers
internal class JSInteropJsonSerializer : IHttpContentSerializer
{
internal static readonly string CONTENT_TYPE = "application/json";
public string ContentType => CONTENT_TYPE;
internal static readonly string PROBLEM_TYPE = "application/problem+json";
public string[] ContentTypes => new string[] { CONTENT_TYPE, PROBLEM_TYPE };

private static readonly IDictionary<Type, Func<string, object>> _knownJsonPrimitives = new Dictionary<Type, Func<string, object>>
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public HttpContent Serialize<T>(T request)
//Can't use the same stream writing as AspNetCore.Client.Serializers.JsonHttpSerializer because Blazor's json doesn't expose those AFAIK

var json = Json.Serialize(request);
return new StringContent(json, Encoding.UTF8, ContentType);
return new StringContent(json, Encoding.UTF8, CONTENT_TYPE);
}
}
}
4 changes: 2 additions & 2 deletions src/AspNetCore.Client.MessagePack/MessagePackSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AspNetCore.Client.Serializers
internal class MessagePackSerializer : IHttpContentSerializer
{
internal static readonly string CONTENT_TYPE = "application/x-msgpack";
public string ContentType => CONTENT_TYPE;
public string[] ContentTypes => new string[] { CONTENT_TYPE };

/// <summary>
/// Deserializes the request content which is assumed to be MessagePack into a object of <typeparamref name="T"/>
Expand All @@ -36,7 +36,7 @@ public HttpContent Serialize<T>(T request)
{
var data = MessagePack.MessagePackSerializer.Serialize(request, ContractlessStandardResolver.Instance);
var content = new ByteArrayContent(data);
content.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
content.Headers.ContentType = new MediaTypeHeaderValue(CONTENT_TYPE);
content.Headers.ContentLength = data.Length;

return content;
Expand Down
4 changes: 2 additions & 2 deletions src/AspNetCore.Client.Protobuf/ProtobufSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AspNetCore.Client.Serializers
internal class ProtobufSerializer : IHttpContentSerializer
{
internal static readonly string CONTENT_TYPE = "application/x-protobuf";
public string ContentType => CONTENT_TYPE;
public string[] ContentTypes => new string[] { CONTENT_TYPE };

/// <summary>
/// Deserializes the request content which is assumed to be protobuf into a object of <typeparamref name="T"/>
Expand All @@ -37,7 +37,7 @@ public HttpContent Serialize<T>(T request)
var stream = new MemoryStream();
Serializer.Serialize(stream, request);
var content = new StreamContent(stream);
content.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
content.Headers.ContentType = new MediaTypeHeaderValue(CONTENT_TYPE);
return content;
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/AspNetCore.Client/Serializers/HttpSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -27,13 +28,23 @@ public HttpSerializer(IServiceProvider provider, ClientConfiguration config)
_provider = provider;
_config = config;

Serializer = (IHttpContentSerializer)_provider.GetService(config.Serializer);
Serializer = (IHttpContentSerializer)_provider.GetService(_config.Serializer);

Deserializers = new Dictionary<string, IHttpContentSerializer>();
foreach (var serType in _config.Deserializers)
{
var ser = (IHttpContentSerializer)_provider.GetService(serType);
Deserializers.Add(ser.ContentType, ser);
foreach (var contentType in ser.ContentTypes ?? Enumerable.Empty<string>())
{
if (Deserializers.ContainsKey(contentType))
{
Deserializers[contentType] = ser;
}
else
{
Deserializers.Add(contentType, ser);
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/AspNetCore.Client/Serializers/IHttpContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace AspNetCore.Client.Serializers
public interface IHttpContentSerializer
{
/// <summary>
/// Content-Type that this Serializer can parse
/// Content-Types that this can deserialize
/// </summary>
string ContentType { get; }
string[] ContentTypes { get; }

/// <summary>
/// Deserializes the content of the http response into the type provided
Expand Down
5 changes: 3 additions & 2 deletions src/AspNetCore.Client/Serializers/JsonHttpSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace AspNetCore.Client.Serializers
internal class JsonHttpSerializer : IHttpContentSerializer
{
internal static readonly string CONTENT_TYPE = "application/json";
public string ContentType => CONTENT_TYPE;
internal static readonly string PROBLEM_TYPE = "application/problem+json";
public string[] ContentTypes => new string[] { CONTENT_TYPE, PROBLEM_TYPE };


private static readonly IDictionary<Type, Func<string, object>> _knownJsonPrimitives = new Dictionary<Type, Func<string, object>>
Expand Down Expand Up @@ -69,7 +70,7 @@ public async Task<T> Deserialize<T>(HttpContent content)
public HttpContent Serialize<T>(T request)
{
var json = JsonConvert.SerializeObject(request);
return new StringContent(json, Encoding.UTF8, ContentType);
return new StringContent(json, Encoding.UTF8, CONTENT_TYPE);
}
}
}
2 changes: 1 addition & 1 deletion src/AspNetCore.Client/Serializers/TextHttpSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace AspNetCore.Client.Serializers
internal class TextHttpSerializer : IHttpContentSerializer
{
internal static readonly string CONTENT_TYPE = "text/plain";
public string ContentType => CONTENT_TYPE;
public string[] ContentTypes => new string[] { CONTENT_TYPE };


/// <summary>
Expand Down
Loading

0 comments on commit ccd87d5

Please sign in to comment.