Skip to content

Commit d83a64f

Browse files
authored
Add UseFullTypeNameSchemaIds extension for OpenAPI schema reference IDs (#291)
2 parents 93c0d97 + 8dfe695 commit d83a64f

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

samples/TinyHelpers.AspNetCore.Sample/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868

6969
// Add time examples for TimeSpan and TimeOnly fields.
7070
options.AddTimeExamples();
71+
72+
// Uncomment to use full type names (including namespace) for schema IDs.
73+
// This helps avoid naming collisions when multiple types have the same name.
74+
// options.UseFullTypeNameSchemaIds();
7175
});
7276

7377
// Add default problem details and exception handler.

src/TinyHelpers.AspNetCore/OpenApi/OpenApiExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ public static OpenApiOptions EnableEnumSupport(this OpenApiOptions options)
5454
=> options.AddSchemaTransformer<EnumSchemaTransformer>();
5555
#endif
5656

57+
/// <summary>
58+
/// Configures the OpenAPI schema reference IDs to use the full type name (including namespace)
59+
/// instead of just the type name. This helps avoid naming collisions when multiple types have the same name.
60+
/// </summary>
61+
/// <param name="options">The <see cref="OpenApiOptions"/> to configure.</param>
62+
/// <returns>The <see cref="OpenApiOptions"/> instance for further customization.</returns>
63+
/// <remarks>
64+
/// By default, OpenAPI uses only the type name for schema references. This extension method changes
65+
/// the behavior to use the full type name (namespace + type name) to ensure unique schema IDs.
66+
/// </remarks>
67+
public static OpenApiOptions UseFullTypeNameSchemaIds(this OpenApiOptions options)
68+
{
69+
ArgumentNullException.ThrowIfNull(options);
70+
71+
options.CreateSchemaReferenceId = (jsonTypeInfo) =>
72+
{
73+
// Get the full type name (including namespace) for the schema ID
74+
var fullName = jsonTypeInfo.Type.FullName;
75+
76+
// Replace + with . for nested types to make the schema ID more readable
77+
return fullName?.Replace('+', '.');
78+
};
79+
80+
return options;
81+
}
82+
5783
}
5884

5985
#endif

src/TinyHelpers.AspNetCore/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ builder.Services.AddOpenApi(options =>
7373
});
7474
```
7575

76+
- `UseFullTypeNameSchemaIds()`: configures OpenAPI to use the full type name (including namespace) for schema reference IDs, helping to avoid naming collisions when multiple types have the same name:
77+
78+
```csharp
79+
builder.Services.AddOpenApi(options =>
80+
{
81+
options.UseFullTypeNameSchemaIds();
82+
});
83+
```
84+
85+
This is particularly useful when you have multiple types with the same name in different namespaces. For example, if you have both `MyApp.Models.User` and `MyApp.DTOs.User`, the default behavior would create a single schema named "User", potentially causing conflicts. With `UseFullTypeNameSchemaIds()`, the schemas will be named "MyApp.Models.User" and "MyApp.DTOs.User" respectively.
86+
7687
**Contribute**
7788

7889
The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests on the repo and we'll address them as we can.

0 commit comments

Comments
 (0)