|
1 | 1 | #if NET9_0_OR_GREATER |
2 | 2 |
|
3 | | -using Microsoft.AspNetCore.Builder; |
4 | | -using Microsoft.AspNetCore.Http; |
5 | 3 | using Microsoft.AspNetCore.OpenApi; |
6 | 4 | using Microsoft.Extensions.DependencyInjection; |
7 | | -using Microsoft.OpenApi; |
8 | 5 | using TinyHelpers.AspNetCore.OpenApi.Transformers; |
9 | 6 |
|
10 | 7 | namespace TinyHelpers.AspNetCore.OpenApi; |
@@ -76,17 +73,33 @@ public OpenApiOptions UseFullTypeNameSchemaIds() |
76 | 73 | { |
77 | 74 | ArgumentNullException.ThrowIfNull(options); |
78 | 75 |
|
79 | | - options.CreateSchemaReferenceId = (jsonTypeInfo) => |
| 76 | + options.CreateSchemaReferenceId = jsonTypeInfo => |
80 | 77 | { |
81 | | - // Get the full type name (including namespace) for the schema ID |
82 | | - var fullName = jsonTypeInfo.Type.FullName; |
83 | | - |
84 | | - // Replace + with . for nested types to make the schema ID more readable |
85 | | - return fullName?.Replace('+', '.'); |
| 78 | + // Create the default ID (handles generics, nested types, etc.) |
| 79 | + var defaultId = OpenApiOptions.CreateDefaultSchemaReferenceId(jsonTypeInfo); |
| 80 | + if (string.IsNullOrEmpty(defaultId)) |
| 81 | + { |
| 82 | + return defaultId; |
| 83 | + } |
| 84 | + |
| 85 | + var @namespace = jsonTypeInfo.Type.Namespace; |
| 86 | + if (string.IsNullOrEmpty(@namespace)) |
| 87 | + { |
| 88 | + // If there's no namespace, just keep the default. |
| 89 | + return defaultId; |
| 90 | + } |
| 91 | + |
| 92 | + // Include namespace in the reference ID. |
| 93 | + return $"{@namespace}.{defaultId}"; |
86 | 94 | }; |
87 | 95 |
|
88 | 96 | return options; |
89 | 97 | } |
| 98 | + |
| 99 | +#if NET10_0_OR_GREATER |
| 100 | + public OpenApiOptions UseStrictNumericSchemas() |
| 101 | + => options.AddSchemaTransformer<StrictNumericSchemaTransformer>(); |
| 102 | +#endif |
90 | 103 | } |
91 | 104 | } |
92 | 105 |
|
|
0 commit comments