From 15382f85657c5633783628093178caccb8ec881b Mon Sep 17 00:00:00 2001 From: Adrian Hall Date: Wed, 16 Jul 2025 10:41:57 -0700 Subject: [PATCH] (#314) Updated sample server to support OpenAPI 9.x --- docs/in-depth/server/openapi/net9.md | 5 +++-- .../Controllers/TodoItemController.cs | 1 + .../Controllers/TodoListController.cs | 1 + .../src/Sample.Datasync.Server/Program.cs | 9 ++++++--- .../Sample.Datasync.Server.csproj | 19 +++++++++++-------- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docs/in-depth/server/openapi/net9.md b/docs/in-depth/server/openapi/net9.md index f7657e1d..348eda4a 100644 --- a/docs/in-depth/server/openapi/net9.md +++ b/docs/in-depth/server/openapi/net9.md @@ -11,8 +11,9 @@ Follow [the basic instructions for OpenApi integration](https://learn.microsoft. using CommunityToolkit.Datasync.Server.OpenApi; -3. Add a service to generate an OpenAPI definition to your `Program.cs` file: +3. Add services to generate an OpenAPI definition to your `Program.cs` file: + builder.Services.AddEndpointsApiExplorer(); builder.Services.AddOpenApi(options => options.AddDatasyncTransformers()); 4. Enable the middleware for serving the generated JSON document and the Swagger UI, also in `Program.cs`: @@ -27,4 +28,4 @@ Browsing to the `/openapi/v1.json` endpoint of the web service allows you to dow ## Known issues -The .NET 9.x OpenApi support currently does not support dynamic schema generation. This means that the schema generated within the OpenApi document will be incomplete. \ No newline at end of file +The .NET 9.x OpenApi support currently does not support dynamic schema generation. This means that the schema generated within the OpenApi document will be incomplete. diff --git a/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoItemController.cs b/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoItemController.cs index 2543b2de..6a6badba 100644 --- a/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoItemController.cs +++ b/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoItemController.cs @@ -10,6 +10,7 @@ namespace Sample.Datasync.Server.Controllers; [Route("tables/[controller]")] +[ApiExplorerSettings(IgnoreApi = false)] public class TodoItemController : TableController { public TodoItemController(AppDbContext context) diff --git a/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoListController.cs b/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoListController.cs index 28d5dca2..830eeaa3 100644 --- a/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoListController.cs +++ b/samples/datasync-server/src/Sample.Datasync.Server/Controllers/TodoListController.cs @@ -10,6 +10,7 @@ namespace Sample.Datasync.Server.Controllers; [Route("tables/[controller]")] +[ApiExplorerSettings(IgnoreApi = false)] public class TodoListController : TableController { public TodoListController(AppDbContext context) : base(new EntityTableRepository(context)) diff --git a/samples/datasync-server/src/Sample.Datasync.Server/Program.cs b/samples/datasync-server/src/Sample.Datasync.Server/Program.cs index 53df7457..e9b81fcb 100644 --- a/samples/datasync-server/src/Sample.Datasync.Server/Program.cs +++ b/samples/datasync-server/src/Sample.Datasync.Server/Program.cs @@ -6,6 +6,7 @@ using CommunityToolkit.Datasync.Server.NSwag; using CommunityToolkit.Datasync.Server.OpenApi; using CommunityToolkit.Datasync.Server.Swashbuckle; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Sample.Datasync.Server.Db; @@ -36,6 +37,8 @@ if (openApiEnabled) { + // Explicit API Explorer configuration + _ = builder.Services.AddEndpointsApiExplorer(); _ = builder.Services.AddOpenApi(options => options.AddDatasyncTransformers()); } @@ -60,13 +63,13 @@ _ = app.UseSwagger().UseSwaggerUI(); } +app.UseAuthorization(); +app.MapControllers(); + if (openApiEnabled) { _ = app.MapOpenApi(pattern: "swagger/{documentName}/swagger.json"); _ = app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample.Datasync.Server v1")); } -app.UseAuthorization(); -app.MapControllers(); - app.Run(); diff --git a/samples/datasync-server/src/Sample.Datasync.Server/Sample.Datasync.Server.csproj b/samples/datasync-server/src/Sample.Datasync.Server/Sample.Datasync.Server.csproj index 06b2470e..f0c61a3f 100644 --- a/samples/datasync-server/src/Sample.Datasync.Server/Sample.Datasync.Server.csproj +++ b/samples/datasync-server/src/Sample.Datasync.Server/Sample.Datasync.Server.csproj @@ -7,16 +7,19 @@ - - - - - - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + +