Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #8341
Closed
Open
Issue created Jan 05, 2021 by Administrator@rootContributor5 of 5 checklist items completed5/5 checklist items

[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

Swagger.json

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

Assignee
Assign to
Time tracking