-
-
Notifications
You must be signed in to change notification settings - Fork 158
Minimal Swashbuckle setup #1073
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9798f4f
Add minimal Swashbuckle setup
maurei cf699c9
add tests, run ci build for openapi, use build props for openapi proj…
maurei f4cd2a3
review feedback
maurei dd3f794
CI clone_script fix
maurei efa98f5
review feedback
maurei 901d2d0
fix CI build
maurei 7d42f80
Merge branch 'openapi' into oa/minimal-setup
maurei 64671c0
Reformat project files (using VS, doesn't appear to work in Rider)
e80478d
Fixed schema error in VS
204bcf9
reorder csproj
maurei a61d571
review feedback
maurei c0d4918
add SwaggerUI to example project
maurei fa448f5
rm redundant bit from dbcontext
maurei 3aad466
add documentation
maurei bfab00f
typo
maurei 8ab5b7c
remove redundant formatter instructions
maurei a46fc4d
review feedback
maurei 5fc0afa
review feedback
maurei 3a3c150
Merge branch 'openapi' into oa/minimal-setup
maurei 71be36e
bump version
maurei e715d2b
updated swagger.json to pass test
maurei 79f79dc
add nuget specification in project file, add sourcelink
maurei 6c61da6
adjust buildscript
maurei c6e6e37
Add comment to AddOpenApi, introduced authors tag and build property …
maurei c236cad
Reformatted project file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ environment: | |
branches: | ||
only: | ||
- master | ||
- openapi | ||
- develop | ||
- unstable | ||
- /release\/.+/ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# OpenAPI | ||
|
||
You can describe your API with an OpenAPI specification using the [Swashbuckle](https://github.com/domaindrivendev/Swashbuckle.AspNetCore) integration for JsonApiDotNetCore. | ||
|
||
## Installation | ||
|
||
Install the `JsonApiDotNetCore.OpenApi` NuGet package. | ||
|
||
### CLI | ||
|
||
``` | ||
dotnet add package JsonApiDotNetCore.OpenApi | ||
``` | ||
|
||
### Visual Studio | ||
|
||
```powershell | ||
Install-Package JsonApiDotNetCore.OpenApi | ||
``` | ||
|
||
### *.csproj | ||
|
||
```xml | ||
<ItemGroup> | ||
<!-- Be sure to check NuGet for the latest version # --> | ||
<PackageReference Include="JsonApiDotNetCore.OpenApi" Version="4.0.0" /> | ||
</ItemGroup> | ||
``` | ||
|
||
## Usage | ||
|
||
Add the integration in your `Startup` class. | ||
|
||
```c# | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
IMvcCoreBuilder mvcBuilder = services.AddMvcCore(); | ||
services.AddJsonApi<AppDbContext>(mvcBuilder: mvcBuilder); | ||
|
||
// Adds the Swashbuckle integration. | ||
services.AddOpenApi(mvcBuilder); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
app.UseRouting(); | ||
app.UseJsonApi(); | ||
|
||
// Adds the Swashbuckle middleware. | ||
app.UseSwagger(); | ||
|
||
app.UseEndpoints(endpoints => endpoints.MapControllers()); | ||
} | ||
} | ||
``` | ||
|
||
By default, the OpenAPI specification will be available at `http://localhost:<port>/swagger/v1/swagger.json`. | ||
|
||
Swashbuckle also ships with [SwaggerUI](https://swagger.io/tools/swagger-ui/), tooling for a generated documentation page. This can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Startup` class. | ||
|
||
```c# | ||
// Startup.cs | ||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
app.UseSwaggerUI(); | ||
} | ||
``` | ||
|
||
By default, SwaggerUI will be available at `http://localhost:<port>/swagger`. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/JsonApiDotNetCore.OpenApi/JsonApiDotNetCore.OpenApi.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<VersionPrefix>$(JsonApiDotNetCoreVersionPrefix)</VersionPrefix> | ||
<TargetFramework>$(NetCoreAppVersion)</TargetFramework> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
bart-degreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<PackageTags>jsonapidotnetcore;jsonapi;json:api;dotnet;asp.net;openapi;swagger;swaggerui;swashbuckle</PackageTags> | ||
<Description>A Swashbuckle integration that enables you to describe a JsonApiDotNetCore API with an OpenAPI specification.</Description> | ||
<Authors>json-api-dotnet</Authors> | ||
<PackageProjectUrl>https://www.jsonapi.net/</PackageProjectUrl> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<DebugType>embedded</DebugType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\JsonApiDotNetCore\JsonApiDotNetCore.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbuckleVersion)" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="$(SwashbuckleVersion)" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="$(SwashbuckleVersion)" /> | ||
bart-degreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</ItemGroup> | ||
</Project> |
19 changes: 19 additions & 0 deletions
19
src/JsonApiDotNetCore.OpenApi/OpenApiEndpointConvention.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.ApplicationModels; | ||
using Microsoft.AspNetCore.Mvc.Routing; | ||
|
||
namespace JsonApiDotNetCore.OpenApi | ||
{ | ||
internal sealed class OpenApiEndpointConvention : IActionModelConvention | ||
{ | ||
public void Apply(ActionModel action) | ||
{ | ||
ArgumentGuard.NotNull(action, nameof(action)); | ||
|
||
if (!action.ActionMethod.GetCustomAttributes(true).OfType<HttpMethodAttribute>().Any()) | ||
{ | ||
action.ApiExplorer.IsVisible = false; | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace JsonApiDotNetCore.OpenApi | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the OpenAPI integration to JsonApiDotNetCore by configuring Swashbuckle. | ||
/// </summary> | ||
public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder mvcBuilder, Action<SwaggerGenOptions> setupSwaggerGenAction = null) | ||
bart-degreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ArgumentGuard.NotNull(services, nameof(services)); | ||
ArgumentGuard.NotNull(mvcBuilder, nameof(mvcBuilder)); | ||
|
||
mvcBuilder.AddApiExplorer(); | ||
|
||
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new OpenApiEndpointConvention())); | ||
|
||
services.AddSwaggerGen(setupSwaggerGenAction); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.Resources; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace OpenApiTests | ||
{ | ||
[UsedImplicitly(ImplicitUseTargetFlags.Members)] | ||
public sealed class Airplane : Identifiable | ||
{ | ||
[Attr] | ||
public int SeatingCapacity { get; set; } | ||
|
||
[Attr] | ||
public DateTimeOffset ManufacturedAt { get; set; } | ||
|
||
[HasMany] | ||
public ISet<Flight> Flights { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Controllers; | ||
using JsonApiDotNetCore.Services; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace OpenApiTests | ||
{ | ||
public sealed class AirplanesController : JsonApiController<Airplane> | ||
{ | ||
public AirplanesController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Airplane> resourceService) | ||
: base(options, loggerFactory, resourceService) | ||
{ | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.Resources; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace OpenApiTests | ||
{ | ||
[UsedImplicitly(ImplicitUseTargetFlags.Members)] | ||
public sealed class Flight : Identifiable | ||
bart-degreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
[Attr] | ||
public string Destination { get; set; } | ||
|
||
[Attr] | ||
public DateTimeOffset DepartsAt { get; set; } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.