Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum Support and Resource Models added #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions Swagger.Net.WebAPI/Controllers/BlogPostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,44 @@
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Swagger.Net.WebAPI.Models;

namespace Swagger.Net.WebApi.Controllers
{
/// <summary>
/// BlogPosts Controller
/// </summary>
public class BlogPostsController : ApiController
{
// GET api/blogposts
public IEnumerable<string> Get()
/// <summary>
/// Gets this instance.
/// </summary>
/// <returns></returns>
public IEnumerable<Blog> Get()
{
return new string[] { "value1", "value2" };
return LoadBlogs();
}

// GET api/blogposts/5
public string Get(int id)
/// <summary>
/// Gets the specified id.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
public Blog Get(int id)
{
return "value";
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
Expand All @@ -35,5 +58,22 @@ public void Put(int id, [FromBody]string value)
public void Delete(int id)
{
}


/// <summary>
/// Loads the blogs.
/// </summary>
/// <returns></returns>
List<Blog> LoadBlogs()
{
return new List<Blog> {
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},
};
}
}
}
6 changes: 1 addition & 5 deletions Swagger.Net.WebAPI/Controllers/PetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Swagger.Net.WebAPI.Models;

namespace Swagger_Test
{
Expand Down Expand Up @@ -68,9 +69,4 @@ public HttpResponseMessage Delete(int id)
}
}

public class Pet
{
public int Id;
public string Name;
}
}
5 changes: 5 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 @@ -20,6 +21,10 @@ protected void Application_Start()
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
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());
}
}
}
76 changes: 76 additions & 0 deletions Swagger.Net.WebAPI/Models/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Swagger.Net.WebAPI.Models
{
/// <summary>
/// Blog Model
/// </summary>
public class Blog
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>
/// The id.
/// </value>
public int Id { get; set; }

/// <summary>
/// Gets or sets the auther.
/// </summary>
/// <value>
/// The auther.
/// </value>
public string Auther { get; set; }

/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title { get; set; }

/// <summary>
/// Gets or sets the content.
/// </summary>
/// <value>
/// The content.
/// </value>
public string Content { get; set; }

/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
public Blogtype Type { get; set; }
}

/// <summary>
/// Blog Type Enum
/// </summary>
public enum Blogtype
{
/// <summary>
/// The social
/// </summary>
Social,
/// <summary>
/// The technical
/// </summary>
Technical,
/// <summary>
/// The health
/// </summary>
Health,
/// <summary>
/// The others
/// </summary>
Others
}
}
48 changes: 48 additions & 0 deletions Swagger.Net.WebAPI/Models/Pet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Swagger.Net.WebAPI.Models
{

/// <summary>
/// Pet model
/// </summary>
public class Pet
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>
/// The id.
/// </value>
public int Id { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
}

/// <summary>
/// PetType Enum
/// </summary>
public enum PetType
{
/// <summary>
/// The bird
/// </summary>
Bird,
/// <summary>
/// The animal
/// </summary>
Animal,
/// <summary>
/// The fish
/// </summary>
Fish
}
}
7 changes: 6 additions & 1 deletion Swagger.Net.WebAPI/Swagger.Net.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<UseIISExpress>false</UseIISExpress>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -124,6 +128,8 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\Blog.cs" />
<Compile Include="Models\Pet.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -144,7 +150,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
Expand Down
4 changes: 4 additions & 0 deletions Swagger.Net/Swagger.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.4.5.6\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Entity" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
Expand Down
24 changes: 22 additions & 2 deletions Swagger.Net/SwaggerActionFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
Expand Down Expand Up @@ -46,7 +47,7 @@ private ResourceListing getDocs(HttpActionContext actionContext)
{
string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName;
if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) ||
apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper()))
apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper()))
continue;

// Make sure we only report the current controller docs
Expand All @@ -55,6 +56,25 @@ private ResourceListing getDocs(HttpActionContext actionContext)

ResourceApi rApi = SwaggerGen.CreateResourceApi(api);
r.apis.Add(rApi);
List<ResourceModel> rModels = SwaggerGen.CreateResourceModel(api.ActionDescriptor.ReturnType);

if (rModels != null && rModels.Any())
{
foreach (var rModel in rModels)
{
if (r.models.ContainsKey(rModel.name))
{
if (r.models.Values.Any(m => m.NameSpace.Equals(rModel.NameSpace, StringComparison.OrdinalIgnoreCase)))
continue;

int count = r.models.Values.Count(m => m.NameSpace.Substring(m.NameSpace.LastIndexOf('.') + 1).Equals(rModel.name));

rModel.name += count;
}
r.models.Add(rModel.name, rModel);
}
}


ResourceApiOperation rApiOperation = SwaggerGen.CreateResourceApiOperation(api, docProvider);
rApi.operations.Add(rApiOperation);
Expand All @@ -65,7 +85,7 @@ private ResourceListing getDocs(HttpActionContext actionContext)
rApiOperation.parameters.Add(parameter);
}
}

return r;
}
}
Expand Down
Loading