Using IOpenApiSchemaTransformer to generate XML example only in specific cases

2 days ago 1
ARTICLE AD BOX

Somewhat related to this question, ASP.NET Core OpenAPI/SwaggerUI XML root collections "XML example cannot be generated; root element name is undefined"

I'm trying to look at using IOpenApiSchemaTransformer to manually create the XML examples when the request or response object returned is a Collection<T>. I support different data formats by using media types (e.g., application/vnd.example.weatherforecast+json and application/vnd.example.weatherforecast+xml)

In my TransformAsync implementation I can pretty easily tell when the object being returned is one I need to worry about by looking at OpenApiSchemaTransformerContext.JsonTypeInfo.Type but I'm noticing that the transform is invoked several times for the same types, I assume because its running per defined media type, but I can't seem to figure out when its asking for the XML schema. Any ideas?

Using Microsoft.AspNetCore.OpenApi 10.0.4 and Swashbuckle.AspNetCore.SwaggerUI 10.1.5 on .Net 10.

Read Entire Article