Skip to content

Commit

Permalink
Place application/json first for easier usage of Swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
cwe1ss committed Mar 1, 2019
1 parent 6e8f755 commit c156ec6
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Meceqs.AspNetCore.Swagger/MeceqsDocumentFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Meceqs.AspNetCore.Receiving;
using System;
using Meceqs.AspNetCore.Receiving;
using Meceqs.Transport;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
Expand All @@ -13,6 +14,8 @@ namespace Meceqs.AspNetCore.Swagger

public class MeceqsDocumentFilter : IDocumentFilter
{
private const string JsonContentType = "application/json";

private readonly ReceiveEndpointOptions _transportOptions;
private readonly IOptionsMonitor<AspNetCoreReceiverOptions> _receiverOptions;
private readonly IMessagePathConvention _messagePathConvention;
Expand Down Expand Up @@ -63,13 +66,11 @@ private void AddMessageType(MessageMetadata messageType, SwaggerDocument documen

// Content Types

var consumeTypes = _serializationProvider.GetSupportedContentTypes(messageType.MessageType);
operation.Consumes = new List<string>(consumeTypes);
operation.Consumes = GetOrderedContentTypes(messageType.MessageType);

if (messageType.ResponseType != typeof(void))
{
var producesTypes = _serializationProvider.GetSupportedContentTypes(messageType.ResponseType);
operation.Produces = new List<string>(producesTypes);
operation.Produces = GetOrderedContentTypes(messageType.ResponseType);
}

// Parameters
Expand Down Expand Up @@ -127,5 +128,19 @@ private void AddMessageType(MessageMetadata messageType, SwaggerDocument documen
Post = operation
});
}

private List<string> GetOrderedContentTypes(Type objectType)
{
var contentTypes = _serializationProvider.GetSupportedContentTypes(objectType).ToList();

// If JSON is supported, we place it at the beginning so that the Swagger UI automatically selects it.
// This makes using the UI much easier if you have other non-textual serializers.
if (contentTypes.Count > 1 && contentTypes.Remove(JsonContentType))
{
contentTypes.Insert(0, JsonContentType);
}

return contentTypes;
}
}
}

0 comments on commit c156ec6

Please sign in to comment.