-
Couldn't load subscription status.
- Fork 14
CLOUDP-302671: Add filter command that removes x-xgen extensions from OAS #495
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 all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
a73bf48
chore: decouple extension filters from versioning extension filters
blva 8dc841b
Update
blva 043c7be
update
blva 0385316
WIP
blva ddef873
chore: fix filters docs
blva 7119a8d
remove command
blva 309442d
Merge remote-tracking branch 'origin/main' into filter-docs
blva 87aed69
update
blva fd281e9
Add metadata validation per filter and unit tests
blva a1900f1
lint
blva 9cc82a7
lint
blva fb2bf6e
Fix tests
blva 8d4f89c
Revert "remove command"
blva 193357b
Move versioning related filtering
blva b953b7b
Update
blva 2dd3e79
Illustrative commit
blva 55891ca
Revert "Illustrative commit"
blva 1667b43
Lint
blva 60c38e3
Update
blva 23c123d
Update
blva f38bcc9
fix conflict
blva aad85b4
update test files
blva 613d130
fiX test
blva 4f0bf0d
Make tests run in parallelC
blva 635eb8e
Update filter
blva 748d74e
UpdatE
blva 20f21d6
lint
blva d6735fd
lint
blva 70b6264
update
blva 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright 2024 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 ( | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/getkin/kin-openapi/openapi3" | ||
| "github.com/mongodb/openapi/tools/cli/internal/apiversion" | ||
| "github.com/mongodb/openapi/tools/cli/internal/cli/flag" | ||
| "github.com/mongodb/openapi/tools/cli/internal/cli/usage" | ||
| "github.com/mongodb/openapi/tools/cli/internal/openapi" | ||
| "github.com/mongodb/openapi/tools/cli/internal/openapi/filter" | ||
| "github.com/spf13/afero" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type Opts struct { | ||
| fs afero.Fs | ||
| basePath string | ||
| outputPath string | ||
| env string | ||
| version string | ||
| format string | ||
| } | ||
|
|
||
| func (o *Opts) Run() error { | ||
| loader := openapi.NewOpenAPI3() | ||
| specInfo, err := loader.CreateOpenAPISpecFromPath(o.basePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var filteredOAS *openapi3.T | ||
| // If a version is provided, versioning filters will also be applied. | ||
| if o.version != "" { | ||
| filteredOAS, err = ByVersion(specInfo.Spec, o.version, o.env) | ||
| } else { | ||
| filters := filter.FiltersWithoutVersioning | ||
| metadata := filter.NewMetadata(nil, o.env) | ||
| filteredOAS, err = filter.ApplyFilters(specInfo.Spec, metadata, filters) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return openapi.Save(o.outputPath, filteredOAS, o.format, o.fs) | ||
| } | ||
|
|
||
| func ByVersion(oas *openapi3.T, version, env string) (result *openapi3.T, err error) { | ||
| log.Printf("Filtering OpenAPI document by version %q", version) | ||
| apiVersion, err := apiversion.New(apiversion.WithVersion(version)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return filter.ApplyFilters(oas, filter.NewMetadata(apiVersion, env), filter.DefaultFilters) | ||
| } | ||
|
|
||
| func (o *Opts) PreRunE(_ []string) error { | ||
| if o.basePath == "" { | ||
| return fmt.Errorf("no OAS detected. Please, use the flag %s to include the base OAS", flag.Base) | ||
| } | ||
|
|
||
| return openapi.ValidateFormatAndOutput(o.format, o.outputPath) | ||
| } | ||
|
|
||
| // Builder builds the filter command with the following signature: | ||
| // filter -s oas -o output-oas.json. | ||
| func Builder() *cobra.Command { | ||
| opts := &Opts{ | ||
| fs: afero.NewOsFs(), | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "filter -s spec ", | ||
| Short: `Filter Open API specification removing hidden endpoints and extension metadata. | ||
| If a version is provided, versioning filters will also be applied.`, | ||
| Args: cobra.NoArgs, | ||
| PreRunE: func(_ *cobra.Command, args []string) error { | ||
| return opts.PreRunE(args) | ||
| }, | ||
| RunE: func(_ *cobra.Command, _ []string) error { | ||
| return opts.Run() | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "-", usage.Spec) | ||
| cmd.Flags().StringVar(&opts.env, flag.Environment, "", usage.Environment) | ||
| cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output) | ||
| cmd.Flags().StringVar(&opts.version, flag.Version, "", usage.Version) | ||
| cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, openapi.ALL, usage.Format) | ||
|
|
||
| // Required flags | ||
| _ = cmd.MarkFlagRequired(flag.Output) | ||
| _ = cmd.MarkFlagRequired(flag.Spec) | ||
| _ = cmd.MarkFlagRequired(flag.Environment) | ||
| return cmd | ||
| } | ||
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,143 @@ | ||
| // Copyright 2024 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 ( | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/getkin/kin-openapi/openapi3" | ||
| "github.com/mongodb/openapi/tools/cli/internal/openapi" | ||
| "github.com/spf13/afero" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/tufin/oasdiff/load" | ||
| ) | ||
|
|
||
| func TestSuccessfulFilter_Run(t *testing.T) { | ||
| fs := afero.NewMemMapFs() | ||
| t.Parallel() | ||
|
|
||
| opts := &Opts{ | ||
| basePath: "../../../test/data/base_spec.json", | ||
| outputPath: "filtered-oas.yaml", | ||
| fs: fs, | ||
| env: "dev", | ||
| } | ||
|
|
||
| if err := opts.Run(); err != nil { | ||
| t.Fatalf("Run() unexpected error: %v", err) | ||
| } | ||
|
|
||
| newSpec, err := loadRunResultOas(fs, opts.outputPath) | ||
| require.NoError(t, err) | ||
|
|
||
| // check all paths are kept | ||
| for _, pathItem := range newSpec.Spec.Paths.Map() { | ||
| for _, operation := range pathItem.Operations() { | ||
| // check extensions are removed at the operation level | ||
| require.Nil(t, operation.Extensions) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestSuccessfulFilterWithVersion_Run(t *testing.T) { | ||
| fs := afero.NewMemMapFs() | ||
| t.Parallel() | ||
|
|
||
| opts := &Opts{ | ||
| basePath: "../../../test/data/base_spec.json", | ||
| outputPath: "filtered-oas.yaml", | ||
| fs: fs, | ||
| env: "dev", | ||
| version: "2023-01-01", | ||
| } | ||
|
|
||
| if err := opts.Run(); err != nil { | ||
| t.Fatalf("Run() unexpected error: %v", err) | ||
| } | ||
|
|
||
| s, err := loadRunResultOas(fs, opts.outputPath) | ||
| require.NoError(t, err) | ||
| // assert /api/atlas/v2/groups/{groupId}:migrate does not exist in filtered spec as it is from a newer version | ||
| paths := s.Spec.Paths.Map() | ||
| require.Contains(t, paths, "/api/atlas/v2/groups") | ||
| require.NotContains(t, paths, "/api/atlas/v2/groups/{groupId}:migrate") | ||
| } | ||
|
|
||
| func TestOpts_PreRunE(t *testing.T) { | ||
| testCases := []struct { | ||
| wantErr require.ErrorAssertionFunc | ||
| basePath string | ||
| name string | ||
| }{ | ||
| { | ||
| wantErr: require.Error, | ||
| name: "NoBasePath", | ||
| }, | ||
| { | ||
| wantErr: require.NoError, | ||
| basePath: "../../../test/data/base_spec.json", | ||
| name: "Successful", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range testCases { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| o := &Opts{ | ||
| basePath: tt.basePath, | ||
| format: "json", | ||
| } | ||
| tt.wantErr(t, o.PreRunE(nil)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestInvalidFormat_PreRun(t *testing.T) { | ||
| opts := &Opts{ | ||
| outputPath: "foas.json", | ||
| basePath: "base.json", | ||
| format: "html", | ||
| } | ||
|
|
||
| err := opts.PreRunE(nil) | ||
| require.Error(t, err) | ||
| require.EqualError(t, err, "format must be either 'json', 'yaml' or 'all', got 'html'") | ||
| } | ||
|
|
||
| func TestInvalidPath_PreRun(t *testing.T) { | ||
| opts := &Opts{ | ||
| outputPath: "foas.html", | ||
| basePath: "base.json", | ||
| format: "all", | ||
| } | ||
|
|
||
| err := opts.PreRunE(nil) | ||
| require.Error(t, err) | ||
| require.EqualError(t, err, "output file must be either a JSON or YAML file, got foas.html") | ||
| } | ||
|
|
||
| func loadRunResultOas(fs afero.Fs, fileName string) (*load.SpecInfo, error) { | ||
| oas := openapi.NewOpenAPI3() | ||
| oas.Loader.ReadFromURIFunc = func(_ *openapi3.Loader, _ *url.URL) ([]byte, error) { | ||
| f, err := fs.OpenFile(fileName, 0, 0) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
| return afero.ReadAll(f) | ||
| } | ||
|
|
||
| return oas.CreateOpenAPISpecFromPath(fileName) | ||
| } |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -159,6 +159,7 @@ func TestInvalidPath_PreRun(t *testing.T) { | |||||
| outputPath: "foas.html", | ||||||
| externalPaths: externalPaths, | ||||||
| basePath: "base.json", | ||||||
| format: "json", | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| err := opts.PreRunE(nil) | ||||||
|
|
||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we define a flag with the name of the filter to apply? I am wondering a flag would help to clarify what filters will be applied to the spec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, but since we don't have a use case I would rather only give the two options for now, to avoid generating several different specs with different filters applied