.NET 9 OpenAPI: How to rename the schemas? #59985
Replies: 1 comment
-
Ended up coming up with this solution: [DisplayName("UserProfile")]
public class UserProfileDto
{
...
}
public class OpenApiModifier
{
public static string? DisplayNameOrDefaultSchemaRefId(JsonTypeInfo jsonTypeInfo)
{
var displayName = jsonTypeInfo.Type.GetCustomAttributes<DisplayNameAttribute>().SingleOrDefault()?.DisplayName;
if (displayName != null)
{
return displayName;
}
return OpenApiOptions.CreateDefaultSchemaReferenceId(jsonTypeInfo);
}
}
builder.Services.AddOpenApi(options =>
{
options.CreateSchemaReferenceId = OpenApiModifier.DisplayNameOrDefaultSchemaRefId;
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have classes that are named like FooDto and BarDto. I want to be able to remove the Dto in the result OpenAPI spec.
I was looking into AddSchemaTransformer but I can't figure out how to change the schema name.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions