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

SwashbuckleXml.zip

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 22 (5 by maintainers)

Most upvoted comments

@joegithub @GeorgyOkov Finally I got it working with Xml. Following is the CustomXmlSchemaFilter that handles return of collection, collection property in POCO.

public class CustomXmlSchemaFilter : ISchemaFilter
    {
        private const string _SCHEMA_ARRAY_TYPE = "array";
        private const string _SCHEMA_STRING_TYPE = "string";
        private const string _PREFIX_ARRAY = "ArrayOf";

        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (context.ApiModel.Type.IsValueType)
                return;

            if (schema.Type == _SCHEMA_STRING_TYPE)
                return;

            schema.Xml = new OpenApiXml
            {
                Name = context.ApiModel.Type.Name
            };

            if (schema.Type == _SCHEMA_ARRAY_TYPE && schema.Items.Reference != null)
            {
                schema.Xml = new OpenApiXml
                {
                    Name = $"{_PREFIX_ARRAY}{schema.Items.Reference.Id}",
                    Wrapped = true,
                };
            }

            if (schema.Properties == null)
            {
                return;
            }

            foreach (var property in schema.Properties.Where(x => x.Value.Type == _SCHEMA_ARRAY_TYPE))
            {
                property.Value.Items.Xml = new OpenApiXml
                {
                    Name = property.Value.Items.Type,
                };
                property.Value.Xml = new OpenApiXml
                {
                    Name = property.Key,
                    Wrapped = true
                };
            }
        }
    }

Specify to use this Schema filter in Swagger via following:

services.AddSwaggerGen(c =>
{
    ...

    c.SchemaFilter<CustomXmlSchemaFilter>();
});

Hope this helps.