fix(cmd/api): ignore non-positive cache ttl in spec
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
579b27d84e
commit
8301d4d1c7
2 changed files with 33 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue