Skip to content

Commit 752af56

Browse files
.NET 10 preparation
Cherry-pick changes from #231 ahead of updating to .NET 10 in November.
1 parent 2c69637 commit 752af56

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/TodoApp/ApiEndpoints.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Martin Costello, 2024. All rights reserved.
22
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
33

4+
using System.Text.Json.Serialization;
45
using Microsoft.AspNetCore.Http.HttpResults;
56
using Microsoft.AspNetCore.Mvc;
67
using Microsoft.EntityFrameworkCore;
@@ -33,6 +34,15 @@ public static IServiceCollection AddTodoApi(this IServiceCollection services)
3334
services.AddScoped<ITodoRepository, TodoRepository>();
3435
services.AddScoped<ITodoService, TodoService>();
3536

37+
// Use the same JSON serializer options for OpenAPI as for the API itself
38+
services.ConfigureHttpJsonOptions(options =>
39+
{
40+
options.SerializerOptions.DefaultIgnoreCondition = TodoJsonSerializerContext.Default.Options.DefaultIgnoreCondition;
41+
options.SerializerOptions.NumberHandling = TodoJsonSerializerContext.Default.Options.NumberHandling;
42+
options.SerializerOptions.PropertyNamingPolicy = TodoJsonSerializerContext.Default.Options.PropertyNamingPolicy;
43+
options.SerializerOptions.WriteIndented = TodoJsonSerializerContext.Default.Options.WriteIndented;
44+
});
45+
3646
// Configure an EFCore data context to store the Todos backed by SQLite
3747
services.AddDbContext<TodoContext>((serviceProvider, options) =>
3848
{

src/TodoApp/OpenApi/ExamplesProcessor.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected void Process(OpenApiOperation operation, ApiDescription description)
3434
TryAddRequestExamples(body, description, examples);
3535
}
3636

37-
TryAddResponseExamples(operation.Responses, description, examples);
37+
TryAddResponseExamples(operation.Responses ?? [], description, examples);
3838
}
3939

4040
protected void Process(OpenApiSchema schema, Type type)
@@ -81,8 +81,7 @@ private static void TryAddParameterExamples(
8181
if (metadata?.GenerateExample(Context) is { } value)
8282
{
8383
// Find the parameter that corresponds to the argument and set its example
84-
var parameter = parameters.FirstOrDefault((p) => p.Name == argument.Name);
85-
if (parameter is not null)
84+
if (parameters.FirstOrDefault((p) => p.Name == argument.Name) is OpenApiParameter parameter)
8685
{
8786
parameter.Example ??= value;
8887
}

src/TodoApp/OpenApi/Swashbuckle/AddDocumentTagsFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class AddDocumentTagsFilter : IDocumentFilter
1313
{
1414
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
1515
{
16+
swaggerDoc.Tags ??= [];
1617
swaggerDoc.Tags.Add(new() { Name = "TodoApp" });
1718
}
1819
}

src/TodoApp/TodoJsonSerializerContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace TodoApp;
2020
[JsonSerializable(typeof(TodoListViewModel))]
2121
[JsonSourceGenerationOptions(
2222
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
23+
NumberHandling = JsonNumberHandling.Strict,
2324
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
2425
WriteIndented = true)]
2526
public sealed partial class TodoJsonSerializerContext : JsonSerializerContext;

0 commit comments

Comments
 (0)