swag icon indicating copy to clipboard operation
swag copied to clipboard

missing scopes on oauth2 access code flow

Open oprimogus opened this issue 8 months ago • 1 comments

Describe the bug When defining an OAuth2AccessCode security scheme, the generated OpenAPI spec is incorrect for use with Swagger UI. It includes annotations that don't make sense in this context (e.g., in header), and the name of the security scheme in the spec is also wrong.

To Reproduce Define the OAuth2AccessCode security scheme like this in main.go. (swagger version: swag v2) Command used: go tool swag init -g cmd/main.go -o api --v3.1 --parseInternal --parseDependency

// @securitydefinitions.oauth2.accessCode OAuth2AccessCode
// @tokenUrl								https://mydomain.com/oauth/v2/token
// @authorizationurl						https://mydomain.com/oauth/v2/authorize
// @in										header
// @scope.openid							OpenID Connect basic login
// @scope.email							Access to user's email
// @scope.profile							Access to user's profile

is generated this spec:

securitySchemes:
    OAuth2AccessCode:
      flows:
        authorizationCode:
          authorizationUrl: https://mydomain.com/oauth/v2/authorize
          tokenUrl: https://mydomain.com/oauth/v2/token
      in: header
      type: oauth2

Expected behavior

  securitySchemes:
    OAuth2AccessCode:
      flows:
        authorizationCode:
          authorizationUrl: https://mydomain.com/oauth/authorize
          scopes:
            openid: "OpenID Connect basic login"
            email: "Access to user's email"
            profile: "Access to user's profile"
          tokenUrl: https://mydomain.com/oauth/token
      type: oauth2

Note:

  • The name of the security scheme should not include the annotation string.
  • The in field shouldn't be required for OAuth2; it doesn't apply to this security type.

Screenshots If applicable, add screenshots to help explain your problem.

Your swag version e.g. v2.0.0-rc4

Your go version e.g. 1.24.0

Desktop (please complete the following information):

  • OS: [e.g. Fedora]
  • Browser: [e.g. firefox]
  • Version: [e.g. 137.0.1]

Additional context Add any other context about the problem here.

oprimogus avatar Apr 21 '25 02:04 oprimogus

@sdghchj Can you give an hand with this?

oprimogus avatar Apr 24 '25 20:04 oprimogus