Skip to content

Commit

Permalink
Swashbuckle compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dasiths committed Mar 22, 2020
1 parent 7cb3682 commit 88ad2ae
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- name: Create the package
run: dotnet pack --configuration Release SimpleEndPoints
working-directory: ./src
- name: Publish the package to NUGET.ORG
- name: Publish the package to NUGET.ORG
if: github.ref == 'refs/heads/master'
run: dotnet nuget push SimpleEndPoints/bin/Release/*.nupkg -k ${{secrets.NUGET_APIKEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate
working-directory: ./src
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ In the NuGet Package Manager Console, type:
services.AddControllers((options) =>
{
options.AddEndpointRoutingConvention(); // This is required to translate endpoint names
options.AddSimpleEndpointsRouting(); // This is required to translate endpoint names
// Optional: If you need to support Swashbuckle, you will need the "SimpleEndpoints.Swashbuckle" package too
options.AddSwashbuckleCompatibilityForSimpleEndpoints(); // this is required to support Swashbuckle
});
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EndpointRoutingConvention: IApplicationModelConvention
{
private const string EndpointString = "endpoint";

public void Apply(ApplicationModel application)
public virtual void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleEndPoints/Extensions/MvcOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SimpleEndpoints.Extensions
{
public static class MvcOptionsExtensions
{
public static void AddEndpointRoutingConvention(this MvcOptions options)
public static void AddSimpleEndpointsRouting(this MvcOptions options)
{
options.Conventions.Add(new EndpointRoutingConvention());
}
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleEndPoints/SimpleEndpoints.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryUrl>https://github.com/dasiths/SimpleEndpoints</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageTags>aspnetcore api controller endpoints</PackageTags>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,22 @@

namespace SimpleEndpoints.Example.Endpoints.WeatherForecast
{
public class WeatherForecastEndpoint : AsyncGetEndpoint<int, List<WeatherForecast>>
public class WeatherForecastEndpoint : AsyncGetEndpoint<List<WeatherForecast>>
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

[NonAction]
public override async Task<ActionResult<List<WeatherForecast>>> HandleAsync(int someParams, CancellationToken cancellationToken = default)
public override async Task<ActionResult<List<WeatherForecast>>> HandleAsync(CancellationToken cancellationToken = default)
{
var rng = new Random();
return await Task.FromResult(Enumerable.Range(1, someParams).Select(index => new WeatherForecast
return await Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
}).ToList());
}

public async Task<ActionResult<List<WeatherForecast>>> Get(int count, CancellationToken cancellationToken)
{
return await this.HandleAsync(count + 5, cancellationToken);
}
}
}
5 changes: 5 additions & 0 deletions src/SimpleEndpoints.Example/SimpleEndpoints.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleEndpoints.Swashbuckle\SimpleEndpoints.Swashbuckle.csproj" />
<ProjectReference Include="..\SimpleEndPoints\SimpleEndpoints.csproj" />
</ItemGroup>

Expand Down
21 changes: 20 additions & 1 deletion src/SimpleEndpoints.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using SimpleEndpoints.Conventions;
using SimpleEndpoints.Core;
using SimpleEndpoints.Extensions;
using SimpleEndpoints.Swashbuckle;

namespace SimpleEndpoints.Example
{
Expand All @@ -24,7 +26,14 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers((options) =>
{
options.AddEndpointRoutingConvention(); // This is required to translate endpoint names
options.AddSimpleEndpointsRouting(); // This is required to translate endpoint names
options.AddSwashbuckleCompatibilityForSimpleEndpoints(); // this is required to support Swashbuckle
});

// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
}

Expand All @@ -38,6 +47,16 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseHttpsRedirection();

// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();

// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});

app.UseRouting();

app.UseAuthorization();
Expand Down
12 changes: 12 additions & 0 deletions src/SimpleEndpoints.Swashbuckle/MvcOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace SimpleEndpoints.Swashbuckle
{
public static class MvcOptionsExtensions
{
public static void AddSwashbuckleCompatibilityForSimpleEndpoints(this MvcOptions options)
{
options.Conventions.Add(new SwashbuckleCompatibilityConvention());
}
}
}
20 changes: 20 additions & 0 deletions src/SimpleEndpoints.Swashbuckle/SimpleEndpoints.Swashbuckle.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Swashbuckle support for SimpleEndpoints.
A simple, convention-based, endpoint per action pattern implementation for AspNetCore 3.0+</Description>
<Copyright>2020 Dasith Wijesiriwardena</Copyright>
<PackageProjectUrl>https://github.com/dasiths/SimpleEndpoints</PackageProjectUrl>
<RepositoryUrl>https://github.com/dasiths/SimpleEndpoints</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageTags>aspnetcore api controller endpoints swashbuckle</PackageTags>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Routing;

namespace SimpleEndpoints.Swashbuckle
{
public class SwashbuckleCompatibilityConvention : IApplicationModelConvention
{
public void Apply(ApplicationModel application)
{
foreach (var action in application.Controllers.SelectMany(c => c.Actions))
{
if (action.Selectors.Any() &&
action.Selectors[0].ActionConstraints.Count == 0 &&
action.ApiExplorer.IsVisible.GetValueOrDefault(false))
{
var attribute = action.Attributes.FirstOrDefault(a => a is HttpMethodAttribute);

if (attribute is HttpMethodAttribute item)
{
var o = new HttpMethodActionConstraint(item.HttpMethods);
action.Selectors[0].ActionConstraints.Add(o);
}
}
}
}
}
}
8 changes: 7 additions & 1 deletion src/SimpleEndpoints.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleEndpoints.Example", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleEndpoints", "SimpleEndPoints\SimpleEndpoints.csproj", "{06D8DE86-AC08-452C-882D-6F57CFF4835E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleEndpoints.Tests", "SimpleEndpoints.Tests\SimpleEndpoints.Tests.csproj", "{60D80A29-1FFD-495A-9CF0-9285920F0356}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleEndpoints.Tests", "SimpleEndpoints.Tests\SimpleEndpoints.Tests.csproj", "{60D80A29-1FFD-495A-9CF0-9285920F0356}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleEndpoints.Swashbuckle", "SimpleEndpoints.Swashbuckle\SimpleEndpoints.Swashbuckle.csproj", "{2AE58DD8-87F7-4136-8F61-9ECFB9A3B728}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -27,6 +29,10 @@ Global
{60D80A29-1FFD-495A-9CF0-9285920F0356}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60D80A29-1FFD-495A-9CF0-9285920F0356}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60D80A29-1FFD-495A-9CF0-9285920F0356}.Release|Any CPU.Build.0 = Release|Any CPU
{2AE58DD8-87F7-4136-8F61-9ECFB9A3B728}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2AE58DD8-87F7-4136-8F61-9ECFB9A3B728}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AE58DD8-87F7-4136-8F61-9ECFB9A3B728}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AE58DD8-87F7-4136-8F61-9ECFB9A3B728}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 88ad2ae

Please sign in to comment.