简体   繁体   中英

Swashbuckle.AspNetCore - Unable to generate json for controllers that inherit base controllers

I have two controllers that inherit other controllers - for one of them, the swagger doc is generated, for the other it is not. My controllers are:

[Route("Actor")]
public class ActorController : BaseDefControllerLog<Actor> 
 {
        public ActorController(BaseDefController<Actor> baseController, ILogger<ActorController> logger, IAppConfig appSettings) : base(baseController, logger, appSettings)
 { }
  }


[Route("ActorCatalog")]
public class ActorCatalogController: LoggerController, ICatalogController<Actor> 
{
        private readonly BaseDefControllerLog<Actor> _baseController;

        public ActorCatalogController(BaseDefControllerLog<Actor> baseController, ILogger<ActorCatalogController> logger) : base(logger)
        {
            _baseController = baseController;
        }

        [Route("GetAll")]
        [HttpGet]
        public ICollection<Actor> GetAll(int? pageNumber = null, int? pageSize = null)
        {
            //GetAll logic
        }
}

Below, BaseDefController, BaseDefControllerLog and LoggerController:

public class BaseDefController<T> : Controller, ICatalogController<T> where T : class, IConfigDef
    {
        private readonly IReadManager<T> _manager;

        public BaseDefController(IReadManager<T> manager)
        {
            _manager = manager;
        }
        [HttpGet]
        public virtual ICollection<T> GetAll(int? pageNumber = null, int? pageSize = null)
        {
            return _manager.GetAll(pageNumber, pageSize);
        }
    }


public class BaseDefControllerLog<T>: LoggerController where T : class, IConfigDef
    {
        private readonly BaseDefController<T> _baseController;
        private readonly ILogger<BaseDefControllerLog<T>> _logger;
        public BaseDefControllerLog(BaseDefController<T> baseController, ILogger<BaseDefControllerLog<T>> logger, IAppConfig appSettings) : base(logger)
        {
            _baseController = baseController;
            _logger = logger;
        }

       [HttpGet]
        public virtual ICollection<T> GetAll(string remoteIpAddress = null, int? pageNumber = null, int? pageSize = null)
        {
            //GetAll logic
        }
}



public class LoggerController : Controller
{
        protected ILogger Logger;

        public LoggerController(ILogger logger)
        {
            Logger = logger;
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        { //...  }
        public override void OnActionExecuted(ActionExecutedContext context)
        { //... }
 }

The "Actor" model is a simple class:

public class Actor : IConfigDef
    {
        public int Id { get; set; }
        public Guid Tag { get; set; }
        public string Name { get; set; }
        public DateTime LastUpdated { get; set; }
        public bool Deleted { get; set; }
    }

VERSION:

Swashbuckle.AspNetCore 1.0.0

STEPS TO REPRODUCE:

I have decorated both ActorController and ActorCatalogController with the Route attribute. At this point, swagger throws an exception when generating the json. If I remove the route on ActorController and therefore generate documentation only for ActorCatalogController, everything works fine. If I decorate the inherited controllers' actions with route attributes, I also get a 500 response on json. I also tried to remove the virtual keyword, with no luck.

EXPECTED RESULT:

Documentation for both controllers (ActorCatalogController and ActorController).

ACTUAL RESULT:

500 : http://localhost:4100/swagger/v1/swagger.json The JSON cannot be generated. I catch this exception: "Exception thrown: 'System.NotSupportedException' in Swashbuckle.AspNetCore.SwaggerGen.dll".

ADDITIONAL DETAILS

The target framework for my project is .NET framework 4.6.2. I use Microsoft.AspNetCore 1.1.2 and Microsoft.AspNetCore.Hosting.WindowsServices 1.1.2. My swagger configuration in Startup.cs is the following:

public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddMvc(config =>
                {
                    config.RespectBrowserAcceptHeader = true;
                })
                .AddJsonOptions(options =>
                {
                    options.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
                })
                .AddXmlSerializerFormatters();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "InternalServer", Version = "v1" });
            });

            InitializeContainer(services);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseMvc();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("v1/swagger.json", "SwaggerDemo V1");
            });

            LogRegistration(loggerFactory);
        }

我已解决问题,有关解决方法的更多详细信息,请参见: https : //github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/511

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM