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

5
  • Open the /swagger/v1/swagger.json hopefully, there are more details there. Commented Jul 1, 2019 at 19:32
  • @HelderSepulveda, The file is not created thus the fetch error Commented Jul 2, 2019 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. Commented Jul 3, 2019 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<ApiDescription> apiDescriptions, ISchemaRegistry schemaRegistry) Commented Jul 5, 2019 at 10:28
  • In this SampleController.Get() is a overridden function from a abstract base class MyControllerBase Commented Jul 5, 2019 at 10:29

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.