-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-261180: Add filter to remove unused views #350
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
71ab224
added filter
andreaangiolillo edb4d50
added filter
andreaangiolillo e708adb
merged master
andreaangiolillo f3c866f
fixes
andreaangiolillo 82c82b2
fixed linting
andreaangiolillo d8f04df
updated unit test
andreaangiolillo fca08bc
fixes
andreaangiolillo 7a0b03e
merged master
andreaangiolillo 83584f8
Update filter_test.go
andreaangiolillo f779d06
fixes
andreaangiolillo 337c750
Update versioning.go
andreaangiolillo a60097c
Update openapi-v2-2023-01-01.json
andreaangiolillo f7b6a09
Update schemas.go
andreaangiolillo 0e4a1fa
updated tests
andreaangiolillo 47aded8
Create schemas_test.go
andreaangiolillo 1a71913
added unit tests
andreaangiolillo 41f6ee7
Update schemas.go
andreaangiolillo 0207e55
Update schemas.go
andreaangiolillo e828d28
Update schemas.go
andreaangiolillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 ( | ||
"log" | ||
"maps" | ||
"strings" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
// SchemasFilter removes unused #/components/schemas/. | ||
type SchemasFilter struct { | ||
oas *openapi3.T | ||
} | ||
|
||
func (*SchemasFilter) ValidateMetadata() error { | ||
return nil | ||
} | ||
|
||
func (f *SchemasFilter) Apply() error { | ||
if f.oas.Paths == nil { | ||
return nil | ||
} | ||
|
||
for { | ||
oasSpecAsBytes, err := f.oas.MarshalJSON() | ||
if err != nil { | ||
return err | ||
} | ||
spec := string(oasSpecAsBytes) | ||
|
||
schemasToDelete := make([]string, 0) | ||
for k := range f.oas.Components.Schemas { | ||
ref := "#/components/schemas/" + k | ||
if !strings.Contains(spec, ref) { | ||
schemasToDelete = append(schemasToDelete, k) | ||
} | ||
} | ||
|
||
if len(schemasToDelete) == 0 { | ||
return nil | ||
} | ||
|
||
for _, schemaToDelete := range schemasToDelete { | ||
log.Printf("Deleting unused schema: '%s'", schemaToDelete) | ||
maps.DeleteFunc(f.oas.Components.Schemas, func(k string, _ *openapi3.SchemaRef) bool { | ||
return k == schemaToDelete | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.