-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Little OpenAPI generation fixes for NSwag usage. Closes GH-685
- Loading branch information
1 parent
ffd304b
commit 910dc0c
Showing
12 changed files
with
304 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,89 @@ | ||
using Marten; | ||
using Wolverine; | ||
using Wolverine.Http; | ||
|
||
namespace NSwagDemonstrator; | ||
|
||
|
||
|
||
|
||
public class Todo | ||
{ | ||
public int Id { get; set; } | ||
public string? Name { get; set; } | ||
public bool IsComplete { get; set; } | ||
} | ||
|
||
public record CreateTodo(string Name); | ||
|
||
public record UpdateTodo(int Id, string Name, bool IsComplete); | ||
|
||
public record DeleteTodo(int Id); | ||
|
||
public record TodoCreated(int Id); | ||
|
||
public static class TodoEndpoints | ||
{ | ||
[WolverineGet("/todoitems")] | ||
public static Task<IReadOnlyList<Todo>> Get(IQuerySession session) | ||
=> session.Query<Todo>().ToListAsync(); | ||
|
||
|
||
[WolverineGet("/todoitems/complete")] | ||
public static Task<IReadOnlyList<Todo>> GetComplete(IQuerySession session) => | ||
session.Query<Todo>().Where(x => x.IsComplete).ToListAsync(); | ||
|
||
// Wolverine can infer the 200/404 status codes for you here | ||
// so there's no code noise just to satisfy OpenAPI tooling | ||
[WolverineGet("/todoitems/{id}")] | ||
public static Task<Todo?> GetTodo(int id, IQuerySession session, CancellationToken cancellation) | ||
=> session.LoadAsync<Todo>(id, cancellation); | ||
|
||
|
||
[WolverinePost("/todoitems")] | ||
public static async Task<IResult> Create(CreateTodo command, IDocumentSession session, IMessageBus bus) | ||
{ | ||
var todo = new Todo { Name = command.Name }; | ||
session.Store(todo); | ||
|
||
// Going to raise an event within out system to be processed later | ||
await bus.PublishAsync(new TodoCreated(todo.Id)); | ||
|
||
return Results.Created($"/todoitems/{todo.Id}", todo); | ||
} | ||
|
||
[WolverineDelete("/todoitems")] | ||
public static void Delete(DeleteTodo command, IDocumentSession session) | ||
=> session.Delete<Todo>(command.Id); | ||
} | ||
|
||
|
||
public static class TodoCreatedHandler | ||
{ | ||
// Do something in the background, like assign it to someone, | ||
// send out emails or texts, alerts, whatever | ||
public static void Handle(TodoCreated created, ILogger logger) | ||
{ | ||
logger.LogInformation("Got a new TodoCreated event for " + created.Id); | ||
} | ||
} | ||
|
||
public static class UpdateTodoEndpoint | ||
{ | ||
public static async Task<(Todo? todo, IResult result)> LoadAsync(UpdateTodo command, IDocumentSession session) | ||
{ | ||
var todo = await session.LoadAsync<Todo>(command.Id); | ||
return todo != null | ||
? (todo, new WolverineContinue()) | ||
: (todo, Results.NotFound()); | ||
} | ||
|
||
[WolverinePut("/todoitems")] | ||
public static void Put(UpdateTodo command, Todo todo, IDocumentSession session) | ||
{ | ||
todo.Name = todo.Name; | ||
todo.IsComplete = todo.IsComplete; | ||
session.Store(todo); | ||
} | ||
} | ||
|
This file contains 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,13 @@ | ||
using Wolverine.Http; | ||
|
||
namespace NSwagDemonstrator; | ||
|
||
#region sample_hello_world_with_wolverine_http | ||
|
||
public class HelloEndpoint | ||
{ | ||
[WolverineGet("/")] | ||
public string Get() => "Hello."; | ||
} | ||
|
||
#endregion |
This file contains 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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Wolverine.Http.Marten\Wolverine.Http.Marten.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\..\Servers.cs"> | ||
<Link>Servers.cs</Link> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<!-- | ||
To use OpenApiGenerateDocumentsOnBuild with Wolverine, upgrade Microsoft.Extensions.ApiDescription.Server (transitive NSwag dependency). | ||
See also: https://github.com/RicoSuter/NSwag/issues/3403 | ||
--> | ||
<!-- <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="8.0.1"> --> | ||
<!-- <PrivateAssets>all</PrivateAssets> --> | ||
<!-- <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> --> | ||
<!-- </PackageReference> --> | ||
<PackageReference Include="NSwag.AspNetCore" Version="14.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,6 @@ | ||
@NSwagDemonstrator_HostAddress = http://localhost:5283 | ||
|
||
GET {{NSwagDemonstrator_HostAddress}}/weatherforecast/ | ||
Accept: application/json | ||
|
||
### |
This file contains 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,56 @@ | ||
|
||
using IntegrationTests; | ||
using Marten; | ||
using Oakton; | ||
using Oakton.Resources; | ||
using Wolverine; | ||
using Wolverine.Http; | ||
using Wolverine.Marten; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Adding Marten for persistence | ||
builder.Services.AddMarten(opts => | ||
{ | ||
opts.Connection(Servers.PostgresConnectionString); | ||
opts.DatabaseSchemaName = "todo"; | ||
}) | ||
.IntegrateWithWolverine(); | ||
|
||
builder.Services.AddResourceSetupOnStartup(); | ||
|
||
// Wolverine usage is required for WolverineFx.Http | ||
builder.Host.UseWolverine(opts => | ||
{ | ||
opts.Durability.Mode = DurabilityMode.Solo; | ||
opts.Durability.DurabilityAgentEnabled = false; | ||
|
||
// This middleware will apply to the HTTP | ||
// endpoints as well | ||
opts.Policies.AutoApplyTransactions(); | ||
|
||
// Setting up the outbox on all locally handled | ||
// background tasks | ||
opts.Policies.UseDurableLocalQueues(); | ||
}); | ||
|
||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddOpenApiDocument(); | ||
//builder.Services.AddSwaggerDocument(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseOpenApi(); // serve documents (same as app.UseSwagger()) | ||
app.UseSwaggerUi(); | ||
//app.UseReDoc(); // serve ReDoc UI | ||
|
||
|
||
// Let's add in Wolverine HTTP endpoints to the routing tree | ||
app.MapWolverineEndpoints(); | ||
|
||
// TODO Investigate if this is a dotnet-getdocument issue | ||
args = args.Where(arg => !arg.StartsWith("--applicationName")).ToArray(); | ||
|
||
return await app.RunOaktonCommands(args); | ||
|
This file contains 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,41 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:16225", | ||
"sslPort": 44302 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5283", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7250;http://localhost:5283", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains 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,32 @@ | ||
using JasperFx.Core; | ||
using Wolverine.Http; | ||
using Wolverine.Marten; | ||
|
||
namespace NSwagDemonstrator; | ||
|
||
public record CreateTodoListRequest(string Title); | ||
|
||
public class TodoList | ||
{ | ||
|
||
} | ||
|
||
public record TodoListCreated(Guid ListId, string Title); | ||
|
||
public static class TodoListEndpoint | ||
{ | ||
[WolverinePost("/api/todo-lists")] | ||
public static (IResult, IStartStream) CreateTodoList( | ||
CreateTodoListRequest request, | ||
HttpRequest req | ||
) | ||
{ | ||
var listId = CombGuidIdGeneration.NewGuid(); | ||
var result = new TodoListCreated(listId, request.Title); | ||
var startStream = MartenOps.StartStream<TodoList>(result); | ||
var response = Results.Created("api/todo-lists/" + listId, result); | ||
|
||
return (response, startStream); | ||
} | ||
} | ||
|
This file contains 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,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains 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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains 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 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 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