Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add filter to remove unused views #350

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand All @@ -21,7 +22,7 @@ import (
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
)

// Filter: ExtensionFilter is a filter that updates the x-sunset and x-xgen-version extensions to a date string
// ExtensionFilter is a filter that updates the x-sunset and x-xgen-version extensions to a date string
// and deletes the x-sunset extension if the latest matched version is deprecated by hidden versions
// for the target environment
type ExtensionFilter struct {
Expand Down
9 changes: 9 additions & 0 deletions tools/cli/internal/openapi/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand Down Expand Up @@ -55,6 +56,14 @@ func DefaultFilters(oas *openapi3.T, metadata *Metadata) []Filter {
&HiddenEnvsFilter{oas: oas, metadata: metadata},
&TagsFilter{oas: oas},
&OperationsFilter{oas: oas},
&SchemasFilter{oas: oas},
}
}

func FiltersToRemoveUnusedElements(oas *openapi3.T) []Filter {
return []Filter{
&TagsFilter{oas: oas},
&SchemasFilter{oas: oas},
}
}

Expand Down
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/hidden_envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand All @@ -25,7 +26,7 @@ const (
hiddenEnvsExtKey = "envs"
)

// Filter: HiddenEnvsFilter is a filter that removes paths, operations,
// HiddenEnvsFilter is a filter that removes paths, operations,
// request/response bodies and content types that are hidden for the target environment
type HiddenEnvsFilter struct {
oas *openapi3.T
Expand Down
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand All @@ -20,7 +21,7 @@ import (
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
)

// Filter: InfoFilter is a filter that modifies the Info object in the OpenAPI spec.
// InfoFilter is a filter that modifies the Info object in the OpenAPI spec.
type InfoFilter struct {
oas *openapi3.T
metadata *Metadata
Expand Down
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
"github.com/getkin/kin-openapi/openapi3"
)

// Filter: OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations
// OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations
// and moves the x-sunset extension to the operation level.
type OperationsFilter struct {
oas *openapi3.T
Expand Down
1 change: 1 addition & 0 deletions tools/cli/internal/openapi/filter/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand Down
151 changes: 151 additions & 0 deletions tools/cli/internal/openapi/filter/schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
"encoding/json"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"strings"
)

// SchemasFilter removes tags that are not used in the operations.
type SchemasFilter struct {
oas *openapi3.T
}

func (f *SchemasFilter) Apply() error {
if f.oas.Paths == nil {
return nil
}

usedRefs := map[string]bool{}
allRefs := findRefs(f.oas)

// Extract unique references used in the OpenAPI document
for ref := range allRefs {
refParts := strings.Split(ref, "/")
if len(refParts) >= 4 && refParts[1] == "components" && refParts[2] == "schemas" {
usedRefs[refParts[3]] = true
}
}

res2B, _ := json.Marshal(usedRefs)
fmt.Println("ANDREA:")
fmt.Println(string(res2B))

filterComponentSchemasInRefs(f.oas, usedRefs)
return nil
}

// findRefs returns all the ref included in an openapi spec
func findRefs(oas *openapi3.T) map[string]bool {
if oas == nil {
return nil
}

refs := map[string]bool{}
for _, v := range oas.Paths.Map() {
refs[v.Ref] = true
for _, op := range v.Operations() {
for _, param := range op.Parameters {
refs[param.Ref] = true
findRefsSchemaRef(refs, param.Value.Schema)
}

if op.RequestBody != nil {
refs[op.RequestBody.Ref] = true
if op.RequestBody.Value != nil {
for _, content := range op.RequestBody.Value.Content {
if content.Schema != nil {
findRefsSchemaRef(refs, content.Schema)
}
}
}
}

for _, resp := range op.Responses.Map() {
refs[resp.Ref] = true
for _, content := range resp.Value.Content {
if content.Schema != nil {
findRefsSchemaRef(refs, content.Schema)
}
}
}
}

}

Check failure on line 89 in tools/cli/internal/openapi/filter/schemas.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

return refs
}

func findRefsSchemasRefs(refs map[string]bool, schemas openapi3.SchemaRefs) {
if schemas == nil {
return
}

for _, schema := range schemas {
if ok := refs[schema.Ref]; ok {
continue
}

refs[schema.Ref] = true
if schema.Value != nil {
findRefsSchemasRefs(refs, schema.Value.AllOf)
findRefsSchemasRefs(refs, schema.Value.OneOf)
findRefsSchemasRefs(refs, schema.Value.AnyOf)
findRefsSchemas(refs, schema.Value.Properties)
}
}
}

func findRefsSchemas(refs map[string]bool, schemas openapi3.Schemas) {
if schemas == nil {
return
}

for _, schema := range schemas {
findRefsSchemaRef(refs, schema)
}
}

func findRefsSchemaRef(refs map[string]bool, schema *openapi3.SchemaRef) {
if schema == nil {
return
}

refs[schema.Ref] = true
if schema.Value == nil {
return
}

findRefsSchemasRefs(refs, schema.Value.AllOf)
findRefsSchemasRefs(refs, schema.Value.OneOf)
findRefsSchemasRefs(refs, schema.Value.AnyOf)
}

func filterComponentSchemasInRefs(oas *openapi3.T, usedRefs map[string]bool) {
schemasToDelete := make([]string, 0)
for k, v := range oas.Components.Schemas {
fmt.Printf("k: %s, v: %v", k, v)
if ok := usedRefs[k]; !ok {
schemasToDelete = append(schemasToDelete, k)
}
}

for _, schemaToDelete := range schemasToDelete {
delete(oas.Components.Schemas, schemaToDelete)
}
}
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand All @@ -19,7 +20,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

// Filter: TagsFilter removes tags that are not used in the operations.
// TagsFilter removes tags that are not used in the operations.
type TagsFilter struct {
oas *openapi3.T
}
Expand Down
1 change: 1 addition & 0 deletions tools/cli/internal/openapi/filter/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand Down
3 changes: 2 additions & 1 deletion tools/cli/internal/openapi/filter/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filter

import (
Expand All @@ -21,7 +22,7 @@ import (
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
)

// Filter: VersioningFilter is a filter that modifies the OpenAPI spec by removing operations and responses
// VersioningFilter is a filter that modifies the OpenAPI spec by removing operations and responses
// that are not supported by the target version.
type VersioningFilter struct {
oas *openapi3.T
Expand Down
9 changes: 8 additions & 1 deletion tools/cli/internal/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package openapi

//go:generate mockgen -destination=../openapi/mock_openapi.go -package=openapi github.com/mongodb/openapi/tools/cli/internal/openapi Parser,Merger
import (
"github.com/mongodb/openapi/tools/cli/internal/openapi/filter"
"log"

"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)

// This struct is a 1-to-1 copy of the Spec struct in the openapi3 package.
// Spec is a struct is a 1-to-1 copy of the Spec struct in the openapi3 package.
// We need this to override the order of the fields in the struct.
type Spec struct {
OpenAPI string `json:"openapi" yaml:"openapi"`
Expand Down Expand Up @@ -63,6 +64,12 @@ func (o *OasDiff) MergeOpenAPISpecs(paths []string) (*Spec, error) {
}
}

for _, f := range filter.FiltersToRemoveUnusedElements(o.base.Spec) {
if err := f.Apply(); err != nil {
return nil, err
}
}

return newSpec(o.base.Spec), nil
}

Expand Down
2 changes: 2 additions & 0 deletions tools/cli/internal/openapi/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (o *OpenAPI3) WithExcludedPrivatePaths() *OpenAPI3 {

func (o *OpenAPI3) CreateOpenAPISpecFromPath(path string) (*load.SpecInfo, error) {
o.Loader.IsExternalRefsAllowed = o.IsExternalRefsAllowed

spec, err := load.NewSpecInfo(o.Loader, load.NewSource(path))
spec.Url = path
if err != nil {
Expand All @@ -56,6 +57,7 @@ func (o *OpenAPI3) CreateOpenAPISpecFromPath(path string) (*load.SpecInfo, error
if o.ExcludePrivatePaths {
removePrivatePaths(spec.Spec)
}

return spec, nil
}

Expand Down
Loading