diff --git a/apidef/oas/middleware.go b/apidef/oas/middleware.go index e42a8a97b46..ada0718aaa2 100644 --- a/apidef/oas/middleware.go +++ b/apidef/oas/middleware.go @@ -356,8 +356,6 @@ func (g *Global) extractTrafficLogsTo(api *apidef.APIDefinition) { } g.TrafficLogs.ExtractTo(api) - - g.TrafficLogs.RetentionPeriod.ExtractTo(api) } func (g *Global) extractRequestSizeLimitTo(api *apidef.APIDefinition) { @@ -1607,9 +1605,13 @@ func (t *TrafficLogs) ExtractTo(api *apidef.APIDefinition) { api.DoNotTrack = !t.Enabled api.TagHeaders = t.TagHeaders - if t.RetentionPeriod != nil { - t.RetentionPeriod.ExtractTo(api) + if t.RetentionPeriod == nil { + t.RetentionPeriod = &RetentionPeriod{} + defer func() { + t.RetentionPeriod = nil + }() } + t.RetentionPeriod.ExtractTo(api) } // RetentionPeriod holds the configuration for analytics retention. diff --git a/apidef/oas/middleware_test.go b/apidef/oas/middleware_test.go index 2d6d180cc41..e10ede0b79b 100644 --- a/apidef/oas/middleware_test.go +++ b/apidef/oas/middleware_test.go @@ -241,6 +241,28 @@ func TestTrafficLogs(t *testing.T) { resultTrafficLogs.Fill(convertedAPI) assert.Equal(t, trafficLogs, resultTrafficLogs) }) + + t.Run("retention header disabled", func(t *testing.T) { + trafficLogs := TrafficLogs{ + Enabled: true, + RetentionPeriod: nil, + } + + var convertedAPI apidef.APIDefinition + var resultTrafficLogs TrafficLogs + + convertedAPI.SetDisabledFlags() + trafficLogs.ExtractTo(&convertedAPI) + + assert.Equal(t, true, convertedAPI.DisableExpireAnalytics) + assert.Equal(t, int64(0), convertedAPI.ExpireAnalyticsAfter) + + resultTrafficLogs.Fill(convertedAPI) + assert.Equal(t, &RetentionPeriod{ + Enabled: false, + Value: 0, + }, resultTrafficLogs.RetentionPeriod) + }) } func TestPluginConfig(t *testing.T) {