Skip to content

Commit

Permalink
Enum support for api parameter added
Browse files Browse the repository at this point in the history
-  Set JsonConfiguration to serialize enum as string
  • Loading branch information
mknayak committed Jul 29, 2013
1 parent fad59ff commit fb751ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
18 changes: 16 additions & 2 deletions Swagger.Net.WebAPI/Controllers/BlogPostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public Blog Get(int id)
return LoadBlogs().FirstOrDefault(b => b.Id == id); ;
}

/// <summary>
/// Gets the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public IEnumerable<Blog> GetBlogsByType(Blogtype type)
{
return LoadBlogs().Where(b => b.Type == type);
}

// POST api/blogposts
public void Post([FromBody]string value)
{
Expand All @@ -57,8 +67,12 @@ public void Delete(int id)
List<Blog> LoadBlogs()
{
return new List<Blog> {
new Blog{Auther="swagger",Content="content 1",Id=1,Title="test"},
new Blog{Auther="swagger",Content="content 2",Id=2,Title="test"}
new Blog{Auther="swagger",Content="content 1",Id=1,Title="testSocial",Type=Blogtype.Social},
new Blog{Auther="swagger",Content="content 2",Id=2,Title="testHealth",Type=Blogtype.Health},
new Blog{Auther="swagger",Content="content 3",Id=2,Title="testTechnical",Type=Blogtype.Technical},
new Blog{Auther="swagger",Content="content 4",Id=2,Title="testSocial",Type=Blogtype.Social},
new Blog{Auther="swagger",Content="content 5",Id=2,Title="testHealth",Type=Blogtype.Health},
new Blog{Auther="swagger",Content="content 6",Id=2,Title="testTechnical",Type=Blogtype.Technical},
};
}
}
Expand Down
3 changes: 3 additions & 0 deletions Swagger.Net.WebAPI/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Newtonsoft.Json.Converters;

namespace Swagger.Net.WebApi
{
Expand All @@ -22,6 +23,8 @@ protected void Application_Start()
RouteConfig.RegisterRoutes(RouteTable.Routes);

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
// Show Enum as string in WebApi
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
}
}
}
11 changes: 10 additions & 1 deletion Swagger.Net/SwaggerModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public static ResourceApiOperationParameter CreateResourceApiOperationParameter(
required = docProvider.GetRequired(param.ParameterDescriptor)
};

if (param.ParameterDescriptor.ParameterType.IsEnum)
{
parameter.dataType = "string";
parameter.allowableValues = new AllowableValues();
parameter.allowableValues.valueType = "LIST";
parameter.allowableValues.values = Enum.GetNames(param.ParameterDescriptor.ParameterType);
}

return parameter;
}

Expand Down Expand Up @@ -292,6 +300,7 @@ public class ResourceApiOperation
public string summary { get; set; }
public string notes { get; set; }
public List<ResourceApiOperationParameter> parameters { get; set; }
public AllowableValues allowableValues { get; set; }
}

public class ResourceApiOperationParameter
Expand All @@ -302,7 +311,7 @@ public class ResourceApiOperationParameter
public string dataType { get; set; }
public bool required { get; set; }
public bool allowMultiple { get; set; }
public OperationParameterAllowableValues allowableValues { get; set; }
public AllowableValues allowableValues { get; set; }
}

public class OperationParameterAllowableValues
Expand Down

0 comments on commit fb751ae

Please sign in to comment.