Skip to content

Commit 89a23a3

Browse files
Merge remote-tracking branch 'origin/main' into CLOUDP-294587
2 parents 04361db + 933fcbd commit 89a23a3

File tree

6 files changed

+116
-130
lines changed

6 files changed

+116
-130
lines changed

tools/cli/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ E2E_TIMEOUT?=60m
1313
E2E_PARALLEL?=1
1414
E2E_EXTRA_ARGS?=
1515

16-
17-
1816
LINKER_GH_SHA_FLAG=-s -w -X github.com/mongodb/openapi/tools/cli/internal/version.GitCommit=${GIT_SHA}
1917
LINKER_FLAGS=${LINKER_GH_SHA_FLAG} -X github.com/mongodb/openapi/tools/cli/internal/version.Version=${VERSION}
2018

tools/cli/internal/apiversion/version.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func Parse(contentType string) (string, error) {
164164
}
165165

166166
// FindLatestContentVersionMatched finds the latest content version that matches the requested version.
167-
func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *APIVersion) (*APIVersion, error) {
167+
func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *APIVersion) *APIVersion {
168168
/*
169169
given:
170170
version: 2024-01-01
@@ -178,7 +178,7 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
178178
*/
179179
var latestVersionMatch *APIVersion
180180
if op.Responses == nil {
181-
return requestedVersion, nil
181+
return requestedVersion
182182
}
183183

184184
for _, response := range op.Responses.Map() {
@@ -189,15 +189,15 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
189189
for contentType := range response.Value.Content {
190190
contentVersion, err := New(WithContent(contentType))
191191
if err != nil {
192-
log.Printf("Ignoring invalid content type: %s", contentType)
192+
log.Printf("Ignoring invalid content type: %q", contentType)
193193
continue
194194
}
195195
if contentVersion.GreaterThan(requestedVersion) {
196196
continue
197197
}
198198

199199
if contentVersion.Equal(requestedVersion) {
200-
return contentVersion, nil
200+
return contentVersion
201201
}
202202

203203
if latestVersionMatch == nil || contentVersion.GreaterThan(latestVersionMatch) {
@@ -207,10 +207,10 @@ func FindLatestContentVersionMatched(op *openapi3.Operation, requestedVersion *A
207207
}
208208

209209
if latestVersionMatch == nil {
210-
return requestedVersion, nil
210+
return requestedVersion
211211
}
212212

213-
return latestVersionMatch, nil
213+
return latestVersionMatch
214214
}
215215

216216
// Sort versions

tools/cli/internal/apiversion/version_test.go

Lines changed: 96 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"testing"
1919
"time"
2020

21+
"github.com/getkin/kin-openapi/openapi3"
2122
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
2224
)
2325

2426
func TestParseVersion(t *testing.T) {
@@ -126,6 +128,7 @@ func TestNewAPIVersionFromContentType(t *testing.T) {
126128
for _, tt := range testCases {
127129
t.Run(tt.name, func(t *testing.T) {
128130
version, err := New(WithContent(tt.contentType))
131+
t.Parallel()
129132
if tt.wantErr {
130133
assert.Error(t, err)
131134
} else {
@@ -158,6 +161,7 @@ func TestApiVersion_GreaterThan(t *testing.T) {
158161

159162
for _, tt := range testCases {
160163
t.Run(tt.name, func(t *testing.T) {
164+
t.Parallel()
161165
v1, _ := New(WithVersion(tt.version1))
162166
v2, _ := New(WithVersion(tt.version2))
163167
assert.Equal(t, tt.expected, v1.GreaterThan(v2))
@@ -188,6 +192,7 @@ func TestApiVersion_LessThan(t *testing.T) {
188192

189193
for _, tt := range testCases {
190194
t.Run(tt.name, func(t *testing.T) {
195+
t.Parallel()
191196
v1, _ := New(WithVersion(tt.version1))
192197
v2, _ := New(WithVersion(tt.version2))
193198
assert.Equal(t, tt.expected, v1.LessThan(v2))
@@ -215,6 +220,7 @@ func TestApiVersion_IsZero(t *testing.T) {
215220

216221
for _, tt := range testCases {
217222
t.Run(tt.name, func(t *testing.T) {
223+
t.Parallel()
218224
v, _ := New(WithVersion(tt.version))
219225
assert.Equal(t, tt.expected, v.IsZero())
220226
})
@@ -275,6 +281,7 @@ func TestApiVersion_Equal(t *testing.T) {
275281

276282
for _, tt := range testCases {
277283
t.Run(tt.name, func(t *testing.T) {
284+
t.Parallel()
278285
v1, _ := New(WithVersion(tt.version1))
279286
v2, _ := New(WithVersion(tt.version2))
280287
assert.Equal(t, tt.expected, v1.Equal(v2))
@@ -311,6 +318,7 @@ func TestNewAPIVersionFromTime(t *testing.T) {
311318

312319
for _, tt := range testCases {
313320
t.Run(tt.name, func(t *testing.T) {
321+
t.Parallel()
314322
timeValue, _ := time.Parse("2006-01-02", tt.time)
315323
match, err := New(WithDate(timeValue))
316324
if tt.wantErr {
@@ -327,114 +335,163 @@ func TestNewVersionDate(t *testing.T) {
327335
name string
328336
version string
329337
expectedMatch string
330-
wantErr bool
338+
wantErr require.ErrorAssertionFunc
331339
}{
332340
{
333341
name: "2023-01-01",
334342
version: "2023-01-01",
335343
expectedMatch: "2023-01-01",
336-
wantErr: false,
344+
wantErr: require.NoError,
337345
},
338346
{
339347
name: "2023-01-02",
340348
version: "2023-01-02",
341349
expectedMatch: "2023-01-02",
342-
wantErr: false,
350+
wantErr: require.NoError,
343351
},
344352
{
345353
name: "2030-02-20",
346354
version: "2030-02-20",
347355
expectedMatch: "2030-02-20",
348-
wantErr: false,
356+
wantErr: require.NoError,
349357
},
350358
}
351359

352360
for _, tt := range testCases {
353361
t.Run(tt.name, func(t *testing.T) {
362+
t.Parallel()
354363
match, err := DateFromVersion(tt.version)
355-
if tt.wantErr {
356-
assert.Error(t, err)
357-
} else {
358-
assert.Equal(t, tt.expectedMatch, match.Format(dateFormat))
359-
}
364+
tt.wantErr(t, err)
365+
assert.Equal(t, tt.expectedMatch, match.Format(dateFormat))
360366
})
361367
}
362368
}
363369

364-
func TestNewAPIVersionFromDateString(t *testing.T) {
370+
func TestNew_WithVersion(t *testing.T) {
365371
testCases := []struct {
366372
name string
367373
version string
368374
expectedMatch string
369-
wantErr bool
375+
wantErr require.ErrorAssertionFunc
370376
}{
371377
{
372378
name: "2023-01-01",
373379
version: "2023-01-01",
374380
expectedMatch: "2023-01-01",
375-
wantErr: false,
381+
wantErr: require.NoError,
376382
},
377383
{
378384
name: "2023-01-02",
379385
version: "2023-01-02",
380386
expectedMatch: "2023-01-02",
381-
wantErr: false,
387+
wantErr: require.NoError,
382388
},
383389
{
384390
name: "2030-02-20",
385391
version: "2030-02-20",
386392
expectedMatch: "2030-02-20",
387-
wantErr: false,
393+
wantErr: require.NoError,
388394
},
389395
}
390396

391397
for _, tt := range testCases {
392398
t.Run(tt.name, func(t *testing.T) {
399+
t.Parallel()
393400
match, err := New(WithVersion(tt.version))
394-
if tt.wantErr {
395-
assert.Error(t, err)
396-
} else {
397-
assert.Equal(t, tt.expectedMatch, match.String())
398-
}
401+
tt.wantErr(t, err)
402+
assert.Equal(t, tt.expectedMatch, match.String())
399403
})
400404
}
401405
}
402406

403-
func TestNewAPIVersion(t *testing.T) {
407+
func TestFindLatestContentVersionMatched(t *testing.T) {
404408
testCases := []struct {
405409
name string
406-
version string
410+
targetVersion string
407411
expectedMatch string
408-
wantErr bool
409412
}{
410413
{
411-
name: "2023-01-01",
412-
version: "2023-01-01",
414+
name: "exact match 2023-01-01",
415+
targetVersion: "2023-01-01",
413416
expectedMatch: "2023-01-01",
414-
wantErr: false,
415417
},
416418
{
417-
name: "2023-01-02",
418-
version: "2023-01-02",
419-
expectedMatch: "2023-01-02",
420-
wantErr: false,
419+
name: "exact match 2023-11-15",
420+
targetVersion: "2023-11-15",
421+
expectedMatch: "2023-11-15",
421422
},
422423
{
423-
name: "2030-02-20",
424-
version: "2030-02-20",
425-
expectedMatch: "2030-02-20",
426-
wantErr: false,
424+
name: "exact match 2024-05-30",
425+
targetVersion: "2024-05-30",
426+
expectedMatch: "2024-05-30",
427+
},
428+
{
429+
name: "approx match 2023-01-01",
430+
targetVersion: "2023-01-02",
431+
expectedMatch: "2023-01-01",
432+
},
433+
{
434+
name: "approx match 2023-01-01",
435+
targetVersion: "2023-01-31",
436+
expectedMatch: "2023-01-01",
437+
},
438+
{
439+
name: "approx match 2023-02-01",
440+
targetVersion: "2023-02-20",
441+
expectedMatch: "2023-02-01",
442+
},
443+
{
444+
name: "future date",
445+
targetVersion: "2030-02-20",
446+
expectedMatch: "2024-05-30",
447+
},
448+
{
449+
name: "past date",
450+
targetVersion: "1999-02-20",
451+
expectedMatch: "1999-02-20",
427452
},
428453
}
429454

430455
for _, tt := range testCases {
431456
t.Run(tt.name, func(t *testing.T) {
432-
match, err := New(WithVersion(tt.version))
433-
if tt.wantErr {
434-
assert.Error(t, err)
435-
} else {
436-
assert.Equal(t, tt.expectedMatch, match.String())
437-
}
457+
t.Parallel()
458+
targetVersion, err := New(WithVersion(tt.targetVersion))
459+
require.NoError(t, err)
460+
r := FindLatestContentVersionMatched(oasOperationAllVersions(), targetVersion)
461+
// transform time to str with format "2006-01-02"
462+
assert.Equal(t, tt.expectedMatch, r.String())
438463
})
439464
}
440465
}
466+
467+
func oasOperationAllVersions() *openapi3.Operation {
468+
responses := &openapi3.Responses{}
469+
responses.Set("200", &openapi3.ResponseRef{
470+
Value: &openapi3.Response{
471+
Content: map[string]*openapi3.MediaType{
472+
"application/vnd.atlas.2023-01-01+json": {},
473+
"application/vnd.atlas.2023-01-01+csv": {},
474+
"application/vnd.atlas.2023-02-01+json": {},
475+
"application/vnd.atlas.2023-10-01+json": {},
476+
"application/vnd.atlas.2023-11-15+json": {},
477+
"application/vnd.atlas.2024-05-30+json": {},
478+
},
479+
},
480+
})
481+
482+
return &openapi3.Operation{
483+
OperationID: "operationId",
484+
Responses: responses,
485+
RequestBody: &openapi3.RequestBodyRef{
486+
Value: &openapi3.RequestBody{
487+
Content: map[string]*openapi3.MediaType{
488+
"application/vnd.atlas.2023-01-01+json": {},
489+
"application/vnd.atlas.2023-02-01+json": {},
490+
"application/vnd.atlas.2023-10-01+json": {},
491+
"application/vnd.atlas.2023-11-15+json": {},
492+
"application/vnd.atlas.2024-05-30+json": {},
493+
},
494+
},
495+
},
496+
}
497+
}

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package filter
1516

1617
import (
@@ -29,9 +30,11 @@ type ExtensionFilter struct {
2930
metadata *Metadata
3031
}
3132

32-
const sunsetExtension = "x-sunset"
33-
const xGenExtension = "x-xgen-version"
34-
const format = "2006-01-02T15:04:05Z07:00"
33+
const (
34+
sunsetExtension = "x-sunset"
35+
xGenExtension = "x-xgen-version"
36+
format = "2006-01-02T15:04:05Z07:00"
37+
)
3538

3639
func (f *ExtensionFilter) Apply() error {
3740
for _, pathItem := range f.oas.Paths.Map() {
@@ -47,11 +50,7 @@ func (f *ExtensionFilter) Apply() error {
4750

4851
updateExtensionToDateString(operation.Extensions)
4952

50-
latestVersionMatch, err := apiversion.FindLatestContentVersionMatched(operation, f.metadata.targetVersion)
51-
if err != nil {
52-
return err
53-
}
54-
53+
latestVersionMatch := apiversion.FindLatestContentVersionMatched(operation, f.metadata.targetVersion)
5554
for _, response := range operation.Responses.Map() {
5655
if response == nil {
5756
continue
@@ -89,15 +88,15 @@ func updateExtensionToDateString(extensions map[string]any) {
8988
return
9089
}
9190

92-
for key, value := range extensions {
93-
if key != sunsetExtension && key != xGenExtension {
91+
for k, v := range extensions {
92+
if k != sunsetExtension && k != xGenExtension {
9493
continue
9594
}
96-
date, err := time.Parse(format, value.(string))
95+
date, err := time.Parse(format, v.(string))
9796
if err != nil {
9897
continue
9998
}
100-
extensions[key] = date.Format("2006-01-02")
99+
extensions[k] = date.Format("2006-01-02")
101100
}
102101
}
103102

0 commit comments

Comments
 (0)