Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/TodoApp/ApiEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Martin Costello, 2024. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -33,6 +34,15 @@ public static IServiceCollection AddTodoApi(this IServiceCollection services)
services.AddScoped<ITodoRepository, TodoRepository>();
services.AddScoped<ITodoService, TodoService>();

// Use the same JSON serializer options for OpenAPI as for the API itself
services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.DefaultIgnoreCondition = TodoJsonSerializerContext.Default.Options.DefaultIgnoreCondition;
options.SerializerOptions.NumberHandling = TodoJsonSerializerContext.Default.Options.NumberHandling;
options.SerializerOptions.PropertyNamingPolicy = TodoJsonSerializerContext.Default.Options.PropertyNamingPolicy;
options.SerializerOptions.WriteIndented = TodoJsonSerializerContext.Default.Options.WriteIndented;
});

// Configure an EFCore data context to store the Todos backed by SQLite
services.AddDbContext<TodoContext>((serviceProvider, options) =>
{
Expand Down
5 changes: 2 additions & 3 deletions src/TodoApp/OpenApi/ExamplesProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void Process(OpenApiOperation operation, ApiDescription description)
TryAddRequestExamples(body, description, examples);
}

TryAddResponseExamples(operation.Responses, description, examples);
TryAddResponseExamples(operation.Responses ?? [], description, examples);
}

protected void Process(OpenApiSchema schema, Type type)
Expand Down Expand Up @@ -81,8 +81,7 @@ private static void TryAddParameterExamples(
if (metadata?.GenerateExample(Context) is { } value)
{
// Find the parameter that corresponds to the argument and set its example
var parameter = parameters.FirstOrDefault((p) => p.Name == argument.Name);
if (parameter is not null)
if (parameters.FirstOrDefault((p) => p.Name == argument.Name) is OpenApiParameter parameter)
{
parameter.Example ??= value;
}
Expand Down
1 change: 1 addition & 0 deletions src/TodoApp/OpenApi/Swashbuckle/AddDocumentTagsFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class AddDocumentTagsFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Tags ??= [];
swaggerDoc.Tags.Add(new() { Name = "TodoApp" });
}
}
1 change: 1 addition & 0 deletions src/TodoApp/TodoJsonSerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace TodoApp;
[JsonSerializable(typeof(TodoListViewModel))]
[JsonSourceGenerationOptions(
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.Strict,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
WriteIndented = true)]
public sealed partial class TodoJsonSerializerContext : JsonSerializerContext;