Skip to content

Commit 60c38e3

Browse files
committed
Update
1 parent 1667b43 commit 60c38e3

File tree

8 files changed

+38
-55
lines changed

8 files changed

+38
-55
lines changed

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ package filter
1616

1717
import (
1818
"fmt"
19-
"log"
20-
"strings"
2119

22-
"github.com/getkin/kin-openapi/openapi3"
2320
"github.com/mongodb/openapi/tools/cli/internal/cli/flag"
2421
"github.com/mongodb/openapi/tools/cli/internal/cli/usage"
2522
"github.com/mongodb/openapi/tools/cli/internal/openapi"
@@ -44,37 +41,20 @@ func (o *Opts) Run() error {
4441
return err
4542
}
4643

47-
filteredOAS, err := o.filter(specInfo.Spec)
44+
filteredOAS, err := filter.ApplyFilters(specInfo.Spec, filter.NewMetadata(nil, o.env), filter.FiltersWithoutVersioning)
4845
if err != nil {
4946
return err
5047
}
5148

5249
return openapi.Save(o.outputPath, filteredOAS, o.format, o.fs)
5350
}
5451

55-
func (o *Opts) filter(oas *openapi3.T) (result *openapi3.T, err error) {
56-
log.Printf("Filtering OpenAPI document")
57-
return filter.ApplyFilters(oas, filter.NewMetadata(nil, o.env), filter.FiltersWithoutVersioning)
58-
}
59-
6052
func (o *Opts) PreRunE(_ []string) error {
6153
if o.basePath == "" {
6254
return fmt.Errorf("no OAS detected. Please, use the flag %s to include the base OAS", flag.Base)
6355
}
6456

65-
if o.outputPath != "" && !strings.Contains(o.outputPath, openapi.DotJSON) && !strings.Contains(o.outputPath, openapi.DotYAML) {
66-
return fmt.Errorf("output file must be either a JSON or YAML file, got %s", o.outputPath)
67-
}
68-
69-
if o.format != openapi.JSON && o.format != openapi.YAML {
70-
return fmt.Errorf("output format must be either 'json' or 'yaml', got %s", o.format)
71-
}
72-
73-
if strings.Contains(o.basePath, openapi.DotYAML) {
74-
o.format = openapi.YAML
75-
}
76-
77-
return nil
57+
return openapi.ValidateFormatAndOutput(o.format, o.outputPath)
7858
}
7959

8060
// Builder builds the filter command with the following signature:

tools/cli/internal/cli/filter/filter_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,9 @@ import (
2525
"github.com/tufin/oasdiff/load"
2626
)
2727

28-
func TestSuccessfulfilter_Run(t *testing.T) {
28+
func TestSuccessfulFilter_Run(t *testing.T) {
2929
fs := afero.NewMemMapFs()
30-
opts := &Opts{
31-
basePath: "../../../test/data/base_spec.json",
32-
outputPath: "foas.yaml",
33-
fs: fs,
34-
}
35-
36-
if err := opts.Run(); err != nil {
37-
t.Fatalf("Run() unexpected error: %v", err)
38-
}
39-
}
4030

41-
func TestFilterMulitplePreviewsRun(t *testing.T) {
42-
fs := afero.NewMemMapFs()
4331
opts := &Opts{
4432
basePath: "../../../test/data/base_spec.json",
4533
outputPath: "filtered-oas.yaml",
@@ -51,12 +39,17 @@ func TestFilterMulitplePreviewsRun(t *testing.T) {
5139
t.Fatalf("Run() unexpected error: %v", err)
5240
}
5341

54-
// private preview feature 1
55-
info, err := loadRunResultOas(fs, "filtered-oas.yaml")
42+
newSpec, err := loadRunResultOas(fs, opts.outputPath)
5643
require.NoError(t, err)
5744

58-
// check all paths are kept
59-
require.Len(t, info.Spec.Paths.Map(), 231)
45+
// // check all paths are kept
46+
for _, pathItem := range newSpec.Spec.Paths.Map() {
47+
// method
48+
for _, operation := range pathItem.Operations() {
49+
// check extension is removed
50+
require.Nil(t, operation.Extensions)
51+
}
52+
}
6053
}
6154

6255
func TestOpts_PreRunE(t *testing.T) {
@@ -96,13 +89,14 @@ func TestInvalidFormat_PreRun(t *testing.T) {
9689

9790
err := opts.PreRunE(nil)
9891
require.Error(t, err)
99-
require.EqualError(t, err, "output format must be either 'json' or 'yaml', got html")
92+
require.EqualError(t, err, "format must be either 'json', 'yaml' or 'all', got 'html'")
10093
}
10194

10295
func TestInvalidPath_PreRun(t *testing.T) {
10396
opts := &Opts{
10497
outputPath: "foas.html",
10598
basePath: "base.json",
99+
format: "all",
106100
}
107101

108102
err := opts.PreRunE(nil)

tools/cli/internal/cli/merge/merge.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package merge
1717
import (
1818
"encoding/json"
1919
"fmt"
20-
"strings"
2120

2221
"github.com/mongodb/openapi/tools/cli/internal/cli/flag"
2322
"github.com/mongodb/openapi/tools/cli/internal/cli/usage"
@@ -71,11 +70,7 @@ func (o *Opts) PreRunE(_ []string) error {
7170
return fmt.Errorf("no external OAS detected. Please, use the flag %s to include at least one OAS", flag.External)
7271
}
7372

74-
if o.outputPath != "" && !strings.Contains(o.outputPath, ".json") && !strings.Contains(o.outputPath, ".yaml") {
75-
return fmt.Errorf("output file must be either a JSON or YAML file, got %s", o.outputPath)
76-
}
77-
78-
if err := openapi.ValidateFormat(o.format); err != nil {
73+
if err := openapi.ValidateFormatAndOutput(o.format, o.outputPath); err != nil {
7974
return err
8075
}
8176

tools/cli/internal/cli/merge/merge_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func TestInvalidPath_PreRun(t *testing.T) {
159159
outputPath: "foas.html",
160160
externalPaths: externalPaths,
161161
basePath: "base.json",
162+
format: "json",
162163
}
163164

164165
err := opts.PreRunE(nil)

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ func (o *Opts) PreRunE(_ []string) error {
109109
return fmt.Errorf("no OAS detected. Please, use the flag %s to include the base OAS", flag.Base)
110110
}
111111

112-
if o.outputPath != "" && !strings.Contains(o.outputPath, openapi.DotJSON) && !strings.Contains(o.outputPath, openapi.DotYAML) {
113-
return fmt.Errorf("output file must be either a JSON or YAML file, got %s", o.outputPath)
114-
}
115-
116-
return openapi.ValidateFormat(o.format)
112+
return openapi.ValidateFormatAndOutput(o.format, o.outputPath)
117113
}
118114

119115
// Builder builds the split command with the following signature:

tools/cli/internal/openapi/file.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,27 @@ func Save(path string, oas *openapi3.T, format string, fs afero.Fs) error {
131131
return SaveToFile(path, format, newSpec(oas), fs)
132132
}
133133

134-
// ValidateFormat validates the format of files supported.
135-
func ValidateFormat(format string) error {
134+
// ValidateFormatAndOutput validates the format and output file match.
135+
func ValidateFormatAndOutput(format, output string) error {
136136
if format != JSON && format != YAML && format != ALL {
137137
return fmt.Errorf("format must be either 'json', 'yaml' or 'all', got '%s'", format)
138138
}
139139

140+
if output != "" && !strings.Contains(output, DotJSON) && !strings.Contains(output, DotYAML) {
141+
return fmt.Errorf("output file must be either a JSON or YAML file, got %s", output)
142+
}
143+
144+
if format == ALL || output == "" {
145+
return nil
146+
}
147+
148+
if format == JSON && !strings.Contains(output, DotJSON) {
149+
return fmt.Errorf("output file and format mistmatch got format '%s' and output '%s'", format, output)
150+
}
151+
152+
if format == YAML && !strings.Contains(output, DotYAML) {
153+
return fmt.Errorf("output file and format mistmatch got format '%s' and output '%s'", format, output)
154+
}
155+
140156
return nil
141157
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestFiltersWithoutVersioning(t *testing.T) {
120120
metadata := &Metadata{}
121121
filters := FiltersWithoutVersioning(doc, metadata)
122122

123-
assert.Len(t, filters, 5)
123+
assert.Len(t, filters, 4)
124124
}
125125

126126
func TestFiltersToGetVersions(t *testing.T) {

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

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

20-
// 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 and x-xgen-changelog extension from operations.
2121
type OperationsFilter struct {
2222
oas *openapi3.T
2323
}
@@ -35,6 +35,7 @@ func (f *OperationsFilter) Apply() error {
3535
for _, operation := range pathItem.Operations() {
3636
if operation.Extensions != nil {
3737
delete(operation.Extensions, "x-xgen-owner-team")
38+
delete(operation.Extensions, "x-xgen-changelog")
3839
}
3940
}
4041
}

0 commit comments

Comments
 (0)