-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
ASP.NET web core API - incorrect default xml serialization in Swagger #39064
Comments
I tried to reproduce the issue. Below is an API setup that does not properly render an xml response document in the swagger-ui. There are two problems:
// POST api/<ValuesController>
[HttpPost]
[Produces("application/xml")]
[Consumes("application/xml")]
public async Task<ActionResult<IEnumerable<DataReply>>> Post([FromBody] DataRequest dataRequest)
{
_dbContext.DataReplies.Add(new DataReply
{
FromDate = dataRequest.FromDate,
ToDate = dataRequest.ToDate,
});
await _dbContext.SaveChangesAsync();
return await _dbContext.DataReplies.ToListAsync();
}
// Models
[DataContract]
public class DataReply
{
public int DataReplyId { get; set; }
[DataMember]
public DateTime? FromDate { get; set; }
[DataMember]
public DateTime? ToDate { get; set; }
}
[DataContract]
public class DataRequest
{
[DataMember]
[Required]
public DateTime? FromDate { get; set; }
[DataMember]
[Required]
public DateTime? ToDate { get; set; }
} This results in the following issue in swagger-ui : I tried to track down the swagger-ui issue and it appears that it's an existing issue with the dependency that Swashbuckle is using for the UI. See the following issue: swagger-api/swagger-ui#4650 · For the second issue of CamelCasing in the request that results in improper binding, from my preliminary research, this might be a bug. When you add the `[Required] attribute, there are validation failures. After removing the |
Thanks for contacting us. We're moving this issue to the |
After some further investigations we found that the binding failures are due to the fact that XML is case sensitive. In the case above Shashbuckle is using the JSON camelCase serialization. You can change the behavior globally by adding the following configuration: builder.Services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(opts =>
{
opts.JsonSerializerOptions.PropertyNamingPolicy = null;
}); I will close this issue since there is no further action on our side. |
This issue has been moved from a ticket on Developer Community.
I'm using swagger/index page for testing my API. I need both JSON and XML serialization. Json is working fine but the xml serialization generated by swagger contains non-capital letters of node names for capital letter started properties of my [DataContract].
As result, request instance is incorrect - all dates are 01.01.0001 ... (see attached picture "xml serialization in swagger.png")
Obviously, the problem is a incorrect node names - fromDate and toDate. The correct names must be FromDate and ToDate accordingly. The workout is rename these tags manually but much more better to fix the swagger behaviour for XML serialization.
Thanks a lot.
Original Comments
Feedback Bot on 11/14/2021, 11:25 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Feedback Bot on 12/15/2021, 11:52 AM:
This issue is currently being investigated. Our team will get back to you if either more information is needed, a workaround is available, or the issue is resolved.
The text was updated successfully, but these errors were encountered: