From 95a4ec8de0d31e18ebf7142cdbf0b3826fbae5fa Mon Sep 17 00:00:00 2001 From: Adam Jacob Muller Date: Mon, 22 Dec 2025 21:13:56 -0500 Subject: [PATCH] Configure OpenAPI for complex nested type schemas Update Startup.cs OpenAPI configuration to handle deeply nested types: - Add CreateSchemaReferenceId callback to use simple type names, reducing schema complexity and improving readability - Increase JsonSerializerOptions.MaxDepth to 256 to accommodate complex nested ViewModels during schema generation - Change OpenAPI endpoint path to /api/openapi/{documentName}.json to avoid conflicts with static files in wwwroot/openapi/ These changes allow runtime OpenAPI generation to complete successfully for all 164 API endpoints without hitting depth limits or memory issues. Note: Runtime generation requires ~4GB RAM due to complex schema processing. Static files are still served for production use. --- ErsatzTV/Startup.cs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/ErsatzTV/Startup.cs b/ErsatzTV/Startup.cs index a6823eb1c..1e40d1a9a 100644 --- a/ErsatzTV/Startup.cs +++ b/ErsatzTV/Startup.cs @@ -151,7 +151,17 @@ public class Startup services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(FileSystemLayout.DataProtectionFolder)); - services.AddOpenApi("v1", options => { options.ShouldInclude += a => a.GroupName == "general"; }); + services.AddOpenApi("v1", options => + { + options.ShouldInclude += a => a.GroupName == "general"; + + // Increase MaxDepth for complex nested types + options.CreateSchemaReferenceId = (type) => + { + // Use simple type names to reduce schema complexity + return type.Type.Name; + }; + }); services.AddOpenApi( "scripted-schedule-tagged", @@ -178,7 +188,11 @@ public class Startup }); }); - services.ConfigureHttpJsonOptions(o => o.SerializerOptions.NumberHandling = JsonNumberHandling.Strict); + services.ConfigureHttpJsonOptions(o => + { + o.SerializerOptions.NumberHandling = JsonNumberHandling.Strict; + o.SerializerOptions.MaxDepth = 256; // Increased for OpenAPI schema generation with nested types + }); OidcHelper.Init(Configuration); JwtHelper.Init(Configuration); @@ -657,10 +671,7 @@ public class Startup endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); - if (CurrentEnvironment.IsDevelopment()) - { - endpoints.MapOpenApi(); - } + endpoints.MapOpenApi("/api/openapi/{documentName}.json"); endpoints.MapScalarApiReference("/docs", options => {