[object Object]

← back to Cli Printing Press

fix(generator): auth mapping for Discord BotToken scheme

2915ae3e929d8800507fadd2ad2d8e6cde1d377f · 2026-03-23 18:49:16 -0700 · Matt Van Horn

Detect bot token pattern from security scheme name and generate
"Bot {token}" auth format with DISCORD_BOT_TOKEN env var. Also fix
empty token producing non-empty auth header by checking env var values
before format string replacement.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files touched

Diff

commit 2915ae3e929d8800507fadd2ad2d8e6cde1d377f
Author: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Date:   Mon Mar 23 18:49:16 2026 -0700

    fix(generator): auth mapping for Discord BotToken scheme
    
    Detect bot token pattern from security scheme name and generate
    "Bot {token}" auth format with DISCORD_BOT_TOKEN env var. Also fix
    empty token producing non-empty auth header by checking env var values
    before format string replacement.
    
    Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---
 internal/generator/templates/config.go.tmpl |  5 +++++
 internal/openapi/parser.go                  | 12 +++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/internal/generator/templates/config.go.tmpl b/internal/generator/templates/config.go.tmpl
index 16536626..3f1550a4 100644
--- a/internal/generator/templates/config.go.tmpl
+++ b/internal/generator/templates/config.go.tmpl
@@ -65,6 +65,11 @@ func (c *Config) AuthHeader() string {
 		return c.AuthHeaderVal
 	}
 {{- if eq .Auth.Type "api_key"}}
+{{- range .Auth.EnvVars}}
+	if c.{{envVarField .}} == "" {
+		return ""
+	}
+{{- end}}
 	format := "{{.Auth.Format}}"
 {{- range .Auth.EnvVars}}
 	format = strings.ReplaceAll(format, "{ {{- envVarPlaceholder . -}} }", c.{{envVarField .}})
diff --git a/internal/openapi/parser.go b/internal/openapi/parser.go
index 6ddc4600..1feabffc 100644
--- a/internal/openapi/parser.go
+++ b/internal/openapi/parser.go
@@ -111,6 +111,10 @@ func mapAuth(doc *openapi3.T, name string) spec.AuthConfig {
 			auth.Header = "Authorization"
 		}
 		auth.In = strings.TrimSpace(scheme.In)
+		// Detect bot token pattern from scheme name (e.g. "BotToken")
+		if strings.Contains(strings.ToLower(schemeName), "bot") && strings.EqualFold(auth.Header, "Authorization") {
+			auth.Format = "Bot {bot_token}"
+		}
 	case "oauth2":
 		auth.Type = "bearer_token"
 		auth.Header = "Authorization"
@@ -119,7 +123,13 @@ func mapAuth(doc *openapi3.T, name string) spec.AuthConfig {
 	envPrefix := strings.ToUpper(strings.ReplaceAll(name, "-", "_"))
 	switch auth.Type {
 	case "api_key":
-		auth.EnvVars = []string{envPrefix + "_API_KEY"}
+		// Use scheme name for more specific env var (e.g. BotToken -> DISCORD_BOT_TOKEN)
+		schemeEnvSuffix := toSnakeCase(schemeName)
+		if schemeEnvSuffix != "" && schemeEnvSuffix != "api_key" {
+			auth.EnvVars = []string{envPrefix + "_" + strings.ToUpper(schemeEnvSuffix)}
+		} else {
+			auth.EnvVars = []string{envPrefix + "_API_KEY"}
+		}
 	case "bearer_token":
 		auth.EnvVars = []string{envPrefix + "_TOKEN"}
 	}

← 3d07636b feat(generator): sub-resource grouping for nested API paths  ·  back to Cli Printing Press  ·  fix(openapi): handle nullable types in OpenAPI 3.1 specs 33d06483 →