Skip to content

Commit cf99d06

Browse files
CLOUDP-261180: Add filter to remove unused views (#350)
1 parent 831981c commit cf99d06

22 files changed

+677551
-678843
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func DefaultFilters(oas *openapi3.T, metadata *Metadata) []Filter {
7979
&TagsFilter{oas: oas},
8080
&OperationsFilter{oas: oas},
8181
&SunsetFilter{oas: oas},
82+
&SchemasFilter{oas: oas},
8283
}
8384
}
8485

@@ -88,6 +89,7 @@ func FiltersWithoutVersioning(oas *openapi3.T, metadata *Metadata) []Filter {
8889
&HiddenEnvsFilter{oas: oas, metadata: metadata},
8990
&TagsFilter{oas: oas},
9091
&OperationsFilter{oas: oas},
92+
&SchemasFilter{oas: oas},
9193
}
9294
}
9395

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

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

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

118118
func TestFiltersWithoutVersioning(t *testing.T) {
119119
doc := &openapi3.T{}
120120
metadata := &Metadata{}
121121
filters := FiltersWithoutVersioning(doc, metadata)
122122

123-
assert.Len(t, filters, 4)
123+
assert.Len(t, filters, 5)
124124
}
125125

126126
func TestFiltersToGetVersions(t *testing.T) {

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

+1
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 (

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

+1
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 (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
"log"
19+
"maps"
20+
"strings"
21+
22+
"github.com/getkin/kin-openapi/openapi3"
23+
)
24+
25+
// SchemasFilter removes unused #/components/schemas/.
26+
type SchemasFilter struct {
27+
oas *openapi3.T
28+
}
29+
30+
func (*SchemasFilter) ValidateMetadata() error {
31+
return nil
32+
}
33+
34+
func (f *SchemasFilter) Apply() error {
35+
if f.oas.Paths == nil {
36+
return nil
37+
}
38+
39+
for {
40+
oasSpecAsBytes, err := f.oas.MarshalJSON()
41+
if err != nil {
42+
return err
43+
}
44+
45+
spec := string(oasSpecAsBytes)
46+
schemasToDelete := make([]string, 0)
47+
for k := range f.oas.Components.Schemas {
48+
ref := "#/components/schemas/" + k
49+
if !strings.Contains(spec, ref) {
50+
schemasToDelete = append(schemasToDelete, k)
51+
}
52+
}
53+
54+
if len(schemasToDelete) == 0 {
55+
return nil
56+
}
57+
58+
for _, schemaToDelete := range schemasToDelete {
59+
log.Printf("Deleting unused schema: %q", schemaToDelete)
60+
maps.DeleteFunc(f.oas.Components.Schemas, func(k string, _ *openapi3.SchemaRef) bool {
61+
return k == schemaToDelete
62+
})
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)