Skip to content

Commit 0b79c5b

Browse files
authored
Add new Go client (experimental) generator (#3268)
* add new go experimental generator * extends go client codegen * add go samples and test * comment out haskell client test
1 parent 65fdce7 commit 0b79c5b

147 files changed

Lines changed: 11377 additions & 3 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/go-experimental-petstore.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
SPEC="modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml"
29+
GENERATOR="go-experimental"
30+
STUB_DIR="samples/client/petstore/go-experimental/go-petstore"
31+
32+
echo "Removing files and folders under $STUB_DIR"
33+
rm -rf $STUB_DIR
34+
35+
# if you've executed sbt assembly previously it will use that instead.
36+
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
37+
ags="generate -t modules/openapi-generator/src/main/resources/$GENERATOR -i $SPEC -g $GENERATOR -o $STUB_DIR -DpackageName=petstore $@"
38+
39+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
SPEC="modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml"
29+
GENERATOR="go-experimental"
30+
STUB_DIR="samples/openapi3/client/petstore/go-experimental/go-petstore"
31+
32+
echo "Removing files and folders under $STUB_DIR"
33+
rm -rf $STUB_DIR
34+
35+
# if you've executed sbt assembly previously it will use that instead.
36+
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
37+
ags="generate -t modules/openapi-generator/src/main/resources/$GENERATOR -i $SPEC -g $GENERATOR -o $STUB_DIR -DpackageName=petstore $@"
38+
39+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
setlocal
2+
3+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
4+
5+
If Not Exist %executable% (
6+
mvn clean package
7+
)
8+
9+
set SPEC=modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml
10+
set GENERATOR=go-experimental
11+
set STUB_DIR=samples\client\petstore\go-experimental\go-petstore
12+
13+
echo Removing files and folders under %STUB_DIR%
14+
del /F /S /Q %STUB_DIR%
15+
16+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
17+
set ags=generate -t modules\openapi-generator\src\main\resources\%GENERATOR% -i %SPEC% -g %GENERATOR% -o %STUB_DIR% -DpackageName=petstore
18+
19+
java %JAVA_OPTS% -jar %executable% %ags%
20+
21+
endlocal

docs/generators.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The following generators are available:
2727
- [erlang-proper](generators/erlang-proper.md)
2828
- [flash](generators/flash.md)
2929
- [go](generators/go.md)
30+
- [go-experimental](generators/go-experimental.md)
3031
- [groovy](generators/groovy.md)
3132
- [haskell-http-client](generators/haskell-http-client.md)
3233
- [java](generators/java.md)

docs/generators/go-experimental.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
---
3+
id: generator-opts-client-go-experimental
4+
title: Config Options for go-experimental
5+
sidebar_label: go-experimental
6+
---
7+
8+
| Option | Description | Values | Default |
9+
| ------ | ----------- | ------ | ------- |
10+
|packageName|Go package name (convention: lowercase).| |openapi|
11+
|packageVersion|Go package version.| |1.0.0|
12+
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
13+
|isGoSubmodule|whether the generated Go module is a submodule| |false|
14+
|withGoCodegenComment|whether to include Go codegen comment to disable Go Lint and collapse by default GitHub in PRs and diffs| |false|
15+
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
16+
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.openapitools.codegen.languages;
18+
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
public class GoClientExperimentalCodegen extends GoClientCodegen {
23+
24+
private static final Logger LOGGER = LoggerFactory.getLogger(GoClientExperimentalCodegen.class);
25+
26+
public GoClientExperimentalCodegen() {
27+
super();
28+
}
29+
30+
/**
31+
* Configures a friendly name for the generator. This will be used by the generator
32+
* to select the library with the -g flag.
33+
*
34+
* @return the friendly name for the generator
35+
*/
36+
@Override
37+
public String getName() {
38+
return "go-experimental";
39+
}
40+
41+
/**
42+
* Returns human-friendly help for the generator. Provide the consumer with help
43+
* tips, parameters here
44+
*
45+
* @return A string value for the help message
46+
*/
47+
@Override
48+
public String getHelp() {
49+
return "Generates a Go client library (experimental and may subject to breaking changes without further notice).";
50+
}
51+
52+
}

modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ org.openapitools.codegen.languages.ErlangProperCodegen
2828
org.openapitools.codegen.languages.ErlangServerCodegen
2929
org.openapitools.codegen.languages.FlashClientCodegen
3030
org.openapitools.codegen.languages.GoClientCodegen
31+
org.openapitools.codegen.languages.GoClientExperimentalCodegen
3132
org.openapitools.codegen.languages.GoServerCodegen
3233
org.openapitools.codegen.languages.GoGinServerCodegen
3334
org.openapitools.codegen.languages.GraphQLSchemaCodegen
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: go
2+
3+
install:
4+
- go get -d -v .
5+
6+
script:
7+
- go build -v ./
8+
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Go API client for {{packageName}}
2+
3+
{{#appDescription}}
4+
{{{appDescription}}}
5+
{{/appDescription}}
6+
7+
## Overview
8+
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [OpenAPI-spec](https://www.openapis.org/) from a remote server, you can easily generate an API client.
9+
10+
- API version: {{appVersion}}
11+
- Package version: {{packageVersion}}
12+
{{^hideGenerationTimestamp}}
13+
- Build date: {{generatedDate}}
14+
{{/hideGenerationTimestamp}}
15+
- Build package: {{generatorClass}}
16+
{{#infoUrl}}
17+
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
18+
{{/infoUrl}}
19+
20+
## Installation
21+
22+
Install the following dependencies:
23+
24+
```shell
25+
go get github.com/stretchr/testify/assert
26+
go get golang.org/x/oauth2
27+
go get golang.org/x/net/context
28+
go get github.com/antihax/optional
29+
```
30+
31+
Put the package under your project folder and add the following in import:
32+
33+
```golang
34+
import "./{{packageName}}"
35+
```
36+
37+
## Documentation for API Endpoints
38+
39+
All URIs are relative to *{{basePath}}*
40+
41+
Class | Method | HTTP request | Description
42+
------------ | ------------- | ------------- | -------------
43+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}*{{classname}}* | [**{{operationId}}**]({{apiDocPath}}{{classname}}.md#{{operationIdLowerCase}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
44+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
45+
46+
## Documentation For Models
47+
48+
{{#models}}{{#model}} - [{{{classname}}}]({{modelDocPath}}{{{classname}}}.md)
49+
{{/model}}{{/models}}
50+
51+
## Documentation For Authorization
52+
53+
{{^authMethods}} Endpoints do not require authorization.
54+
{{/authMethods}}{{#authMethods}}{{#last}} Authentication schemes defined for the API:{{/last}}{{/authMethods}}
55+
{{#authMethods}}
56+
57+
## {{{name}}}
58+
59+
{{#isApiKey}}- **Type**: API key
60+
61+
Example
62+
63+
```golang
64+
auth := context.WithValue(context.Background(), sw.ContextAPIKey, sw.APIKey{
65+
Key: "APIKEY",
66+
Prefix: "Bearer", // Omit if not necessary.
67+
})
68+
r, err := client.Service.Operation(auth, args)
69+
```
70+
71+
{{/isApiKey}}
72+
{{#isBasic}}- **Type**: HTTP basic authentication
73+
74+
Example
75+
76+
```golang
77+
auth := context.WithValue(context.Background(), sw.ContextBasicAuth, sw.BasicAuth{
78+
UserName: "username",
79+
Password: "password",
80+
})
81+
r, err := client.Service.Operation(auth, args)
82+
```
83+
84+
{{/isBasic}}
85+
{{#isOAuth}}
86+
87+
- **Type**: OAuth
88+
- **Flow**: {{{flow}}}
89+
- **Authorization URL**: {{{authorizationUrl}}}
90+
- **Scopes**: {{^scopes}}N/A{{/scopes}}
91+
{{#scopes}} - **{{{scope}}}**: {{{description}}}
92+
{{/scopes}}
93+
94+
Example
95+
96+
```golang
97+
auth := context.WithValue(context.Background(), sw.ContextAccessToken, "ACCESSTOKENSTRING")
98+
r, err := client.Service.Operation(auth, args)
99+
```
100+
101+
Or via OAuth2 module to automatically refresh tokens and perform user authentication.
102+
103+
```golang
104+
import "golang.org/x/oauth2"
105+
106+
/* Perform OAuth2 round trip request and obtain a token */
107+
108+
tokenSource := oauth2cfg.TokenSource(createContext(httpClient), &token)
109+
auth := context.WithValue(oauth2.NoContext, sw.ContextOAuth2, tokenSource)
110+
r, err := client.Service.Operation(auth, args)
111+
```
112+
113+
{{/isOAuth}}
114+
{{/authMethods}}
115+
116+
## Author
117+
118+
{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
119+
{{/hasMore}}{{/apis}}{{/apiInfo}}

0 commit comments

Comments
 (0)