Permalink
Browse files

Use string in SwaggerPathItem instead of enum (#1802)

* Use string in SwaggerPathItem instead of enum

* Fix build errors
  • Loading branch information...
RSuter committed Jan 9, 2019
1 parent eff7698 commit 64bf42d77b34324618be888edad700e590ba87b9
@@ -67,7 +67,7 @@ protected OperationModelBase(JsonSchema4 exceptionSchema, SwaggerOperation opera
public string Path { get; set; }

/// <summary>Gets or sets the HTTP method.</summary>
public SwaggerOperationMethod HttpMethod { get; set; }
public string HttpMethod { get; set; }

/// <summary>Gets or sets the name of the operation.</summary>
public string OperationName { get; set; }
@@ -20,14 +20,14 @@ public interface IOperationNameGenerator
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
string GetClientName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation);
string GetClientName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation);

/// <summary>Gets the operation name for a given operation.</summary>
/// <param name="document">The Swagger document.</param>
/// <param name="path">The HTTP path.</param>
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The operation name.</returns>
string GetOperationName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation);
string GetOperationName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation);
}
}
@@ -23,7 +23,7 @@ public class MultipleClientsFromOperationIdOperationNameGenerator : IOperationNa
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetClientName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetClientName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
return GetClientName(operation);
}
@@ -34,7 +34,7 @@ public virtual string GetClientName(SwaggerDocument document, string path, Swagg
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The operation name.</returns>
public virtual string GetOperationName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetOperationName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
var clientName = GetClientName(operation);
var operationName = GetOperationName(operation);
@@ -22,7 +22,7 @@ public class MultipleClientsFromPathSegmentsOperationNameGenerator : IOperationN
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetClientName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetClientName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
return path
.Split('/')
@@ -38,7 +38,7 @@ public virtual string GetClientName(SwaggerDocument document, string path, Swagg
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetOperationName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetOperationName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
var operationName = ConvertPathToName(path);

@@ -52,7 +52,7 @@ public virtual string GetOperationName(SwaggerDocument document, string path, Sw

if (hasNameConflict)
{
operationName += CapitalizeFirst(httpMethod.ToString());
operationName += CapitalizeFirst(httpMethod);
}

return operationName;
@@ -20,7 +20,7 @@ public class SingleClientFromOperationIdOperationNameGenerator : IOperationNameG
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetClientName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetClientName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
return string.Empty;
}
@@ -31,7 +31,7 @@ public virtual string GetClientName(SwaggerDocument document, string path, Swagg
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetOperationName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetOperationName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
return operation.OperationId;
}
@@ -22,7 +22,7 @@ public class SingleClientFromPathSegmentsOperationNameGenerator : IOperationName
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetClientName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetClientName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
return string.Empty;
}
@@ -33,7 +33,7 @@ public virtual string GetClientName(SwaggerDocument document, string path, Swagg
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetOperationName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation)
public virtual string GetOperationName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
var operationName = ConvertPathToName(path);
var hasNameConflict = document.Paths
@@ -46,7 +46,7 @@ public virtual string GetOperationName(SwaggerDocument document, string path, Sw

if (hasNameConflict)
{
operationName += CapitalizeFirst(httpMethod.ToString());
operationName += CapitalizeFirst(httpMethod);
}

return operationName;
@@ -15,7 +15,7 @@ public class SwaggerOperationDescription
public string Path { get; set; }

/// <summary>Gets or sets the HTTP method.</summary>
public SwaggerOperationMethod Method { get; set; }
public string Method { get; set; }

/// <summary>Gets or sets the operation.</summary>
public SwaggerOperation Operation { get; set; }
@@ -6,50 +6,36 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace NSwag
{
/// <summary>Enumeration of the available HTTP methods. </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum SwaggerOperationMethod
public static class SwaggerOperationMethod
{
/// <summary>An undefined method.</summary>
[EnumMember(Value = "undefined")]
Undefined,
public static string Undefined { get; } = "undefined";

/// <summary>The HTTP GET method. </summary>
[EnumMember(Value = "get")]
Get,
public static string Get { get; } = "get";

/// <summary>The HTTP POST method. </summary>
[EnumMember(Value = "post")]
Post,
public static string Post { get; } = "post";

/// <summary>The HTTP PUT method. </summary>
[EnumMember(Value = "put")]
Put,
public static string Put { get; } = "put";

/// <summary>The HTTP DELETE method. </summary>
[EnumMember(Value = "delete")]
Delete,
public static string Delete { get; } = "delete";

/// <summary>The HTTP OPTIONS method. </summary>
[EnumMember(Value = "options")]
Options,
public static string Options { get; } = "options";

/// <summary>The HTTP HEAD method. </summary>
[EnumMember(Value = "head")]
Head,
public static string Head { get; } = "head";

/// <summary>The HTTP PATCH method. </summary>
[EnumMember(Value = "patch")]
Patch,
public static string Patch { get; } = "patch";

/// <summary>The HTTP TRACE method (OpenAPI only). </summary>
[EnumMember(Value = "trace")]
Trace
public static string Trace { get; } = "trace";
}
}
@@ -15,9 +15,9 @@

namespace NSwag
{
/// <summary>A Swagger path.</summary>
/// <summary>A Swagger path, the key is usually a value of <see cref="SwaggerOperationMethod"/>.</summary>
[JsonConverter(typeof(SwaggerPathItemConverter))]
public class SwaggerPathItem : ObservableDictionary<SwaggerOperationMethod, SwaggerOperation>
public class SwaggerPathItem : ObservableDictionary<string, SwaggerOperation>
{
/// <summary>Initializes a new instance of the <see cref="SwaggerPathItem"/> class.</summary>
public SwaggerPathItem()
@@ -86,9 +86,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
operations.Parameters = (List<SwaggerParameter>)serializer.Deserialize(reader, typeof(List<SwaggerParameter>));
else
{
var key = (SwaggerOperationMethod)Enum.Parse(typeof(SwaggerOperationMethod), propertyName, true);
var value = (SwaggerOperation)serializer.Deserialize(reader, typeof(SwaggerOperation));
operations.Add(key, value);
operations.Add(propertyName, value);
}

}
@@ -132,8 +132,7 @@ public async Task<SwaggerDocument> GenerateAsync(object serviceProvider)
path = "/" + path;

var controllerActionDescriptor = (ControllerActionDescriptor)apiDescription.ActionDescriptor;
if (!Enum.TryParse<SwaggerOperationMethod>(apiDescription.HttpMethod, ignoreCase: true, result: out var swaggerOperationMethod))
swaggerOperationMethod = SwaggerOperationMethod.Undefined;
var swaggerOperationMethod = apiDescription.HttpMethod.ToLowerInvariant();

var operationDescription = new SwaggerOperationDescription
{
@@ -435,7 +435,7 @@ private string GetActionName(MethodInfo method)
return methodName;
}

private IEnumerable<SwaggerOperationMethod> GetSupportedHttpMethods(MethodInfo method)
private IEnumerable<string> GetSupportedHttpMethods(MethodInfo method)
{
// See http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

@@ -466,7 +466,7 @@ private IEnumerable<SwaggerOperationMethod> GetSupportedHttpMethods(MethodInfo m
}
}

private IEnumerable<SwaggerOperationMethod> GetSupportedHttpMethodsFromAttributes(MethodInfo method)
private IEnumerable<string> GetSupportedHttpMethodsFromAttributes(MethodInfo method)
{
if (method.GetCustomAttributes().Any(a => a.GetType().Name == "HttpGetAttribute"))
yield return SwaggerOperationMethod.Get;

0 comments on commit 64bf42d

Please sign in to comment.