Skip to content

Commit 71ab224

Browse files
added filter
1 parent 2905b28 commit 71ab224

File tree

11 files changed

+130
-7
lines changed

11 files changed

+130
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
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 (
@@ -21,7 +22,7 @@ import (
2122
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
2223
)
2324

24-
// Filter: ExtensionFilter is a filter that updates the x-sunset and x-xgen-version extensions to a date string
25+
// ExtensionFilter is a filter that updates the x-sunset and x-xgen-version extensions to a date string
2526
// and deletes the x-sunset extension if the latest matched version is deprecated by hidden versions
2627
// for the target environment
2728
type ExtensionFilter struct {

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

Lines changed: 9 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 (
@@ -55,6 +56,14 @@ func DefaultFilters(oas *openapi3.T, metadata *Metadata) []Filter {
5556
&HiddenEnvsFilter{oas: oas, metadata: metadata},
5657
&TagsFilter{oas: oas},
5758
&OperationsFilter{oas: oas},
59+
&SchemasFilter{oas: oas},
60+
}
61+
}
62+
63+
func FiltersToRemoveUnusedElements(oas *openapi3.T) []Filter {
64+
return []Filter{
65+
&TagsFilter{oas: oas},
66+
&SchemasFilter{oas: oas},
5867
}
5968
}
6069

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

Lines changed: 2 additions & 1 deletion
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 (
@@ -25,7 +26,7 @@ const (
2526
hiddenEnvsExtKey = "envs"
2627
)
2728

28-
// Filter: HiddenEnvsFilter is a filter that removes paths, operations,
29+
// HiddenEnvsFilter is a filter that removes paths, operations,
2930
// request/response bodies and content types that are hidden for the target environment
3031
type HiddenEnvsFilter struct {
3132
oas *openapi3.T

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

Lines changed: 2 additions & 1 deletion
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 (
@@ -20,7 +21,7 @@ import (
2021
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
2122
)
2223

23-
// Filter: InfoFilter is a filter that modifies the Info object in the OpenAPI spec.
24+
// InfoFilter is a filter that modifies the Info object in the OpenAPI spec.
2425
type InfoFilter struct {
2526
oas *openapi3.T
2627
metadata *Metadata

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
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 (
1718
"github.com/getkin/kin-openapi/openapi3"
1819
)
1920

20-
// Filter: OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations
21+
// OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations
2122
// and moves the x-sunset extension to the operation level.
2223
type OperationsFilter struct {
2324
oas *openapi3.T

tools/cli/internal/openapi/filter/operations_test.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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
"encoding/json"
19+
"fmt"
20+
"github.com/getkin/kin-openapi/openapi3"
21+
"strings"
22+
)
23+
24+
// SchemasFilter removes tags that are not used in the operations.
25+
type SchemasFilter struct {
26+
oas *openapi3.T
27+
}
28+
29+
func (f *SchemasFilter) Apply() error {
30+
if f.oas.Paths == nil {
31+
return nil
32+
}
33+
34+
usedRefs := map[string]bool{}
35+
allRefs := findRefs(f.oas)
36+
37+
// Extract unique references used in the OpenAPI document
38+
for ref := range allRefs {
39+
refParts := strings.Split(ref, "/")
40+
if len(refParts) >= 4 && refParts[1] == "components" && refParts[2] == "schemas" {
41+
usedRefs[refParts[3]] = true
42+
}
43+
}
44+
45+
res2B, _ := json.Marshal(usedRefs)
46+
fmt.Println("ANDREA:")
47+
fmt.Println(string(res2B))
48+
49+
filterComponentSchemasInRefs(f.oas, usedRefs)
50+
return nil
51+
}
52+
53+
// findRefs returns all the ref included in an openapi spec
54+
func findRefs(oas *openapi3.T) map[string]bool {
55+
if oas == nil {
56+
return nil
57+
}
58+
59+
refs := map[string]bool{}
60+
for _, v := range oas.Paths.Map() {
61+
refs[v.Ref] = true
62+
for _, op := range v.Operations() {
63+
for _, param := range op.Parameters {
64+
refs[param.Ref] = true
65+
}
66+
67+
if op.RequestBody != nil {
68+
refs[op.RequestBody.Ref] = true
69+
}
70+
71+
for _, resp := range op.Responses.Map() {
72+
refs[resp.Ref] = true
73+
74+
for _, content := range resp.Value.Content {
75+
if content.Schema != nil {
76+
refs[content.Schema.Ref] = true
77+
}
78+
}
79+
}
80+
}
81+
82+
}
83+
84+
return refs
85+
}
86+
87+
func filterComponentSchemasInRefs(oas *openapi3.T, usedRefs map[string]bool) {
88+
schemasToDelete := make([]string, 0)
89+
for k, v := range oas.Components.Schemas {
90+
fmt.Printf("k: %s, v: %v", k, v)
91+
if ok := usedRefs[k]; !ok {
92+
schemasToDelete = append(schemasToDelete, k)
93+
}
94+
}
95+
96+
for _, schemaToDelete := range schemasToDelete {
97+
delete(oas.Components.Schemas, schemaToDelete)
98+
}
99+
}

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

Lines changed: 2 additions & 1 deletion
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 (
@@ -19,7 +20,7 @@ import (
1920
"github.com/getkin/kin-openapi/openapi3"
2021
)
2122

22-
// Filter: TagsFilter removes tags that are not used in the operations.
23+
// TagsFilter removes tags that are not used in the operations.
2324
type TagsFilter struct {
2425
oas *openapi3.T
2526
}

tools/cli/internal/openapi/filter/tags_test.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 (

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

Lines changed: 2 additions & 1 deletion
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 (
@@ -21,7 +22,7 @@ import (
2122
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
2223
)
2324

24-
// Filter: VersioningFilter is a filter that modifies the OpenAPI spec by removing operations and responses
25+
// VersioningFilter is a filter that modifies the OpenAPI spec by removing operations and responses
2526
// that are not supported by the target version.
2627
type VersioningFilter struct {
2728
oas *openapi3.T

0 commit comments

Comments
 (0)