Skip to content

Commit 83fd1fd

Browse files
committed
update
1 parent ec9aa1e commit 83fd1fd

File tree

2 files changed

+106
-13
lines changed

2 files changed

+106
-13
lines changed

tools/cli/internal/apiversion/version.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func WithDate(date time.Time) Option {
8686
}
8787
}
8888

89-
// WithContent returns an Option to generate a new APIVersion given the contentType.
90-
func WithContent(contentType string) Option {
89+
// withContent returns an Option to generate a new APIVersion given the contentType.
90+
func withContent(contentType string) Option {
9191
return func(v *APIVersion) error {
9292
version, err := Parse(contentType)
9393
if err != nil {
@@ -104,18 +104,19 @@ func WithContent(contentType string) Option {
104104
}
105105
}
106106

107+
// WithFullContent returns an Option to generate a new APIVersion given the contentType and contentValue.
107108
func WithFullContent(contentType string, contentValue *openapi3.MediaType) Option {
108109
return func(v *APIVersion) error {
109110
if !IsPreviewSabilityLevel(contentType) {
110-
return WithContent(contentType)(v)
111+
return withContent(contentType)(v)
111112
}
112113

113-
name, _ := GetPreviewVersionName(contentValue)
114-
if name != "" {
115-
return WithVersion(name)(v)
114+
name, err := GetPreviewVersionName(contentValue)
115+
if err != nil {
116+
return err
116117
}
117-
118-
return WithContent(contentType)(v)
118+
// version will be based on the name, either 'preview' or 'private-preview-<name>'
119+
return WithVersion(name)(v)
119120
}
120121
}
121122

@@ -349,9 +350,21 @@ func parsePreviewExtensionData(contentTypeValue *openapi3.MediaType) (public boo
349350
name = nameV
350351
}
351352

352-
if nameV == "" && publicV != "true" && publicV != "false" {
353-
return false, "", errors.New("invalid value for 'public' field, only 'true' or 'false' are allowed")
353+
if err := validatePreviewExtensionData(name, publicV); err != nil {
354+
return false, "", err
354355
}
355356

356357
return public, name, nil
357358
}
359+
360+
func validatePreviewExtensionData(name string, public string) error {
361+
if name != "" && (public == "true") {
362+
return errors.New("both name and public = true fields are set, only one is allowed")
363+
}
364+
365+
if name == "" && public != "" && public != "true" && public != "false" {
366+
return errors.New("invalid value for 'public' field, only 'true' or 'false' are allowed")
367+
}
368+
369+
return nil
370+
}

tools/cli/internal/apiversion/version_test.go

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func TestNewAPIVersionFromContentType(t *testing.T) {
153153
for _, tt := range testCases {
154154
t.Run(tt.name, func(t *testing.T) {
155155
t.Parallel()
156-
version, err := New(WithContent(tt.contentType))
156+
version, err := New(withContent(tt.contentType))
157157
if tt.wantErr {
158158
assert.Error(t, err)
159159
} else {
@@ -179,9 +179,67 @@ func TestApiVersion_WithFullContent(t *testing.T) {
179179
wantErr: false,
180180
},
181181
{
182-
name: "preview",
183-
contentType: "application/vnd.atlas.preview+json",
182+
name: "csv",
183+
contentType: "application/vnd.atlas.2023-01-02+csv",
184+
contentValue: &openapi3.MediaType{},
185+
186+
expectedMatch: "2023-01-02",
187+
wantErr: false,
188+
},
189+
{
190+
name: "yaml",
191+
contentType: "application/vnd.atlas.2030-02-20+yaml",
184192
contentValue: &openapi3.MediaType{},
193+
expectedMatch: "2030-02-20",
194+
wantErr: false,
195+
},
196+
{
197+
name: "invalid",
198+
199+
contentType: "application/vnd.test.2023-01-01",
200+
contentValue: &openapi3.MediaType{},
201+
expectedMatch: "",
202+
wantErr: true,
203+
},
204+
{
205+
name: "notVersioned",
206+
contentType: "application/json",
207+
contentValue: &openapi3.MediaType{},
208+
expectedMatch: "",
209+
wantErr: true,
210+
},
211+
{
212+
name: "empty",
213+
contentType: "",
214+
contentValue: &openapi3.MediaType{},
215+
expectedMatch: "",
216+
wantErr: true,
217+
},
218+
{
219+
name: "invalidFormat",
220+
contentType: "application/vnd.atlas.2023-01-01",
221+
expectedMatch: "",
222+
contentValue: &openapi3.MediaType{},
223+
wantErr: true,
224+
},
225+
{
226+
name: "invalidDate",
227+
contentType: "application/vnd.atlas.2023111-01-01",
228+
expectedMatch: "",
229+
contentValue: &openapi3.MediaType{},
230+
wantErr: true,
231+
},
232+
233+
{
234+
name: "preview",
235+
contentType: "application/vnd.atlas.preview+json",
236+
contentValue: &openapi3.MediaType{
237+
Extensions: map[string]any{
238+
"x-xgen-preview": map[string]any{
239+
"public": "true",
240+
},
241+
},
242+
},
185243
expectedMatch: "preview",
186244
wantErr: false,
187245
},
@@ -198,6 +256,27 @@ func TestApiVersion_WithFullContent(t *testing.T) {
198256
expectedMatch: "private-preview-feature",
199257
wantErr: false,
200258
},
259+
{
260+
name: "invalid-preview",
261+
contentType: "application/vnd.atlas.preview+json",
262+
contentValue: &openapi3.MediaType{
263+
Extensions: map[string]any{
264+
"x-xgen-preview": map[string]any{
265+
"public": "true",
266+
"name": "feature",
267+
},
268+
},
269+
},
270+
expectedMatch: "private-preview-feature",
271+
wantErr: true,
272+
},
273+
{
274+
name: "invalid-preview",
275+
contentType: "application/vnd.atlas.preview+json",
276+
contentValue: &openapi3.MediaType{},
277+
expectedMatch: "preview",
278+
wantErr: true,
279+
},
201280
}
202281

203282
for _, tt := range testCases {
@@ -207,6 +286,7 @@ func TestApiVersion_WithFullContent(t *testing.T) {
207286
if tt.wantErr {
208287
assert.Error(t, err)
209288
} else {
289+
require.NoError(t, err)
210290
assert.Equal(t, tt.expectedMatch, version.String())
211291
}
212292
})

0 commit comments

Comments
 (0)