Skip to content

Commit 73cdf93

Browse files
feat: Added 'x-xgen-operation-id-override' support (#956)
1 parent 0493cbc commit 73cdf93

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ func apiVersion(version *apiversion.APIVersion) string {
150150
func newAtlasCliCodeSamplesForOperation(op *openapi3.Operation) codeSample {
151151
tag := strcase.ToLowerCamel(op.Tags[0])
152152
operationID := strcase.ToLowerCamel(op.OperationID)
153+
154+
extensions := op.Extensions
155+
if extensions != nil {
156+
override := extensions["x-xgen-operation-id-override"]
157+
if overrideString, ok := override.(string); ok {
158+
operationID = overrideString
159+
}
160+
}
161+
153162
return codeSample{
154163
Lang: "cURL",
155164
Label: "Atlas CLI",

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/stretchr/testify/require"
2424
)
2525

26+
//nolint:funlen // Test cases require long function for comprehensive coverage
2627
func TestCodeSampleFilter(t *testing.T) {
2728
testCases := []struct {
2829
name string
@@ -352,6 +353,97 @@ func TestCodeSampleFilter(t *testing.T) {
352353
})),
353354
},
354355
},
356+
{
357+
name: "stable api with x-xgen-operation-id-override",
358+
version: "2025-01-01",
359+
oas: &openapi3.T{
360+
Paths: openapi3.NewPaths(openapi3.WithPath("/test", &openapi3.PathItem{
361+
Get: &openapi3.Operation{
362+
OperationID: "testOperationID",
363+
Summary: "testSummary",
364+
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{
365+
Content: openapi3.Content{
366+
"application/vnd.atlas.2025-01-01+json": {
367+
Schema: &openapi3.SchemaRef{
368+
Ref: "#/components/schemas/PaginatedAppUserView",
369+
},
370+
Extensions: map[string]any{
371+
"x-gen-version": "2025-01-01",
372+
},
373+
},
374+
},
375+
})),
376+
Tags: []string{"TestTag"},
377+
Extensions: map[string]any{
378+
"x-sunset": "9999-12-31",
379+
"x-xgen-operation-id-override": "customOperationName",
380+
},
381+
},
382+
})),
383+
},
384+
expectedOas: &openapi3.T{
385+
Paths: openapi3.NewPaths(openapi3.WithPath("/test", &openapi3.PathItem{
386+
Get: &openapi3.Operation{
387+
OperationID: "testOperationID",
388+
Summary: "testSummary",
389+
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{
390+
Content: openapi3.Content{
391+
"application/vnd.atlas.2025-01-01+json": {
392+
Schema: &openapi3.SchemaRef{
393+
Ref: "#/components/schemas/PaginatedAppUserView",
394+
},
395+
Extensions: map[string]any{
396+
"x-gen-version": "2025-01-01",
397+
},
398+
},
399+
},
400+
})),
401+
Tags: []string{"TestTag"},
402+
Extensions: map[string]any{
403+
"x-sunset": "9999-12-31",
404+
"x-xgen-operation-id-override": "customOperationName",
405+
"x-codeSamples": []codeSample{
406+
{
407+
Lang: "cURL",
408+
Label: "Atlas CLI",
409+
Source: "atlas api testTag customOperationName --help",
410+
},
411+
{
412+
Lang: "go",
413+
Label: "Go",
414+
Source: "import (\n" +
415+
"\t\"os\"\n \"context\"\n" + "\t\"log\"\n" +
416+
"\tsdk \"go.mongodb.org/atlas-sdk/v20250101001/admin\"\n)\n\n" +
417+
"func main() {\n" +
418+
"\tctx := context.Background()\n" +
419+
"\tclientID := os.Getenv(\"MONGODB_ATLAS_CLIENT_ID\")\n" +
420+
"\tclientSecret := os.Getenv(\"MONGODB_ATLAS_CLIENT_SECRET\")\n\n" +
421+
"\t// See https://dochub.mongodb.org/core/atlas-go-sdk-oauth\n" +
422+
"\tclient, err := sdk.NewClient(sdk.UseOAuthAuth(clientID, clientSecret))\n\n" +
423+
"\tif err != nil {\n" + "\t\tlog.Fatalf(\"Error: %v\", err)\n\t}\n\n" +
424+
"\tparams = &sdk.TestOperationIDApiParams{}\n" +
425+
"\tsdkResp, httpResp, err := client.TestTagApi.\n" +
426+
"\t\tTestOperationIDWithParams(ctx, params).\n" +
427+
"\t\tExecute()" + "\n}\n",
428+
},
429+
{
430+
Lang: "cURL",
431+
Label: "curl (Service Accounts)",
432+
Source: "curl --include --header \"Authorization: Bearer ${ACCESS_TOKEN}\" \\\n " +
433+
"--header \"Accept: application/vnd.atlas.2025-01-01+json\" \\\n " + "-X GET \"https://cloud.mongodb.com/test?pretty=true\"",
434+
},
435+
{
436+
Lang: "cURL",
437+
Label: "curl (Digest)",
438+
Source: "curl --user \"${PUBLIC_KEY}:${PRIVATE_KEY}\" \\\n --digest --include \\\n " +
439+
"--header \"Accept: application/vnd.atlas.2025-01-01+json\" \\\n " + "-X GET \"https://cloud.mongodb.com/test?pretty=true\"",
440+
},
441+
},
442+
},
443+
},
444+
})),
445+
},
446+
},
355447
}
356448

357449
for _, tt := range testCases {

0 commit comments

Comments
 (0)