Swashbuckle.WebApi: Wrong Xml example data generation
Hi all, i have a problem with auto-generated Xml example value on Swagger-UI, the xml structure is invalid and can’t be used to test api
Steps to reproduce the problem:
- Create a new Web Api 2 project using VS2015. New -> Project -> Asp.Net Web Application -> WebApi
- Add a class with collection like this
public class Foo
{
public string Name { get; set; }
public int Code { get; set; }
public IList<FooItem> Items { get; set; }
}
public class FooItem
{
public string ItemName { get; set; }
public int ItemCode { get; set; }
}
- Modify the controller ValuesController.cs
public void Post([FromBody]IList<Foo> list)
{
}
- Now look the Xml sample data generated by standard api help page of asp.net web api projeject
<ArrayOfFoo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SwashbuckleXml.Models">
<Foo>
<Code>2</Code>
<Items>
<FooItem>
<ItemCode>2</ItemCode>
<ItemName>sample string 1</ItemName>
</FooItem>
<FooItem>
<ItemCode>2</ItemCode>
<ItemName>sample string 1</ItemName>
</FooItem>
</Items>
<Name>sample string 1</Name>
</Foo>
<Foo>
<Code>2</Code>
<Items>
<FooItem>
<ItemCode>2</ItemCode>
<ItemName>sample string 1</ItemName>
</FooItem>
<FooItem>
<ItemCode>2</ItemCode>
<ItemName>sample string 1</ItemName>
</FooItem>
</Items>
<Name>sample string 1</Name>
</Foo>
</ArrayOfFoo>
and the Xml sample data generated from Swagger UI
<?xml version="1.0"?>
<Foo>
<Name>string</Name>
<Code>1</Code>
<Items>
<ItemName>string</ItemName>
<ItemCode>1</ItemCode>
</Items>
</Foo>
- As you can see the Xml generated from swagger don’t has Foo root collection and FooItem element
- You can’t use the auto-generated xml sample data on swagger UI, the format is not valid
Any suggestions?
Thanks in advance, Diego
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 22 (5 by maintainers)
@joegithub @GeorgyOkov Finally I got it working with Xml. Following is the
CustomXmlSchemaFilterthat handles return of collection, collection property in POCO.Specify to use this Schema filter in Swagger via following:
Hope this helps.