Search code examples
c#.netsourcegenerators

Controller created with .Net source generator is not accessible or visible in Swagger docs


I created a .Net source generator project (.Net 2.0) that creates an Api Controller. The generated controller looks as follows:

using Company.Domain.Enums;
using Company.Domain.Common;
using Company.Api.Authorization;
using Company.Api.Controllers.Core;
using Company.Application.Core.Common.Models;
using Company.Application.Assets.Manufacturer;
using Company.Application.Assets.Manufacturer.Queries.GetManufacturerById;

using Microsoft.AspNetCore.Mvc;

namespace Company.Api.Controllers
{
    public partial class ManufacturerController : ApiController
    {
        [HttpGet("{id}")]
        [Produces(typeof(ManufacturerModel))]
        public async Task<ActionResult<ManufacturerModel>> GetManufacturerById(int id)
        {
            ManufacturerModel result = await Mediator.Send(new GetManufacturerByIdQuery { Id = id });

            return result != null ? Ok(result) : NotFound();
        }
    }
}

the ApiController base class takes care of the api decoration as follows:

    [Authorize]
    [ApiController]
    [Route("api/[Controller]/[action]")]
    public abstract class ApiController : ControllerBase
    {...

My main project is a Dotnet 7.0 Api.

The problem is that this controller does not show up in my swagger documentation (other manually-created endpoints do) and it also gives a 404 when trying to call it with Postman.

It is as if Dotnet does not recognize and register it as a controller.

I tried not using the ApiController base class and just creating it as if it is a standard controller that extends BaseController, although that dit nothing.

Other classes that I create with the source generator function correctly and can be referenced by manually-created Controllers.

I would like to see the Controller added to Swagger and be callable.


Solution

  • I figured it out. Turned out to be an unrelated error in the source generator that was not showing as an exception.