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

Get the SwaggerSchemaFilter working #1

Closed
wants to merge 1 commit into from
Closed
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
162 changes: 126 additions & 36 deletions Swagger/App_Start/SwaggerConfig.cs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Swagger/App_Start/WebApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Swagger
{
Expand All @@ -10,7 +11,7 @@ public static class WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
// Web API routes
config.MapHttpAttributeRoutes();

Expand Down
50 changes: 8 additions & 42 deletions Swagger/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,21 @@
using Swashbuckle.Swagger;
using Swashbuckle.Swagger.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using Swagger.Models;
using System.Web.Http;

namespace Swagger.Controllers
{
[RoutePrefix("Values")]
[RoutePrefix("SchemaFilter")]
public class ValuesController : ApiController
{
[HttpGet]
[Route("Working")]
[SwaggerResponse(HttpStatusCode.OK, "OK", typeof(WorkingGetResponse))]
public IHttpActionResult Working()
[Route("Regular")]
public SchemaFilterGetResponse GetSchemaFilter()
{
return Ok(new WorkingGetResponse { Strings = new List<string> { "value1", "value2" } });
return null;
}

[HttpGet]
[Route("NotWorking")]
[SwaggerResponse(HttpStatusCode.OK, "OK", typeof(NotWorkingGetResponse))]
public IHttpActionResult NotWorking()
[Route("Inherited")]
public InheritedGetResponse GetInheritedSchemaFilter()
{
return Ok(new NotWorkingGetResponse { Strings = new List<string> { "value1", "value2" } });
}
}

internal class NotWorkingGetResponse : BaseGetResponse
{
public IEnumerable<string> Strings { get; set; } = new List<string>();
}

[SwaggerSchemaFilter(typeof(SingleJsonArraySchemaFilter))]
internal class WorkingGetResponse : BaseGetResponse
{
public IEnumerable<string> Strings { get; set; } = new List<string>();
}

[SwaggerSchemaFilter(typeof(SingleJsonArraySchemaFilter))]
internal abstract class BaseGetResponse
{
}

internal class SingleJsonArraySchemaFilter : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
{
schema.@default = new { working = true };
return null;
}
}
}
29 changes: 29 additions & 0 deletions Swagger/Models/GetResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Swagger.Net;
using Swagger.Net.Annotations;
using System;
using System.Collections.Generic;

namespace Swagger.Models
{
[SwaggerSchemaFilter(typeof(AddDefault))]
public class SchemaFilterGetResponse
{
public IEnumerable<string> Strings { get; set; } = new List<string>();
}

public class InheritedGetResponse : Base
{
public IEnumerable<string> Strings { get; set; } = new List<string>();
}

[SwaggerSchemaFilter(typeof(AddDefault))]
public abstract class Base { }

public class AddDefault : ISchemaFilter
{
public void Apply(Schema schema, SchemaRegistry sr, Type t)
{
schema.example = schema.@default = new { working = true };
}
}
}
16 changes: 11 additions & 5 deletions Swagger/Swagger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,26 @@
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.7\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Swashbuckle.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd1bb07a5ac7c7bc, processorArchitecture=MSIL">
<HintPath>..\packages\Swashbuckle.Core.5.6.0\lib\net40\Swashbuckle.Core.dll</HintPath>
<Reference Include="Swagger.Net, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd1bb07a5ac7c7bc, processorArchitecture=MSIL">
<HintPath>..\packages\Swagger-Net.8.3.5.2\lib\net45\Swagger.Net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Cors.5.2.3\lib\net45\System.Web.Cors.dll</HintPath>
</Reference>
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Web.Http.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Cors.5.2.3\lib\net45\System.Web.Http.Cors.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Abstractions" />
Expand Down Expand Up @@ -160,6 +166,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\GetResponse.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -190,7 +197,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<Content Include="fonts\glyphicons-halflings-regular.woff" />
Expand Down
2 changes: 1 addition & 1 deletion Swagger/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
Expand Down
7 changes: 4 additions & 3 deletions Swagger/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
<package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.7" targetFramework="net461" />
<package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Modernizr" version="2.6.2" targetFramework="net461" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="Respond" version="1.2.0" targetFramework="net461" />
<package id="Swashbuckle" version="5.6.0" targetFramework="net461" />
<package id="Swashbuckle.Core" version="5.6.0" targetFramework="net461" />
<package id="Swagger-Net" version="8.3.5.2" targetFramework="net461" />
<package id="WebActivatorEx" version="2.0" targetFramework="net461" />
<package id="WebGrease" version="1.5.2" targetFramework="net461" />
</packages>