From 8301d4d1c7472796ac7324e6e15052d72ea1de41 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 14:18:31 +0000 Subject: [PATCH] fix(cmd/api): ignore non-positive cache ttl in spec Co-Authored-By: Virgil --- cmd/api/cmd_test.go | 16 ++++++++++++++++ cmd/api/spec_builder.go | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/api/cmd_test.go b/cmd/api/cmd_test.go index 2b442f2..783a14c 100644 --- a/cmd/api/cmd_test.go +++ b/cmd/api/cmd_test.go @@ -289,6 +289,22 @@ func TestAPISpecCmd_Good_CacheAndI18nFlagsPopulateSpec(t *testing.T) { } } +func TestNewSpecBuilder_Good_IgnoresNonPositiveCacheTTL(t *testing.T) { + builder, err := newSpecBuilder(specBuilderConfig{ + cacheTTL: "0s", + }) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if builder.CacheEnabled { + t.Fatal("expected non-positive cache TTL to keep cache disabled") + } + if builder.CacheTTL != "0s" { + t.Fatalf("expected cache TTL metadata to be preserved, got %q", builder.CacheTTL) + } +} + func TestAPISpecCmd_Good_GraphQLPlaygroundFlagPopulatesSpecPaths(t *testing.T) { root := &cli.Command{Use: "root"} AddAPICommands(root) diff --git a/cmd/api/spec_builder.go b/cmd/api/spec_builder.go index 988bd57..594bec8 100644 --- a/cmd/api/spec_builder.go +++ b/cmd/api/spec_builder.go @@ -4,6 +4,7 @@ package api import ( "strings" + "time" goapi "dappco.re/go/core/api" ) @@ -48,6 +49,7 @@ func newSpecBuilder(cfg specBuilderConfig) (*goapi.SpecBuilder, error) { ssePath := strings.TrimSpace(cfg.ssePath) wsPath := strings.TrimSpace(cfg.wsPath) cacheTTL := strings.TrimSpace(cfg.cacheTTL) + cacheTTLValid := parsePositiveDuration(cacheTTL) builder := &goapi.SpecBuilder{ Title: cfg.title, @@ -65,7 +67,7 @@ func newSpecBuilder(cfg specBuilderConfig) (*goapi.SpecBuilder, error) { WSPath: wsPath, PprofEnabled: cfg.pprofEnabled, ExpvarEnabled: cfg.expvarEnabled, - CacheEnabled: cfg.cacheEnabled || cacheTTL != "" || cfg.cacheMaxEntries > 0 || cfg.cacheMaxBytes > 0, + CacheEnabled: cfg.cacheEnabled || cacheTTLValid || cfg.cacheMaxEntries > 0 || cfg.cacheMaxBytes > 0, CacheTTL: cacheTTL, CacheMaxEntries: cfg.cacheMaxEntries, CacheMaxBytes: cfg.cacheMaxBytes, @@ -104,3 +106,17 @@ func newSpecBuilder(cfg specBuilderConfig) (*goapi.SpecBuilder, error) { func parseLocales(raw string) []string { return splitUniqueCSV(raw) } + +func parsePositiveDuration(raw string) bool { + raw = strings.TrimSpace(raw) + if raw == "" { + return false + } + + d, err := time.ParseDuration(raw) + if err != nil || d <= 0 { + return false + } + + return true +}