I have an ASP.Net Web API with XML documentation throughout, including many <see />
tags.
I’ve recently added Swagger (Swashbuckle UI stuff) to the solution, and noticed that it doesn’t handle XML tags like <see />
. After looking around online I found this – https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/57, so it seems like the devs don’t want to fix this.
Does anybody have any suggestions? I tried running a little .exe to replace all tags in the generated XML file with their object names (e.g. <see cref="MyObject"/>
becomes MyObject
), but this kept screwing up and even when I did it manually Swagger for some reason didn’t pick up the changes (is the XML loaded into memory somewhere?)
2
Answers
Following on from Kit's suggestion, I found a way to retrieve and change the documentation, but needed a way to parse XML tags. I found this which, although it refers to HTML, removes XML tags just as well.
So, in the order they are required:
XML documentation comments are generated into a file with the same name as the assembly in the same folder (usually). You could parse that file with the XDocument and related classes, and then use a schema filter (
ISchemaFilter
) to set the appropriate properties on the Open API schema that Swashbuckle exposes.I’m unsure of what schema element would be appropriate for a "see also" or "related resources" functionality. You would need to investigate that (perhaps a custom property in ASP.NET Core’s Open API implementation, or by appending text to a description property).
As to
Yes. Swashbuckle itself parses this XML to do all that it currently does. Roughly speaking it builds it into a document object model (DOM) built on top of Microsoft’s Open API implementation mentioned above. Using a schema filter will give you access to that DOM.