Search code examples
xmlvb.netswashbuckle.net-4.8swashbuckle.examples

Swashbuckle Example Data Type not showing correct markup


I am trying to get my SwashBuckle/Swagger GUI to show the correct elements based on how I set it in code. Currently the example output looks like this:

    <?xml version="1.0"?>
    <SomesModel>
     <_Somes>
       <_SomeID>string</_SomeID>
       <_SomeName>string</_SomeName>
       <_SomeBool>true</_SomeBool>
     </_Somes>
    </SomesModel>

My current code structure looks like this:

WepApiConfig

    config.Formatters.XmlFormatter.UseXmlSerializer = True
    SwaggerConfig.Register(config)

Controller code

    <HttpPost>
    Public Function PostGeneric(some As SomesModel) As HttpResponseMessage
        Return New HttpResponseMessage(Net.HttpStatusCode.Accepted)
    End Function

Root XML object

    <Serializable()>
    <XmlRoot(ElementName:="Somes", [Namespace]:="")>
    <XmlType("Somes")>
    Public Class SomesModel
     <XmlElement(ElementName:="Some")>
     Public Property Somes As List(Of SomeModel)
    End Class

Sub XML Object

   <Serializable()>
   <XmlType("Some")>
   Public Class SomeModel
     <XmlElement>
     Public Property SomeID As String
     <XmlElement>
     Public Property SomeName As String
     <XmlElement>
     Public Property SomeBool As Boolean
   End Class

I specify all the annotations that I normally do when serializing a xml. What extra settings does SwashBuckle need to get the xml serialized correctly?

I have tried editing the xml annotations, but it seems like Swashbuckle just ignores them. Some stack overflow suggestions was to try XMLType and others suggested using XMLRoot and XmlElements. I have tried all combinations of those.

What I would expect it to look like is this as the output.

    <?xml version="1.0"?>
    <Somes>
      <_Some>
        <_SomeID>string</_SomeID>
        <_SomeName>string</_SomeName>
        <_SomeBool>true</_SomeBool>
      </_Some>
     <_Some>
       <_SomeID>string</_SomeID>
       <_SomeName>string</_SomeName>
       <_SomeBool>true</_SomeBool>
     </_Some>
    </Somes>

Solution

  • After a long time digging I found this

    The solution is to create a class that Implements ISchemaFilter My apply method had this structure

    Public Sub Apply(schema As Schema, registry As SchemaRegistry, type As Type) Implements ISchemaFilter.Apply
    

    You get the type.GetCustomAttributes(True) to get all the classes custom attributes and find the one you are looking for and use the xml element like this

    Dim castedAttr = DirectCast(attr, XmlRootAttribute)
    schema.xml = New Xml With
    {
       .name = castedAttr.ElementName
    }
    

    Next you go through the type.Properties() and then go through the CustomAttributes for each property and you set the xml element like

    schema.properties(property.Name).xml  = New Xml With
    {
       .name = castedAttr.ElementName
    }
    

    Last you go into your SwaggerConfig class and add

    c.SchemaFilter(Of YourNewClass)()