[BUG][ASPNETCORE] The aspnetcore generator doesn't generate essential authorization import
Created by: leonluc-dev
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output?
Description
The current version of the aspnetcore generator has an issue with authorized calls.
In ASP.NET authorized calls are marked with the Authorize attribute. The generator generates this properly:
[HttpGet]
[Route("/api/testauthorize")]
[Authorize]
[ValidateModelState]
public abstract IActionResult TestCall();
However, the required using Microsoft.AspNetCore.Authorization;
directive is missing at the top of the generated file if Swashbuckle generation is disabled. This causes compilation to fail due to the Authorize attribute's dependency on this directive.
openapi-generator version
5.0.0
OpenAPI declaration file content or url
Generation Details
Example generation CLI command:
java -jar openapi-generator-cli.jar generate -i ./swagger.json -g aspnetcore -o ./output --additional-properties=generateBody=false,classModifier=abstract,aspnetCoreVersion=3.1,buildTarget=library,isLibrary=true,packageName=TestGeneratedStubs,useSwashbuckle=false
Steps to reproduce
Generate a simple ASP.NET Core solution using the above swagger.json and cli command (the useSwashbuckle=false
parameter is the important part).
Try to build the resulting project using the dotnet build output/TestGeneratedStubs.sln
command
Suggest a fix
It seems the mustache template for the generated ASP.NET Core controllers has the using Microsoft.AspNetCore.Authorization;
directive wrapped in a useSwashbuckle
condition, even though the directive is often necessary even without swashbuckle enabled.
https://github.com/OpenAPITools/openapi-generator/blob/04dfff83e0717e693afb61499c62440491c1ac9c/modules/openapi-generator/src/main/resources/aspnetcore/3.0/controller.mustache#L9-L13