Skip to content

Commit 8c6b263

Browse files
committed
update
1 parent 1fae86e commit 8c6b263

File tree

2 files changed

+95
-25
lines changed

2 files changed

+95
-25
lines changed

tools/cli/internal/openapi/file.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ const (
3535
)
3636

3737
// SaveToFile saves the content to a file in the specified format.
38-
// If format is empty, it saves the content in both JSON and YAML formats.
38+
// If format is empty or set to 'all', it saves the content in both JSON and YAML formats.
3939
func SaveToFile[T any](path, format string, content T, fs afero.Fs) error {
4040
data, err := SerializeToJSON(content)
4141
if err != nil {
4242
return err
4343
}
4444

45+
if format == ALL || format == "" {
46+
// strip . format from path
47+
path = strings.TrimSuffix(path, DotJSON)
48+
path = strings.TrimSuffix(path, DotYAML)
49+
}
50+
4551
if format == JSON || format == "" || format == ALL {
4652
jsonPath := newPathWithExtension(path, JSON)
4753
if errJSON := afero.WriteFile(fs, jsonPath, data, 0o600); errJSON != nil {

tools/cli/internal/openapi/file_test.go

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/getkin/kin-openapi/openapi3"
21+
"github.com/spf13/afero"
2122
"github.com/stretchr/testify/assert"
2223
"github.com/stretchr/testify/require"
2324
)
@@ -31,35 +32,15 @@ func TestNewArrayBytesFromOAS(t *testing.T) {
3132
expected string
3233
}{
3334
{
34-
name: "JSON with HTML characters",
35-
spec: &Spec{
36-
Paths: openapi3.NewPaths(
37-
openapi3.WithPath(
38-
"/api/atlas/v2/groups/{groupId}/databaseUsers/{databaseName}/{username}",
39-
&openapi3.PathItem{
40-
Delete: &openapi3.Operation{
41-
Description: "<test>&</test>",
42-
},
43-
},
44-
)),
45-
},
35+
name: "JSON with HTML characters",
36+
spec: getTestSpecWithHtmlChars(),
4637
path: "test.json",
4738
format: "json",
4839
expected: "<test>&</test>",
4940
},
5041
{
51-
name: "YAML with HTML characters",
52-
spec: &Spec{
53-
Paths: openapi3.NewPaths(
54-
openapi3.WithPath(
55-
"/api/atlas/v2/groups/{groupId}/databaseUsers/{databaseName}/{username}",
56-
&openapi3.PathItem{
57-
Delete: &openapi3.Operation{
58-
Description: "<test>&</test>",
59-
},
60-
},
61-
)),
62-
},
42+
name: "YAML with HTML characters",
43+
spec: getTestSpecWithHtmlChars(),
6344
path: "test.yaml",
6445
format: "yaml",
6546
expected: "<test>&</test>",
@@ -109,3 +90,86 @@ func TestNewArrayBytesFromOAS(t *testing.T) {
10990
})
11091
}
11192
}
93+
94+
func TestSaveToFileFormats(t *testing.T) {
95+
tests := []struct {
96+
name string
97+
spec *Spec
98+
path string
99+
format string
100+
expected string
101+
}{
102+
{
103+
name: "JSON with HTML characters",
104+
spec: getTestSpecWithHtmlChars(),
105+
path: "test.json",
106+
format: "json",
107+
expected: "<test>&</test>",
108+
},
109+
{
110+
name: "YAML with HTML characters",
111+
spec: getTestSpecWithHtmlChars(),
112+
113+
path: "test.yaml",
114+
format: "yaml",
115+
expected: "<test>&</test>",
116+
},
117+
{
118+
name: "all with HTML characters",
119+
spec: getTestSpecWithHtmlChars(),
120+
path: "test.yaml",
121+
format: "all",
122+
expected: "<test>&</test>",
123+
},
124+
{
125+
name: "empty format with HTML characters",
126+
spec: getTestSpecWithHtmlChars(),
127+
path: "test.yaml",
128+
format: "",
129+
expected: "<test>&</test>",
130+
},
131+
}
132+
133+
for _, tt := range tests {
134+
t.Run(tt.name, func(t *testing.T) {
135+
fs := afero.NewMemMapFs()
136+
err := SaveToFile(tt.path, tt.format, tt.spec, fs)
137+
require.NoError(t, err)
138+
139+
data, err := afero.ReadFile(fs, tt.path)
140+
require.NoError(t, err)
141+
assert.Contains(t, string(data), tt.expected)
142+
})
143+
}
144+
}
145+
146+
func TestSaveToFile_All(t *testing.T) {
147+
fs := afero.NewMemMapFs()
148+
err := SaveToFile("test.yaml", "all", getTestSpecWithHtmlChars(), fs)
149+
require.NoError(t, err)
150+
151+
// read yaml file
152+
data, err := afero.ReadFile(fs, "test.yaml")
153+
require.NoError(t, err)
154+
assert.Contains(t, string(data), "<test>&</test>")
155+
156+
// read json file
157+
data, err = afero.ReadFile(fs, "test.json")
158+
require.NoError(t, err)
159+
assert.Contains(t, string(data), "<test>&</test>")
160+
}
161+
162+
func getTestSpecWithHtmlChars() *Spec {
163+
return &Spec{
164+
Paths: openapi3.NewPaths(
165+
openapi3.WithPath(
166+
"/api/atlas/v2/groups/{groupId}/databaseUsers/{databaseName}/{username}",
167+
&openapi3.PathItem{
168+
Delete: &openapi3.Operation{
169+
Description: "<test>&</test>",
170+
},
171+
},
172+
),
173+
),
174+
}
175+
}

0 commit comments

Comments
 (0)