Reputation: 171
I have an endpoint to query details about a "person". Problem, the response could be of type "PhysicalPerson" or "LegalPerson". Both inherit the same "BasePerson" class.
Here is my current code:
[HttpGet("personne/{idpersonne}")]
[ProducesResponseType(typeof(PersonneMoraleType), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(PersonnePhysiqueType), (int)HttpStatusCode.OK)]
public async Task<ActionResult> GetPerson(string idpersonne, [FromQuery] bool avecDocuments = false)
{
return Ok(await _app.LraConnector.LirePersonne(idpersonne, avecDocuments));
}
Problem, swagger only describe the second type (physical), but if I answer a LegalPerson, it throw a scheme exception. If I use the "BasePerson", both answers works fine, but it's not described properly.
Is there something I can do to improve that?
Upvotes: 7
Views: 8744
Reputation: 398
I was looking for an answer to this today and came across this: https://mattfrear.com/2015/04/21/generating-swagger-example-responses-with-swashbuckle/.
Essentially, you can add multiple SwaggerResponse
decorators to your controller method for multiple types, even with the same Http status code. This worked for me.
Upvotes: 0
Reputation: 92
I don't think this is supported by swagger because iirc the responses is a dictionary with the statuscode as the key.
If you change one of the responses to return anything else than OK, then it will return your model.
However I dont think this is what you want. You could create one model like this:
public class Person
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public PhysicalPerson PhysicalPerson { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public OtherPerson OtherPerson { get; set; }
}
And then bind you data to the correct property and return this object as the 200 response.
Upvotes: 1