New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON object/array syntax in <example> XML tags not working #2070
Comments
Create a public class ExamplesSchemaFilter : ISchemaFilter
{
private readonly XPathNavigator _xmlNavigator;
public ExamplesSchemaFilter(XPathDocument xmlDoc)
{
_xmlNavigator = xmlDoc.CreateNavigator();
}
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.MemberInfo != null)
{
var fieldOrPropertyMemberName = XmlCommentsNodeNameHelper.GetMemberNameForFieldOrProperty(context.MemberInfo);
var fieldOrPropertyNode = _xmlNavigator.SelectSingleNode($"/doc/members/member[@name='{fieldOrPropertyMemberName}']");
if (fieldOrPropertyNode == null) return;
var exampleNode = fieldOrPropertyNode.SelectSingleNode("example");
if (exampleNode != null
&& schema.ResolveType(context.SchemaRepository) == "string")
{
schema.Example = OpenApiAnyFactory.CreateFromJson(exampleNode.InnerXml) ?? new OpenApiNull();
}
}
}
} configure the filter in startup ConfigureServices: services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApi", Version = "v1" });
Directory.GetFiles(Environment.ContentRootPath, "*.xml", SearchOption.AllDirectories)
.ToList().ForEach(file =>
{
c.IncludeXmlComments(file);
c.SchemaFilter<ExamplesSchemaFilter>(new XPathDocument(file));
});
}); |
Fixed by #2151. Released with |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not able to use the null example tags in XML documentation. as decribed in #1768 (comment)
To ensure the problem is not produced by any special configuration in my project I created a new .NET 5 solution based on the Web API template:
SwaggerTest.csproj:
Startup.cs:
WeatherForecast.cs
The resulting Sagger Documentation does not pickup the null example:
The text was updated successfully, but these errors were encountered: