Skip to content

Commit c657a6a

Browse files
CLOUDP-297946: removes the x-sunset=9999-12-31 from the openapi spec (#596)
1 parent 4697d32 commit c657a6a

File tree

5 files changed

+407
-1
lines changed

5 files changed

+407
-1
lines changed

tools/cli/internal/openapi/filter/filter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package filter
1516

1617
import (
@@ -77,6 +78,7 @@ func DefaultFilters(oas *openapi3.T, metadata *Metadata) []Filter {
7778
&HiddenEnvsFilter{oas: oas, metadata: metadata},
7879
&TagsFilter{oas: oas},
7980
&OperationsFilter{oas: oas},
81+
&SunsetFilter{oas: oas},
8082
}
8183
}
8284

tools/cli/internal/openapi/filter/filter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestDefaultFilters(t *testing.T) {
112112
metadata := &Metadata{}
113113
filters := DefaultFilters(doc, metadata)
114114

115-
assert.Len(t, filters, 7)
115+
assert.Len(t, filters, 8)
116116
}
117117

118118
func TestFiltersWithoutVersioning(t *testing.T) {

tools/cli/internal/openapi/filter/operations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package filter
1516

1617
import (
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2025 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package filter
16+
17+
import (
18+
"strings"
19+
20+
"github.com/getkin/kin-openapi/openapi3"
21+
)
22+
23+
const sunsetToBeDecided = "9999-12-31"
24+
25+
// SunsetFilter removes the sunsetToBeDecided from the openapi specification.
26+
type SunsetFilter struct {
27+
oas *openapi3.T
28+
}
29+
30+
func (*SunsetFilter) ValidateMetadata() error {
31+
return nil
32+
}
33+
34+
func (f *SunsetFilter) Apply() error {
35+
if f.oas.Paths == nil {
36+
return nil
37+
}
38+
39+
for _, pathItem := range f.oas.Paths.Map() {
40+
for _, operation := range pathItem.Operations() {
41+
if operation.Responses == nil {
42+
continue
43+
}
44+
45+
applyOnOperation(operation)
46+
}
47+
}
48+
return nil
49+
}
50+
51+
func applyOnOperation(op *openapi3.Operation) {
52+
for key, response := range op.Responses.Map() {
53+
if !strings.HasPrefix(key, "20") {
54+
continue
55+
}
56+
57+
for _, content := range response.Value.Content {
58+
if v, ok := content.Extensions["x-sunset"]; ok {
59+
if v != sunsetToBeDecided {
60+
continue
61+
}
62+
delete(content.Extensions, "x-sunset")
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)