Skip to content

undefined security scheme context key type when securitySchemes component is missing #2367

@kitosforos

Description

@kitosforos

After upgrading to v2.7.0, generated code fails to compile when the spec uses security at the top level or operation level but does not define the corresponding entry in components/securitySchemes.

Minimal spec to reproduce:

openapi: "3.0.0"
info:
  title: Test
  version: 1.0.0
security:
  - api_key: []
paths:
  /hello:
    get:
      operationId: getHello
      responses:
        "200":
          description: ok
oapi-codegen -generate types,gin,strict-server -o gen.go -package api spec.yaml

Generated output:

const (
    Api_keyScopes apiKeyContextKey = "api_key.Scopes"
)
gen.go:20:16: undefined: apiKeyContextKey

The constant references apiKeyContextKey but the type definition type apiKeyContextKey string is never emitted.

Adding components/securitySchemes fixes it -- the type is generated correctly. But the tool should not produce uncompilable code for a spec that was previously accepted, wdyt?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions