1

I have a controller say 'SampleController' that uses 'MyControllerBase'(from a NuGet package) as a base class.

I am trying to add API documentation using swagger but the swagger is unable to fetch 'SampleController' thus cannot create swagger.json.

I am getting the error "fetch error: /swagger/v1/swagger.json"

'MyControllerBase' inherits 'ControllerBase' so 'SampleController' is working as an API should work.

How can I tell swagger to use 'MyControllerBase' to find Controllers

From Nuget Package:

namespace My.ApiFormatter.Controller
{
  public abstract class MyControllerBase<T> : ControllerBase, IFilterableController
  {
    public virtual Dictionary<string, IFilterMapper> FilterWhitelist => new Dictionary<string, IFilterMapper>();
  }
}

Implementation:

  [Route("api/[controller]")]
  [ApiController]
  public class SampleController : MyControllerBase<ExampleData>
  {
    [HttpGet]
    public override ActionResult<CollectionEnvelope<ExampleData>> GetAll()
    {
      var results = ExampleDatas
        .Where(x => x.Key > (Info.PageSize * Info.SkipPages)
            && x.Key <= (Info.PageSize * (Info.SkipPages + 1)))
        .Select(y => y.Value).ToList();

      var collectionEnvelope = GetCollectionEnvelope(results, ExampleDatas.Count);
      return Ok(collectionEnvelope);
    }
  }

Error In Swagger

Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56
  • Open the `/swagger/v1/swagger.json` hopefully, there are more details there. – Helder Sepulveda Jul 01 '19 at 19:32
  • @HelderSepulveda, The file is not created thus the fetch error – pawan kumar Jul 02 '19 at 13:11
  • Sorry but that is not file, that route is generated at runtime by swashbuckle, run the application and open that link see what you get. – Helder Sepulveda Jul 03 '19 at 21:32
  • @HelderSepulveda It says, `NotSupportedException: Ambiguous HTTP method for action - ApiFormatter.SampleApi.Controllers.SampleController.Get (ApiFormatter.SampleApi). Actions require an explicit HttpMethod binding for Swagger 2.0 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable apiDescriptions, ISchemaRegistry schemaRegistry) ` – pawan kumar Jul 05 '19 at 10:28
  • In this SampleController.Get() is a overridden function from a abstract base class MyControllerBase – pawan kumar Jul 05 '19 at 10:29

1 Answers1

4

Swagger will just get the methods as controllers in the base class. You are probably getting the error, because swagger thinks any public method is an endpoint. Make the methods and properties that aren't endpoints protected

Steve
  • 41
  • 2