Skip to content

Unquoted 'OidcScopes' key when using OpenID Connect #836

Closed

Description

@kuzznya

Hi all,

I have a problem with codegen when OpenID Connect security scheme is defined in spec:

security:
  - oidc:
      - openid
components:
  securitySchemes:
    oidc:
      type: openIdConnect
      openIdConnectUrl: <url>

The codegen generates such wrappers for all endpoints:

func (siw *ServerInterfaceWrapper) CreateResourceX(c *gin.Context) {

	c.Set(OidcScopes, []string{"openid"})

	for _, middleware := range siw.HandlerMiddlewares {
		middleware(c)
	}

	siw.Handler.CreateResourceX(c)
}

context.Set accepts only strings as keys (and OidcScopes is an unknown thing for the compiler), so I suppose that it should be quoted.

Activity

  1. kuzznya commented on Nov 1, 2022

    @kuzznya
    Author

    I found the root cause for this issue. I used -generate gin parameter, so generator does not generate constants, as it is included in models generation: codegen.go:158. The question is whether it should be like that?

  2. jamietanna commented on Nov 13, 2022

    @jamietanna
    Member

    In this case, you'll also need to make sure that the types are generated i.e.

    generate:
      models: true
      gin-server: true
  3. mromaszewicz commented on Jul 14, 2026

    @mromaszewicz
    Member

    This is a comment from an automated bug scrub, after making a significant number of fixes to the code generation.

    We reproduced this against the current main. The bug is fixed, and in fact the behaviour has changed in two relevant ways.

    Spec used (spec.yaml):

    openapi: "3.0.3"
    info:
      title: Test OIDC
      version: "1.0"
    paths:
      /resources:
        post:
          operationId: CreateResourceX
          security:
            - oidc: [openid]
          responses:
            "200": { description: ok }
    security:
      - oidc: [openid]
    components:
      securitySchemes:
        oidc:
          type: openIdConnect
          openIdConnectUrl: https://example.com/.well-known/openid-configuration

    1. Default config (config.yaml):

    package: repro
    output: server.gen.go
    generate:
      gin-server: true
      models: true

    Emitting security-scope values into the request context is now off by default (it can't represent AND/OR/anonymous security requirements — validation should happen in request-validation middleware instead). So the offending c.Set(OidcScopes, …) line is no longer generated at all:

    func (siw *ServerInterfaceWrapper) CreateResourceX(c *gin.Context) {
    	for _, middleware := range siw.HandlerMiddlewares {
    		middleware(c)
    		if c.IsAborted() {
    			return
    		}
    	}
    	siw.Handler.CreateResourceX(c)
    }

    2. Legacy opt-in — if you still want the scopes-on-context behaviour, set (top-level key):

    compatibility:
      enable-auth-scopes-on-context: true

    The generated code now uses a typed context-key constant and an explicit string(...) conversion, which compiles correctly (the original bug):

    OidcScopes oidcContextKey = "oidc.Scopes"
    // ...
    c.Set(string(OidcScopes), []string{"openid"})

    Either way the unquoted-key compile error is gone, so we're closing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions