dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.48k stars 10.04k forks source link

OpenApi custom polymorphic type names #58332

Open koenbeuk opened 1 month ago

koenbeuk commented 1 month ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

We're looking into migrating from Swashbuckle to Microsoft.AspNetCore.OpenApi. There are many benefits but polymorphic type naming is a larger issue for us as it makes generated type names hard to read. In our case we use the base type name as a suffix, e.g.

abstract class JobTemplate { ... }
class FooJobTemplate : JobTemplate { .. }

Generates a type called: JobTemplateFooJobTemplate.

Describe the solution you'd like

Any way to customize the generated schema name. In Swashbuckle we could configure a delegate that took a Type and returned a String.

Additional context

57982 and #58213 also mention this particular issue.

captainsafia commented 1 month ago

@koenbeuk Thanks for filing this issue! The default implementation for generating polymorphic type names use the BaseType as the prefix. We don't yet expose an API for customizing the polymorphic type name, although we can consider adding this in the future. The ship has sailed for introducing a new API in .NET 9.

In the meantime, if it is a feasible approach for you, you may consider removing the suffix in your type names and relying on the prefixing behavior that is built-in.

marinasundstrom commented 2 weeks ago

I want to move from NSwag and I also see this as a problem. They way polymorphism is done doesn't match either NSwag, or Swashbuckle. The OpenAPI specification being generated is incompatible.

marinasundstrom commented 2 weeks ago

My prototype, limited by the naming convention, and other things:

https://github.com/marinasundstrom/NullabilityTransformersPrototype/tree/main/WebApi

Also https://github.com/RicoSuter/NJsonSchema/issues/1739