Andrei
Andrei

Reputation: 44700

Swagger Swashbuckle abstract class documentation

I have Web API request model with property:

public List<Feature> Features { get; set; }

Feature is an abstract class. I'll have many classes derived from it:

public abstract class Feature
{
    public string Title { get; set; }
}

public class ImageFeature : Feature
{
    public string ImageUrl { get; set; }
}

Obviously Swashbuckle only recognizes Feature properties and generates documentation accordingly. How do I explicitly declare possible implementations of Feature class so that Swashbuckle generate proper docs? Is there some attribute I could use, something like:

[SwaggerResponseType(typeof(ImageFeature))]
[SwaggerResponseType(typeof(AnotherFeature))]
public abstract class Feature

Upvotes: 3

Views: 3298

Answers (2)

Helder Sepulveda
Helder Sepulveda

Reputation: 17664

Take a look at this one: http://swashbuckletest.azurewebsites.net/swagger/ui/index#!/InheritanceTest/InheritanceTest_Get

enter image description here

And here is the code behind that controller: https://github.com/heldersepu/SwashbuckleTest/blob/911bf68e0cf6af3ee5d8278e6dd988eda8c4dc8d/Swagger_Test/Controllers/InheritanceTestController.cs

using System.Web.Http;

namespace Swagger_Test.Controllers
{
    public abstract class Feature
    {
        /// <summary>We all need a Title</summary>
        public string Title { get; set; }
    }

    public class ImageFeature : Feature
    {
        /// <summary>Here goes your image URL</summary>
        public string ImageUrl { get; set; }
    }

    public class InheritanceTestController : ApiController
    {
        public ImageFeature Get([FromUri]ImageFeature imageFeature)
        {
            return imageFeature;
        }

        public ImageFeature Post(ImageFeature imageFeature)
        {
            return imageFeature;
        }
    }
}

Upvotes: 1

Rohith
Rohith

Reputation: 5677

There is nothing specific for Request Models .Possible option is to write an Operation filter

Here's the pseudo code

    public class RequestModelExtentionOperator: IOperationFilter    
        {                 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        if (operation.operationId == "Controller_ActionName")  // controller and action name
        {
           var refSchema = schemaRegistry.GetOrRegister(typeof(List<ImageFeature>));
                //here you can create a new Parameter of type Array
var param=new Parameter 
                    {
                        name = "Features",
                        @in = "formData",
                        required = true,
                        type = "array"
                    };
            param.PopulateFrom(schema);
operation.parameters = new[]{ param };
        }
    }            
            }
    }

Then we can set the OperationFilter

httpConfiguration
     .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
         {
             c.OperationFilter<RequestModelExtentionOperator>();
         });

Hope it helps

Upvotes: 0

Related Questions