Skip to content

Commit 27cf2b6

Browse files
CLOUDP-302981: Fix merge conflict
2 parents 2fb28e0 + 7ba7b40 commit 27cf2b6

File tree

19 files changed

+450
-24
lines changed

19 files changed

+450
-24
lines changed

tools/cli/internal/cli/split/split.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func Builder() *cobra.Command {
136136
}
137137

138138
cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "-", usage.Spec)
139-
cmd.Flags().StringVar(&opts.env, flag.Environment, "", usage.Environment)
139+
cmd.Flags().StringVar(&opts.env, flag.Environment, "prod", usage.Environment)
140140
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output)
141141
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, openapi.ALL, usage.Format)
142142
cmd.Flags().StringVar(&opts.gitSha, flag.GitSha, "", usage.GitSha)

tools/cli/internal/cli/split/split_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestSuccessfulSplit_Run(t *testing.T) {
3131
basePath: "../../../test/data/base_spec.json",
3232
outputPath: "foas.yaml",
3333
fs: fs,
34+
env: "dev",
3435
}
3536

3637
if err := opts.Run(); err != nil {
@@ -44,6 +45,7 @@ func TestSplitPublicPreviewRun(t *testing.T) {
4445
basePath: "../../../test/data/base_spec_with_public_preview.json",
4546
outputPath: "foas.yaml",
4647
fs: fs,
48+
env: "dev",
4749
}
4850

4951
if err := opts.Run(); err != nil {
@@ -64,6 +66,7 @@ func TestSplitPrivatePreviewRun(t *testing.T) {
6466
basePath: "../../../test/data/base_spec_with_private_preview.json",
6567
outputPath: "foas.yaml",
6668
fs: fs,
69+
env: "dev",
6770
}
6871

6972
if err := opts.Run(); err != nil {
@@ -83,6 +86,7 @@ func TestSplitMulitplePreviewsRun(t *testing.T) {
8386
opts := &Opts{
8487
basePath: "../../../test/data/base_spec_with_multiple_private_and_public_previews.json",
8588
outputPath: "foas.yaml",
89+
env: "dev",
8690
fs: fs,
8791
}
8892

@@ -122,6 +126,7 @@ func TestInjectSha_Run(t *testing.T) {
122126
outputPath: "foas.yaml",
123127
fs: fs,
124128
gitSha: "123456",
129+
env: "dev",
125130
}
126131

127132
if err := opts.Run(); err != nil {

tools/cli/internal/openapi/filter/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ The Atlas Admin API OpenAPI specifications are used not only to document REST en
88
- Filtering per version, so that only the endpoints that are available in that version are shown.
99
## What filters are available?
1010
### List of filters
11-
[ExtensionFilter is a filter that removes the x-xgen-IPA-exception extension from the OpenAPI spec.](../internal/openapi/filter/extension.go?plain=1#L21)
12-
[HiddenEnvsFilter is a filter that removes paths, operations,](../internal/openapi/filter/hidden_envs.go?plain=1#L28)
13-
[OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations](../internal/openapi/filter/operations.go?plain=1#L20)
14-
[VersioningExtensionFilter is a filter that updates the x-sunset and x-xgen-version extensions to a date string](../internal/openapi/filter/versioning_extension.go?plain=1#L25)
15-
[VersioningFilter is a filter that modifies the OpenAPI spec by removing operations and responses](../internal/openapi/filter/versioning.go?plain=1#L25)
11+
[ExtensionFilter: is a filter that removes the x-xgen-IPA-exception extension from the OpenAPI spec.](../internal/openapi/filter/extension.go?plain=1#L21)
12+
[HiddenEnvsFilter: is a filter that removes paths, operations,](../internal/openapi/filter/hidden_envs.go?plain=1#L28)
13+
[InfoVersioningFilter: Filter that modifies the Info object in the OpenAPI spec with the target version.](../internal/openapi/filter/info.go?plain=1#L23)
14+
[OperationsFilter: is a filter that removes the x-xgen-owner-team extension from operations](../internal/openapi/filter/operations.go?plain=1#L20)
15+
[TagsFilter: removes tags that are not used in the operations.](../internal/openapi/filter/tags.go?plain=1#L23)
16+
[VersioningExtensionFilter: is a filter that updates the x-sunset and x-xgen-version extensions to a date string](../internal/openapi/filter/versioning_extension.go?plain=1#L25)
17+
[VersioningFilter: is a filter that modifies the OpenAPI spec by removing operations and responses](../internal/openapi/filter/versioning.go?plain=1#L25)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/getkin/kin-openapi/openapi3"
1919
)
2020

21-
// Filter: ExtensionFilter is a filter that removes the x-xgen-IPA-exception extension from the OpenAPI spec.
21+
// ExtensionFilter: is a filter that removes the x-xgen-IPA-exception extension from the OpenAPI spec.
2222
type ExtensionFilter struct {
2323
oas *openapi3.T
2424
metadata *Metadata
@@ -29,6 +29,10 @@ const (
2929
format = "2006-01-02T15:04:05Z07:00"
3030
)
3131

32+
func (f *ExtensionFilter) ValidateMetadata() error {
33+
return validateMetadata(f.metadata)
34+
}
35+
3236
func (f *ExtensionFilter) Apply() error {
3337
for _, pathItem := range f.oas.Paths.Map() {
3438
if pathItem == nil {

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"encoding/json"
1818
"errors"
1919
"fmt"
20+
reflect "reflect"
2021

2122
"github.com/getkin/kin-openapi/openapi3"
2223
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
@@ -25,6 +26,7 @@ import (
2526
//go:generate mockgen -destination=../filter/mock_filter.go -package=filter github.com/mongodb/openapi/tools/cli/internal/openapi/filter Filter
2627
type Filter interface {
2728
Apply() error
29+
ValidateMetadata() error
2830
}
2931

3032
type Metadata struct {
@@ -39,11 +41,29 @@ func NewMetadata(targetVersion *apiversion.APIVersion, targetEnv string) *Metada
3941
}
4042
}
4143

44+
// validateMetadata validates the metadata object, ensuring its not nil and has a target env.
4245
func validateMetadata(metadata *Metadata) error {
4346
if metadata == nil {
4447
return errors.New("metadata is nil")
4548
}
4649

50+
if metadata.targetEnv == "" {
51+
return errors.New("target environment is empty")
52+
}
53+
54+
return nil
55+
}
56+
57+
// validateMetadataWithVersion validates the metadata object, ensuring its not nil and has a target version.
58+
func validateMetadataWithVersion(metadata *Metadata) error {
59+
if err := validateMetadata(metadata); err != nil {
60+
return err
61+
}
62+
63+
if metadata.targetVersion == nil {
64+
return errors.New("target version is nil")
65+
}
66+
4767
return nil
4868
}
4969

@@ -52,7 +72,17 @@ func DefaultFilters(oas *openapi3.T, metadata *Metadata) []Filter {
5272
&ExtensionFilter{oas: oas, metadata: metadata},
5373
&VersioningExtensionFilter{oas: oas, metadata: metadata},
5474
&VersioningFilter{oas: oas, metadata: metadata},
55-
&InfoFilter{oas: oas, metadata: metadata},
75+
&InfoVersioningFilter{oas: oas, metadata: metadata},
76+
&HiddenEnvsFilter{oas: oas, metadata: metadata},
77+
&TagsFilter{oas: oas},
78+
&OperationsFilter{oas: oas},
79+
}
80+
}
81+
82+
func FiltersWithoutVersioning(oas *openapi3.T, metadata *Metadata) []Filter {
83+
return []Filter{
84+
&ExtensionFilter{oas: oas, metadata: metadata},
85+
&InfoVersioningFilter{oas: oas, metadata: metadata},
5686
&HiddenEnvsFilter{oas: oas, metadata: metadata},
5787
&TagsFilter{oas: oas},
5888
&OperationsFilter{oas: oas},
@@ -71,17 +101,17 @@ func ApplyFilters(doc *openapi3.T, metadata *Metadata, filters func(oas *openapi
71101
return nil, errors.New("openapi document is nil")
72102
}
73103

74-
if err := validateMetadata(metadata); err != nil {
75-
return nil, err
76-
}
77-
78104
// make a copy of the oas to avoid modifying the original document when applying filters
79105
oas, err := duplicateOas(doc)
80106
if err != nil {
81107
return nil, err
82108
}
83109

84110
for _, filter := range filters(oas, metadata) {
111+
if err := filter.ValidateMetadata(); err != nil {
112+
s := reflect.TypeOf(filter)
113+
return nil, fmt.Errorf("failed to validate metadata for filter %s with: %w", s, err)
114+
}
85115
if err := filter.Apply(); err != nil {
86116
return nil, err
87117
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2024 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+
package filter
15+
16+
import (
17+
"testing"
18+
19+
"github.com/getkin/kin-openapi/openapi3"
20+
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestNewMetadata(t *testing.T) {
26+
version := &apiversion.APIVersion{}
27+
env := "test-env"
28+
metadata := NewMetadata(version, env)
29+
30+
assert.Equal(t, version, metadata.targetVersion)
31+
assert.Equal(t, env, metadata.targetEnv)
32+
}
33+
34+
func TestValidateMetadata(t *testing.T) {
35+
t.Run("Valid metadata", func(t *testing.T) {
36+
metadata := &Metadata{
37+
targetEnv: "dev",
38+
}
39+
err := validateMetadata(metadata)
40+
assert.NoError(t, err)
41+
})
42+
43+
t.Run("Nil metadata", func(t *testing.T) {
44+
err := validateMetadata(nil)
45+
require.ErrorContains(t, err, "metadata is nil")
46+
})
47+
}
48+
49+
func TestDuplicateOas(t *testing.T) {
50+
doc := &openapi3.T{
51+
Info: &openapi3.Info{
52+
Title: "Test API",
53+
Version: "1.0.0",
54+
},
55+
}
56+
57+
duplicateDoc, err := duplicateOas(doc)
58+
require.NoError(t, err)
59+
require.NotNil(t, duplicateDoc)
60+
assert.Equal(t, doc.Info.Title, duplicateDoc.Info.Title)
61+
assert.Equal(t, doc.Info.Version, duplicateDoc.Info.Version)
62+
}
63+
64+
func TestApplyFilters(t *testing.T) {
65+
doc := &openapi3.T{
66+
Info: &openapi3.Info{
67+
Title: "Test API",
68+
Version: "1.0.0",
69+
},
70+
}
71+
metadata := &Metadata{}
72+
73+
t.Run("Nil document", func(t *testing.T) {
74+
_, err := ApplyFilters(nil, metadata, DefaultFilters)
75+
require.ErrorContains(t, err, "openapi document is nil")
76+
})
77+
78+
t.Run("Nil metadata", func(t *testing.T) {
79+
_, err := ApplyFilters(doc, nil, DefaultFilters)
80+
require.ErrorContains(t, err, "metadata is nil")
81+
})
82+
83+
t.Run("Invalid metadata", func(t *testing.T) {
84+
_, err := ApplyFilters(doc, metadata, DefaultFilters)
85+
require.ErrorContains(t, err, "target environment is empty")
86+
})
87+
88+
t.Run("Missing versioning metadata", func(t *testing.T) {
89+
metadata := &Metadata{
90+
targetEnv: "dev",
91+
}
92+
_, err := ApplyFilters(doc, metadata, DefaultFilters)
93+
require.ErrorContains(t, err, "failed to validate metadata for filter *filter.VersioningExtensionFilter with: target version is nil")
94+
})
95+
96+
t.Run("Valid metadata for default filters", func(t *testing.T) {
97+
version, err := apiversion.New(apiversion.WithVersion("2023-11-15"))
98+
require.NoError(t, err)
99+
metadata := &Metadata{
100+
targetEnv: "dev",
101+
targetVersion: version,
102+
}
103+
104+
filteredDoc, err := ApplyFilters(doc, metadata, DefaultFilters)
105+
require.NoError(t, err)
106+
assert.NotNil(t, filteredDoc)
107+
})
108+
}
109+
110+
func TestDefaultFilters(t *testing.T) {
111+
doc := &openapi3.T{}
112+
metadata := &Metadata{}
113+
filters := DefaultFilters(doc, metadata)
114+
115+
assert.Len(t, filters, 7)
116+
}
117+
118+
func TestFiltersWithoutVersioning(t *testing.T) {
119+
doc := &openapi3.T{}
120+
metadata := &Metadata{}
121+
filters := FiltersWithoutVersioning(doc, metadata)
122+
123+
assert.Len(t, filters, 5)
124+
}
125+
126+
func TestFiltersToGetVersions(t *testing.T) {
127+
doc := &openapi3.T{}
128+
metadata := &Metadata{}
129+
filters := FiltersToGetVersions(doc, metadata)
130+
131+
assert.Len(t, filters, 1)
132+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ const (
2525
hiddenEnvsExtKey = "envs"
2626
)
2727

28-
// Filter: HiddenEnvsFilter is a filter that removes paths, operations,
28+
// HiddenEnvsFilter: is a filter that removes paths, operations,
2929
// request/response bodies and content types that are hidden for the target environment.
3030
type HiddenEnvsFilter struct {
3131
oas *openapi3.T
3232
metadata *Metadata
3333
}
3434

35+
func (f *HiddenEnvsFilter) ValidateMetadata() error {
36+
return validateMetadata(f.metadata)
37+
}
38+
3539
func (f *HiddenEnvsFilter) Apply() error {
3640
// delete hidden paths first before processing
3741
for pathName, pathItem := range f.oas.Paths.Map() {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ import (
2020
"github.com/mongodb/openapi/tools/cli/internal/apiversion"
2121
)
2222

23-
// InfoFilter is a filter that modifies the Info object in the OpenAPI spec.
24-
type InfoFilter struct {
23+
// InfoVersioningFilter: Filter that modifies the Info object in the OpenAPI spec with the target version.
24+
type InfoVersioningFilter struct {
2525
oas *openapi3.T
2626
metadata *Metadata
2727
}
2828

29-
func (f *InfoFilter) Apply() error {
29+
func (f *InfoVersioningFilter) ValidateMetadata() error {
30+
return validateMetadataWithVersion(f.metadata)
31+
}
32+
33+
func (f *InfoVersioningFilter) Apply() error {
3034
if f.oas.Info == nil {
3135
return nil
3236
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestInfoFilter(t *testing.T) {
3939
},
4040
}
4141

42-
filter := &InfoFilter{
42+
filter := &InfoVersioningFilter{
4343
metadata: NewMetadata(targetVersion, "test"),
4444
oas: oas,
4545
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ import (
1717
"github.com/getkin/kin-openapi/openapi3"
1818
)
1919

20-
// Filter: OperationsFilter is a filter that removes the x-xgen-owner-team extension from operations
20+
// OperationsFilter: is a filter that removes the x-xgen-owner-team extension from operations
2121
// and moves the x-sunset extension to the operation level.
2222
type OperationsFilter struct {
2323
oas *openapi3.T
2424
}
2525

26+
func (*OperationsFilter) ValidateMetadata() error {
27+
return nil
28+
}
29+
2630
func (f *OperationsFilter) Apply() error {
2731
if f.oas.Paths == nil {
2832
return nil

0 commit comments

Comments
 (0)