Reputation: 611
I have this controller in my asp.net-core web-api project:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public ActionResult<List<ActualModel>> Get()
{
return new List<ActualModel>()
{
new ActualModel { StringValue1 = "SomeString" },
new ActualModel { StringValue1 = "MoreString" }
};
}
}
The only method in this controller returns a list of "ActualModel". The definition of it looks like this:
public class ActualModel
{
public string StringValue1 { get; set; }
}
And my swashbuckle
generated Swagger-UI shows it like this:
But I would like the Swashbuckle
to generate the Swagger-Specification so that it not contains the "ActualModel" but the following model instead:
public class OtherModel
{
public string StringValue2 { get; set; }
public int IntValue1 { get; set; }
}
I've noticed there is a function that I think I can use for this:
services.AddSwaggerGen(c =>
{
c.MapType<ActualModel>(() => new OpenApiSchema { Type = "string" });
But I can't figure out how I get the "OtherModel" to be generated into an OpenApiSchema
so that it replaces the "ActualModel" nor can I find information or examples about it.
I tried giving it the full class name with namespace and without the namespace, I googled for "Swashbuckle replace Type with other Type" and "Swashbuckle MapType" but I cant find any examples which show how to simply replace one type with another type that is defined in the application.
Background: The above is just an example of course. In a "real" project I have generic and hierarchical class-definitions that I want to return in my controller but I have a Serializer in between the value the controller returns and the actual network output which changes the data structure to another, more simple structure for use on a Typescript side. That's why I want to replace the swashbuckle generated model definitions.
Upvotes: 0
Views: 1728
Reputation: 77304
Well, you could just tell swagger what you are producing if for example it's not obvious because of templates or a hierarchy:
[HttpGet]
[ProducesResponseType(typeof(OtherModel[]), 200)] // <--- This line
public ActionResult<List<ActualModel>> Get()
{
return new List<ActualModel>()
{
new ActualModel { StringValue1 = "SomeString" },
new ActualModel { StringValue1 = "MoreString" }
};
}
However, it's your job to make sure they are actually compatible, there are no checks.
Upvotes: 4