XML namespace not in swagger definition
See original GitHub issueMy client wishes to use an API I develop with both JSON and XML in/output. The XML should have a namespace, because it will be used from BizTalk server. When I define a DataContract for my request and response, I can add the Namespace property to the DataContract attribute. Using the Swagger UI I am able to test the service and it does indeed require the namespace and sends the namespace in the response.
The problem is, the namespace is not shown in the XML definition in the Swagger UI. This means that the example messages shown, are not correct and will not work. This also causes a problem when using API management, since that will use the Swagger definition for metadata. When a developer will use the API from API management (or adds a reference), it will send incorrect messages.
DataContract definition:
[DataContract( Name= "root", Namespace = "http://example.com/api/V1")]
public class root
{.....
Example message ACCEPTED by web api:
<root xmlns="http://example.com/api/V1"> <someNode>abc</someNode> </root>
Example message from Swagger UI (NOT ACCEPTED by web api):
<root> <someNode>abc</someNode> </root>
Am I missing something or is this a limitation of Swashbuckle/Swagger?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (1 by maintainers)
In case anyone else runs into these issues, here’s how I got around them. They may or may not be desirable depending on your use case, but these were fine for mine.
Specify the full namespace and class name in the XMLRoot and DataContract attributes.
Specify an empty string for the Namespace parameter for the XMLRoot and DataContract attributes.
[System.Runtime.Serialization.DataContract(Name = "MyNameSpace.AppArea.SomeClass", Namespace = "")] [System.Xml.Serialization.XmlRoot("MyNameSpace.AppArea.SomeClass", Namespace = "")] public class SomeClass { }
I can share my more/less completed solution. I suppose it can fit 90% (maybe more) of your APIs, but maybe you need to extend something (i.e. add namespaces), so you’re welcome! https://gist.github.com/dsharkou/10633526b93458c7b8fc9fb958644cd0