-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-319370: Add curl and atlascli samples to the OAS #722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fd69e12
added sample
andreaangiolillo e6437cf
add sample in openapi
andreaangiolillo 17823d4
added sample
andreaangiolillo d80f4df
Revert "add sample in openapi"
andreaangiolillo a3cf10e
fixes e2e tests by regenerating files to include samples
andreaangiolillo 14fdfdc
Update split_test.go
andreaangiolillo f7ac011
Revert "Update split_test.go"
andreaangiolillo 2beb1bd
Update code_sample_test.go
andreaangiolillo c031fb1
Update code_sample.go
andreaangiolillo 5dbf15b
Update code_sample.go
andreaangiolillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package filter | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/mongodb/openapi/tools/cli/internal/apiversion" | ||
) | ||
|
||
const codeSampleExtensionName = "x-codeSamples" | ||
|
||
// https://redocly.com/docs-legacy/api-reference-docs/specification-extensions/x-code-samples#x-codesamples | ||
type codeSample struct { | ||
Lang string `json:"lang,omitempty" yaml:"lang,omitempty"` | ||
Label string `json:"label,omitempty" yaml:"label,omitempty"` | ||
Source string `json:"source,omitempty" yaml:"source,omitempty"` | ||
} | ||
|
||
// CodeSampleFilter modifies includes the fields "x-state" and "x-beta" to the "preview" and "upcoming" APIs Operations. | ||
// The "x-state" and "x-beta" fields are bump.sh custom fields to include budges | ||
// Bump.sh feature: https://docs.bump.sh/help/specification-support/doc-code-samples/#example-usage | ||
type CodeSampleFilter struct { | ||
oas *openapi3.T | ||
metadata *Metadata | ||
} | ||
|
||
func (f *CodeSampleFilter) ValidateMetadata() error { | ||
return validateMetadataWithVersion(f.metadata) | ||
} | ||
|
||
func (f *CodeSampleFilter) Apply() error { | ||
for pathName, p := range f.oas.Paths.Map() { | ||
for opMethod, op := range p.Operations() { | ||
if err := f.includeCodeSamplesForOperation(pathName, opMethod, op); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *CodeSampleFilter) newCurlCodeSamplesForOperation(pathName, opMethod string) codeSample { | ||
source := "curl --user \"{PUBLIC-KEY}:{PRIVATE-KEY}\" \\\n --digest \\\n " + | ||
"--header \"Accept: application/vnd.atlas." + apiVersion(f.metadata.targetVersion) + "+json\" \\\n " | ||
|
||
switch opMethod { | ||
case "GET": | ||
source += "-X " + opMethod + " \"" + pathName + "?pretty=true\"" | ||
case "DELETE": | ||
source += "-X " + opMethod + " \"" + pathName + "\"" | ||
case "POST", "PATCH", "PUT": | ||
source += "-X " + opMethod + " \"" + pathName + "\"\n " | ||
source += "-d " + "{ <Payload> }" | ||
} | ||
|
||
return codeSample{ | ||
Lang: "cURL", | ||
Label: "curl", | ||
Source: source, | ||
} | ||
} | ||
|
||
func apiVersion(version *apiversion.APIVersion) string { | ||
if version.IsStable() { | ||
return version.Date().Format(time.DateOnly) | ||
} | ||
|
||
if version.IsPreview() { | ||
return "preview" | ||
} | ||
|
||
// Upcoming api version | ||
return version.Date().Format(time.DateOnly) + ".upcoming" | ||
} | ||
|
||
func newAtlasCliCodeSamplesForOperation(op *openapi3.Operation) codeSample { | ||
return codeSample{ | ||
Lang: "cURL", | ||
Label: "Atlas CLI", | ||
Source: "atlas api " + op.OperationID + " --help", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more of a q for @jeroenvervaeke or maybe @andreaangiolillo already checked this, are there any OperationIDs/apis not included in auto-gen that we should account for here and filter them out? |
||
} | ||
} | ||
|
||
func (f *CodeSampleFilter) includeCodeSamplesForOperation(pathName, opMethod string, op *openapi3.Operation) error { | ||
if op == nil || opMethod == "" || pathName == "" { | ||
return nil | ||
} | ||
|
||
if op.Extensions == nil { | ||
op.Extensions = map[string]any{} | ||
} | ||
|
||
op.Extensions[codeSampleExtensionName] = []codeSample{ | ||
f.newCurlCodeSamplesForOperation(pathName, opMethod), | ||
newAtlasCliCodeSamplesForOperation(op), | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
// Copyright 2025 MongoDB Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package filter | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/mongodb/openapi/tools/cli/internal/apiversion" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCodeSampleFilter(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
oas *openapi3.T | ||
version string | ||
expectedOas *openapi3.T | ||
}{ | ||
{ | ||
name: "stable api", | ||
version: "2025-01-01", | ||
oas: &openapi3.T{ | ||
Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ | ||
Get: &openapi3.Operation{ | ||
OperationID: "testOperationID", | ||
Summary: "testSummary", | ||
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ | ||
Content: openapi3.Content{ | ||
"application/vnd.atlas.2025-01-01+json": { | ||
Schema: &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/PaginatedAppUserView", | ||
}, | ||
Extensions: map[string]any{ | ||
"x-gen-version": "2025-01-01", | ||
}, | ||
}, | ||
}, | ||
})), | ||
Extensions: map[string]any{ | ||
"x-sunset": "9999-12-31", | ||
}, | ||
}, | ||
})), | ||
}, | ||
expectedOas: &openapi3.T{ | ||
Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ | ||
Get: &openapi3.Operation{ | ||
OperationID: "testOperationID", | ||
Summary: "testSummary", | ||
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ | ||
Content: openapi3.Content{ | ||
"application/vnd.atlas.2025-01-01+json": { | ||
Schema: &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/PaginatedAppUserView", | ||
}, | ||
Extensions: map[string]any{ | ||
"x-gen-version": "2025-01-01", | ||
}, | ||
}, | ||
}, | ||
})), | ||
Extensions: map[string]any{ | ||
"x-sunset": "9999-12-31", | ||
"x-codeSamples": []codeSample{ | ||
{ | ||
Lang: "cURL", | ||
Label: "curl", | ||
Source: "curl --user \"{PUBLIC-KEY}:{PRIVATE-KEY}\" \\\n --digest \\\n " + | ||
"--header \"Accept: application/vnd.atlas.2025-01-01+json\" \\\n " + "-X GET \"test?pretty=true\"", | ||
}, | ||
{ | ||
Lang: "cURL", | ||
Label: "Atlas CLI", | ||
Source: "atlas api testOperationID --help", | ||
}, | ||
}, | ||
}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
{ | ||
name: "preview api", | ||
version: "preview", | ||
oas: &openapi3.T{ | ||
Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ | ||
Get: &openapi3.Operation{ | ||
OperationID: "testOperationID", | ||
Summary: "testSummary", | ||
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ | ||
Content: openapi3.Content{ | ||
"application/vnd.atlas.preview+json": { | ||
Schema: &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/PaginatedAppUserView", | ||
}, | ||
Extensions: map[string]any{ | ||
"x-gen-version": "preview", | ||
}, | ||
}, | ||
}, | ||
})), | ||
Extensions: map[string]any{ | ||
"x-sunset": "9999-12-31", | ||
}, | ||
}, | ||
})), | ||
}, | ||
expectedOas: &openapi3.T{ | ||
Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ | ||
Get: &openapi3.Operation{ | ||
OperationID: "testOperationID", | ||
Summary: "testSummary", | ||
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ | ||
Content: openapi3.Content{ | ||
"application/vnd.atlas.preview+json": { | ||
Schema: &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/PaginatedAppUserView", | ||
}, | ||
Extensions: map[string]any{ | ||
"x-gen-version": "preview", | ||
}, | ||
}, | ||
}, | ||
})), | ||
Extensions: map[string]any{ | ||
"x-sunset": "9999-12-31", | ||
"x-codeSamples": []codeSample{ | ||
{ | ||
Lang: "cURL", | ||
Label: "curl", | ||
Source: "curl --user \"{PUBLIC-KEY}:{PRIVATE-KEY}\" \\\n --digest \\\n " + | ||
"--header \"Accept: application/vnd.atlas.preview+json\" \\\n " + "-X GET \"test?pretty=true\"", | ||
}, | ||
{ | ||
Lang: "cURL", | ||
Label: "Atlas CLI", | ||
Source: "atlas api testOperationID --help", | ||
}, | ||
}, | ||
}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
{ | ||
name: "upcoming api", | ||
version: "2025-01-01.upcoming", | ||
oas: &openapi3.T{ | ||
Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ | ||
Get: &openapi3.Operation{ | ||
OperationID: "testOperationID", | ||
Summary: "testSummary", | ||
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ | ||
Content: openapi3.Content{ | ||
"application/vnd.atlas.2025-01-01.upcoming+json": { | ||
Schema: &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/PaginatedAppUserView", | ||
}, | ||
Extensions: map[string]any{ | ||
"x-gen-version": "2025-01-01.upcoming", | ||
}, | ||
}, | ||
}, | ||
})), | ||
Extensions: map[string]any{ | ||
"x-sunset": "9999-12-31", | ||
}, | ||
}, | ||
})), | ||
}, | ||
expectedOas: &openapi3.T{ | ||
Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ | ||
Get: &openapi3.Operation{ | ||
OperationID: "testOperationID", | ||
Summary: "testSummary", | ||
Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ | ||
Content: openapi3.Content{ | ||
"application/vnd.atlas.2025-01-01.upcoming+json": { | ||
Schema: &openapi3.SchemaRef{ | ||
Ref: "#/components/schemas/PaginatedAppUserView", | ||
}, | ||
Extensions: map[string]any{ | ||
"x-gen-version": "2025-01-01.upcoming", | ||
}, | ||
}, | ||
}, | ||
})), | ||
Extensions: map[string]any{ | ||
"x-sunset": "9999-12-31", | ||
"x-codeSamples": []codeSample{ | ||
{ | ||
Lang: "cURL", | ||
Label: "curl", | ||
Source: "curl --user \"{PUBLIC-KEY}:{PRIVATE-KEY}\" \\\n --digest \\\n " + | ||
"--header \"Accept: application/vnd.atlas.2025-01-01.upcoming+json\" \\\n " + "-X GET \"test?pretty=true\"", | ||
}, | ||
{ | ||
Lang: "cURL", | ||
Label: "Atlas CLI", | ||
Source: "atlas api testOperationID --help", | ||
}, | ||
}, | ||
}, | ||
}, | ||
})), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
oas := tt.oas | ||
version, err := apiversion.New(apiversion.WithVersion(tt.version)) | ||
require.NoError(t, err) | ||
|
||
filter := &CodeSampleFilter{ | ||
oas: oas, | ||
metadata: &Metadata{targetVersion: version, targetEnv: "dev"}, | ||
} | ||
|
||
require.NoError(t, filter.Apply()) | ||
if !reflect.DeepEqual(tt.expectedOas, tt.oas) { | ||
t.Errorf("expected %v, got %v", tt.expectedOas, oas) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.