Skip to content

Commit 1a9cfce

Browse files
fix snapshots for api-generation (#3932)
1 parent 2cb5d03 commit 1a9cfce

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

tools/cmd/api-generator/convert_commands.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ var (
3232
versionRegex = regexp.MustCompile(`^application/vnd\.atlas\.(?P<version>\d{4}-\d{2}-\d{2}|preview|upcoming)\+(?P<contentType>[\w]+)$`)
3333
)
3434

35-
func specToCommands(spec *openapi3.T) (api.GroupedAndSortedCommands, error) {
35+
func specToCommands(now time.Time, spec *openapi3.T) (api.GroupedAndSortedCommands, error) {
3636
groups := make(map[string]*api.Group, 0)
3737

3838
for path, item := range spec.Paths.Map() {
3939
for verb, operation := range item.Operations() {
40-
command, err := operationToCommand(path, verb, operation)
40+
command, err := operationToCommand(now, path, verb, operation)
4141
if err != nil {
4242
return nil, fmt.Errorf("failed to convert operation to command: %w", err)
4343
}
@@ -134,7 +134,7 @@ func extractExtensionsFromOperation(operation *openapi3.Operation) operationExte
134134
return ext
135135
}
136136

137-
func operationToCommand(path, verb string, operation *openapi3.Operation) (*api.Command, error) {
137+
func operationToCommand(now time.Time, path, verb string, operation *openapi3.Operation) (*api.Command, error) {
138138
extensions := extractExtensionsFromOperation(operation)
139139
if extensions.skip {
140140
return nil, nil
@@ -153,7 +153,7 @@ func operationToCommand(path, verb string, operation *openapi3.Operation) (*api.
153153
return nil, err
154154
}
155155

156-
versions, err := buildVersions(operation)
156+
versions, err := buildVersions(now, operation)
157157
if err != nil {
158158
return nil, err
159159
}
@@ -366,7 +366,7 @@ func extractParameters(parameters openapi3.Parameters) (parameterSet, error) {
366366
}
367367

368368
// Build versions from responses and request body.
369-
func buildVersions(operation *openapi3.Operation) ([]api.Version, error) {
369+
func buildVersions(now time.Time, operation *openapi3.Operation) ([]api.Version, error) {
370370
versionsMap := make(map[string]*api.Version)
371371

372372
if err := processResponses(operation.Responses, versionsMap); err != nil {
@@ -379,7 +379,7 @@ func buildVersions(operation *openapi3.Operation) ([]api.Version, error) {
379379

380380
// filter sunsetted versions
381381
for key, version := range versionsMap {
382-
if version.Sunset != nil && time.Now().After(*version.Sunset) {
382+
if version.Sunset != nil && now.After(*version.Sunset) {
383383
delete(versionsMap, key)
384384
}
385385
}

tools/cmd/api-generator/main.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/getkin/kin-openapi/openapi3"
32+
"github.com/mongodb/mongodb-atlas-cli/atlascli/tools/internal/metadatatypes"
3233
"github.com/spf13/cobra"
3334
)
3435

@@ -91,25 +92,29 @@ func run(ctx context.Context, specPath string, outputType OutputType, w io.Write
9192
defer specFile.Close()
9293
}
9394

95+
now := time.Now()
96+
9497
switch outputType {
9598
case Commands:
96-
return convertSpecToAPICommands(ctx, spec, w)
99+
return convertSpecToAPICommands(ctx, now, spec, w)
97100
case Metadata:
98-
return convertSpecToMetadata(ctx, spec, w)
101+
return convertSpecToMetadata(ctx, now, spec, w)
99102
default:
100103
return fmt.Errorf("'%s' is not a valid outputType", outputType)
101104
}
102105
}
103106

104-
func convertSpecToAPICommands(ctx context.Context, r io.Reader, w io.Writer) error {
105-
return convertSpec(ctx, r, w, specToCommands, commandsTemplateContent)
107+
func convertSpecToAPICommands(ctx context.Context, now time.Time, r io.Reader, w io.Writer) error {
108+
return convertSpec(ctx, now, r, w, specToCommands, commandsTemplateContent)
106109
}
107110

108-
func convertSpecToMetadata(ctx context.Context, r io.Reader, w io.Writer) error {
109-
return convertSpec(ctx, r, w, specToMetadata, metadataTemplateContent)
111+
func convertSpecToMetadata(ctx context.Context, now time.Time, r io.Reader, w io.Writer) error {
112+
return convertSpec(ctx, now, r, w, func(_ time.Time, spec *openapi3.T) (metadatatypes.Metadata, error) {
113+
return specToMetadata(spec)
114+
}, metadataTemplateContent)
110115
}
111116

112-
func convertSpec[T any](ctx context.Context, r io.Reader, w io.Writer, mapper func(spec *openapi3.T) (T, error), templateContent string) error {
117+
func convertSpec[T any](ctx context.Context, now time.Time, r io.Reader, w io.Writer, mapper func(now time.Time, spec *openapi3.T) (T, error), templateContent string) error {
113118
spec, err := loadSpec(r)
114119
if err != nil {
115120
return fmt.Errorf("failed to load spec, error: %w", err)
@@ -121,7 +126,7 @@ func convertSpec[T any](ctx context.Context, r io.Reader, w io.Writer, mapper fu
121126
}
122127
}
123128

124-
commands, err := mapper(spec)
129+
commands, err := mapper(now, spec)
125130
if err != nil {
126131
return fmt.Errorf("failed convert spec to api commands: %w", err)
127132
}

tools/cmd/api-generator/main_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ import (
2222
"os"
2323
"path/filepath"
2424
"testing"
25+
"time"
2526

2627
"github.com/bradleyjkemp/cupaloy/v2"
2728
)
2829

2930
func testSpec(t *testing.T, name, specPath string) {
3031
t.Helper()
3132

33+
snapshotTime := time.Date(2025, time.May, 1, 0, 0, 0, 0, time.UTC)
3234
snapshotter := cupaloy.New(cupaloy.SnapshotSubdirectory("testdata/.snapshots"), cupaloy.SnapshotFileExtension(".snapshot"))
3335

34-
outputFunctions := map[OutputType]func(ctx context.Context, r io.Reader, w io.Writer) error{
36+
outputFunctions := map[OutputType]func(ctx context.Context, now time.Time, r io.Reader, w io.Writer) error{
3537
Commands: convertSpecToAPICommands,
3638
Metadata: convertSpecToMetadata,
3739
}
@@ -46,7 +48,7 @@ func testSpec(t *testing.T, name, specPath string) {
4648
})
4749

4850
buf := &bytes.Buffer{}
49-
if err := outputTypeFunc(t.Context(), specFile, buf); err != nil {
51+
if err := outputTypeFunc(t.Context(), snapshotTime, specFile, buf); err != nil {
5052
t.Fatalf("failed to convert spec into %s, error: %s", outputType, err)
5153
}
5254

0 commit comments

Comments
 (0)