Skip to content

Commit a821b2e

Browse files
committed
CLOUDP-294902: Support listing private preview values
1 parent c14d3b7 commit a821b2e

File tree

5 files changed

+68693
-6
lines changed

5 files changed

+68693
-6
lines changed

tools/cli/internal/cli/versions/versions_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestVersion_RunWithEnv(t *testing.T) {
5959
func TestVersion_RunWithPreview(t *testing.T) {
6060
fs := afero.NewMemMapFs()
6161
opts := &Opts{
62-
basePath: "../../../test/data/base_spec_with_preview.json",
62+
basePath: "../../../test/data/base_spec_with_private_preview.json",
6363
outputPath: "foas.json",
6464
fs: fs,
6565
env: "staging",
@@ -78,7 +78,7 @@ func TestVersion_RunWithPreview(t *testing.T) {
7878
func TestVersion_RunStabilityLevelPreview(t *testing.T) {
7979
fs := afero.NewMemMapFs()
8080
opts := &Opts{
81-
basePath: "../../../test/data/base_spec_with_preview.json",
81+
basePath: "../../../test/data/base_spec_with_private_preview.json",
8282
outputPath: "foas.json",
8383
fs: fs,
8484
env: "staging",
@@ -92,13 +92,13 @@ func TestVersion_RunStabilityLevelPreview(t *testing.T) {
9292
// Check initial versions
9393
assert.NotEmpty(t, b)
9494
assert.NotContains(t, string(b), "2023-02-01")
95-
assert.Contains(t, string(b), "preview")
95+
assert.Contains(t, string(b), "private-preview")
9696
}
9797

9898
func TestVersion_RunStabilityLevelStable(t *testing.T) {
9999
fs := afero.NewMemMapFs()
100100
opts := &Opts{
101-
basePath: "../../../test/data/base_spec_with_preview.json",
101+
basePath: "../../../test/data/base_spec_with_private_preview.json",
102102
outputPath: "foas.json",
103103
fs: fs,
104104
env: "staging",
@@ -112,5 +112,26 @@ func TestVersion_RunStabilityLevelStable(t *testing.T) {
112112
// Check initial versions
113113
assert.NotEmpty(t, b)
114114
assert.Contains(t, string(b), "2023-02-01")
115-
assert.NotContains(t, string(b), "preview")
115+
assert.NotContains(t, string(b), "private-review")
116+
}
117+
118+
func TestVersion_PreviewAndPublicPreview(t *testing.T) {
119+
fs := afero.NewMemMapFs()
120+
opts := &Opts{
121+
basePath: "../../../test/data/base_spec_with_public_preview.json",
122+
outputPath: "foas.json",
123+
fs: fs,
124+
env: "staging",
125+
stabilityLevel: "PREVIEW",
126+
}
127+
128+
require.NoError(t, opts.Run())
129+
b, err := afero.ReadFile(fs, opts.outputPath)
130+
require.NoError(t, err)
131+
132+
// Check initial versions
133+
assert.NotEmpty(t, b)
134+
assert.Contains(t, string(b), "2023-02-01")
135+
assert.NotContains(t, string(b), "private-preview")
136+
assert.Contains(t, string(b), "preview")
116137
}

tools/cli/internal/openapi/versions.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func extractVersions(oas *openapi3.T) ([]string, error) {
6767
version, err = getPreviewVersionName(contentTypeValue)
6868
if err != nil {
6969
fmt.Printf("failed to parse preview version name: %v\n", err)
70-
continue
70+
return nil, err
7171
}
7272
}
7373

@@ -137,6 +137,10 @@ func parsePreviewExtensionData(contentTypeValue *openapi3.MediaType) (public boo
137137
name = nameV
138138
}
139139

140+
if nameV == "" && publicV != "true" && publicV != "false" {
141+
return false, "", errors.New("invalid value for 'public' field, only 'true' or 'false' are allowed")
142+
}
143+
140144
return public, name, nil
141145
}
142146

tools/cli/internal/openapi/versions_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ func TestVersions_PublicPreview(t *testing.T) {
3939
assert.Equal(t, []string{"2023-01-01", "2023-02-01", "preview"}, versions)
4040
}
4141

42+
func TestVersions_InvalidPreviewData(t *testing.T) {
43+
r := NewVersionedResponses(t)
44+
// override the extension so something invalid like "public": true
45+
r.Paths.Find("pathBase4").Post.Responses.Map()["200"].Value.Content.Get("application/vnd.atlas.preview+json").Extensions["x-xgen-preview"] = map[string]any{
46+
"public": true,
47+
}
48+
49+
_, err := ExtractVersionsWithEnv(r, "qa")
50+
require.Error(t, err)
51+
require.ErrorContains(t, err, "nvalid value for 'public' field")
52+
}
53+
4254
func NewVersionedResponses(t *testing.T) *openapi3.T {
4355
t.Helper()
4456
inputPath := &openapi3.Paths{}
@@ -95,6 +107,7 @@ func NewVersionedResponses(t *testing.T) *openapi3.T {
95107
},
96108
})
97109

110+
// private preview version
98111
extensionThree := map[string]any{
99112
"x-xgen-version": "preview",
100113
"x-xgen-preview": map[string]any{
@@ -133,6 +146,7 @@ func NewVersionedResponses(t *testing.T) *openapi3.T {
133146
},
134147
})
135148

149+
// public preview version
136150
extensionFour := map[string]any{
137151
"x-xgen-version": "preview",
138152
"x-xgen-preview": map[string]any{

0 commit comments

Comments
 (0)