We use custom mustache templates to generate the open api client for the API specs. The templates are located under the ./templates/go folder
Step 1: Generate the the client for a Schema using open api Docker image or cli. Provide the following parameters
-ipath to open api spec yaml-tpath to custom templates in./templates/go-ggenerator typego-experimental-p packageName= <Api namespace in StartCase>-ooutput path./src/<Api namespace in lowercase>
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/schema/CheckoutService-v51.yaml \
-t /local/templates/go \
-g go-experimental \
-p packageName=Checkout \
-o /local/src/checkout
or
openapi-generator-cli generate \
-i /schema/CheckoutService-v51.yaml
-t /templates/go \
-g go-experimental \
-p packageName=Checkout \
-o /src/checkout
Step 2: Delete the following files/folders from ./src/<api package folder name in lowercase>. If the foldername is not in lowercase, rename it
configuration.goclient.goutils.goresponse.gotravis.ymlgit_push.shgo.modgo.sumdocs
Step 3: Remove the HTTP method(Post, Get, Put, Patch) suffix on API endpoint methods (Regex to find them ([A-Z][a-zA-Z0-9]*)Post\(request)
Step 4: Add the new service to APIClient struct in ./src/api/api.go and add import for the same
type APIClient struct {
client *common.Client
// API Services
Checkout *checkout.Checkout
<Api namespace in StartCase> *<Api namespace in lowercase>.<Api namespace in StartCase>
}Init service in the NewClient method
func NewClient(cfg *common.Config) *APIClient {
...
// API Services
c.Checkout = &checkout.Checkout{
Client: c.client,
BasePath: func() string {
return fmt.Sprintf("%s/%s", c.client.Cfg.CheckoutEndpoint, CheckoutAPIVersion)
},
}
c.<Api namespace in StartCase> = &<Api namespace in lowercase>.<Api namespace in StartCase>{
Client: c.client,
BasePath: func() string {
return fmt.Sprintf("%s/%s", c.client.Cfg.<API end point>, <API version constant>)
},
}
}Step 5: Run make run or go run main.go and Fix any issues found
Step 6: Add tests for the new APIs created under ./src/<Api namespace in lowercase>
Step 7: Run make test or go test ./... and Fix any issues found