Last week I’ve encountered an interesting bug in swagger documentation of our API. For some reason couple of Get operations were missing some of the query parameters. The issue itself was only visible in a couple of controllers, simplified version of one of them is shown below
1 2 3 4 5 6 7 8 |
public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get([FromUri] FullAvailabilityRequest request) { return new string[] {"value1", "value2"}; } } |
As you can see the controller has a Get method which takes as an argument following class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class FullAvailabilityRequest : AvailabilityRequest { public string TermsOfService { get; set; } } [DataContract] public class AvailabilityRequest { [DataMember] public string Origin { get; set; } [DataMember] public string Destination { get; set; } } |
If you run swagger for Get operation you will see that only two parameters are listed in a parameter list
After some digging I found the actual problem were basically [DataMember] and [DataContract] attributes randomly scattered in the models used in Get methods arguments. Even though these attributes are not used for model binding from the query string, swagger takes them into account either way. It happens basically because swagger under the hood uses JsonObjectContract (even for [FromUri] request classes) and by default it respects [DataContract] attribute – more info can be found here. Removing redundant [DataContract] and [DataMember] attributes from respective models solved the issue.
PS. This particular corner case is not an issue for ASP.NET Core swagger implementation.