Skip to content

Commit 3db6b52

Browse files
mfatihercikkota65535
authored andcommitted
Generate API files as interfaces for go-gin server (OpenAPITools#17784)
* add feature to generate only interface files * generate sample * add workflow file foe go gin service * add workflow file foe go gin service * add workflow file foe go gin service * update samples
1 parent 88d463c commit 3db6b52

File tree

24 files changed

+1559
-20
lines changed

24 files changed

+1559
-20
lines changed

.github/workflows/samples-go-gin.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Samples Go
2+
3+
on:
4+
push:
5+
paths:
6+
- 'samples/server/petstore/go-gin-api-server/**'
7+
- 'samples/server/petstore/go-gin-api-server-interface-only/**'
8+
pull_request:
9+
paths:
10+
- 'samples/server/petstore/go-gin-api-server/**'
11+
- 'samples/server/petstore/go-gin-api-server-interface-only/**'
12+
13+
jobs:
14+
build:
15+
name: Build Go
16+
runs-on: ubuntu-latest
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
sample:
21+
- samples/server/petstore/go-gin-api-server/
22+
- samples/server/petstore/go-gin-api-server-interface-only/
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: actions/setup-go@v5
26+
with:
27+
go-version: "stable"
28+
- run: go version
29+
- name: Run test
30+
working-directory: ${{ matrix.sample }}
31+
run: go test -mod=mod -v
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
generatorName: go-gin-server
2+
outputDir: samples/server/petstore/go-gin-api-server-interface-only
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/go-gin-server
5+
additionalProperties:
6+
hideGenerationTimestamp: "true"
7+
packageName: petstoreserver
8+
interfaceOnly: true

docs/generators/go-gin-server.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2121
|apiPath|Name of the folder that contains the Go source code| |go|
2222
|enumClassPrefix|Prefix enum with class name| |false|
2323
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
24+
|interfaceOnly|Whether to generate only API interface stubs without the implementation files.| |false|
2425
|packageName|Go package name (convention: lowercase).| |openapi|
2526
|packageVersion|Go package version.| |1.0.0|
2627
|serverPort|The network port the generated server binds to| |8080|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoGinServerCodegen.java

+19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public class GoGinServerCodegen extends AbstractGoCodegen {
3838

3939
private final Logger LOGGER = LoggerFactory.getLogger(GoGinServerCodegen.class);
4040

41+
public static final String INTERFACE_ONLY = "interfaceOnly";
42+
43+
protected boolean interfaceOnly = false;
44+
4145
protected String apiVersion = "1.0.0";
4246
protected int serverPort = 8080;
4347
protected String projectName = "openapi-server";
@@ -114,6 +118,8 @@ public GoGinServerCodegen() {
114118

115119
cliOptions.add(new CliOption("apiPath", "Name of the folder that contains the Go source code")
116120
.defaultValue(apiPath));
121+
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY,
122+
"Whether to generate only API interface stubs without the implementation files.", interfaceOnly));
117123

118124
CliOption optServerPort = new CliOption("serverPort", "The network port the generated server binds to");
119125
optServerPort.setType("int");
@@ -148,6 +154,10 @@ public void processOpts() {
148154
additionalProperties.put(CodegenConstants.PACKAGE_NAME, this.packageName);
149155
}
150156

157+
if (additionalProperties.containsKey(INTERFACE_ONLY)) {
158+
this.setInterfaceOnly(Boolean.parseBoolean(additionalProperties.get(INTERFACE_ONLY).toString()));
159+
}
160+
151161
/*
152162
* Additional Properties. These values can be passed to the templates and
153163
* are available in models, apis, and supporting files
@@ -180,6 +190,11 @@ public void processOpts() {
180190
modelPackage = packageName;
181191
apiPackage = packageName;
182192

193+
if (interfaceOnly) {
194+
apiTemplateFiles.clear();
195+
apiTemplateFiles.put("interface-api.mustache", ".go");
196+
}
197+
183198
/*
184199
* Supporting Files. You can write single files for the generator with the
185200
* entire object tree available. If the input file has a suffix of `.mustache
@@ -194,6 +209,10 @@ public void processOpts() {
194209
supportingFiles.add(new SupportingFile("go.mod.mustache", "go.mod"));
195210
}
196211

212+
public void setInterfaceOnly(boolean interfaceOnly) {
213+
this.interfaceOnly = interfaceOnly;
214+
}
215+
197216
@Override
198217
public String apiPackage() {
199218
return apiPath;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{>partial_header}}
2+
package {{packageName}}
3+
4+
{{#operations}}
5+
import (
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
type {{classname}} interface {
10+
11+
12+
{{#operation}}
13+
// {{nickname}} {{httpMethod}} {{{basePathWithoutHost}}}{{{path}}}{{#summary}}
14+
// {{{.}}} {{/summary}}
15+
{{#isDeprecated}}
16+
// Deprecated
17+
{{/isDeprecated}}
18+
{{nickname}}(c *gin.Context)
19+
20+
{{/operation}}
21+
{{/operations}}
22+
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/goginserver/GoGinServerCodegenTest.java

+32-20
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@
1717

1818
package org.openapitools.codegen.goginserver;
1919

20-
import org.openapitools.codegen.*;
20+
import org.openapitools.codegen.DefaultGenerator;
21+
import org.openapitools.codegen.TestUtils;
2122
import org.openapitools.codegen.config.CodegenConfigurator;
22-
import org.openapitools.codegen.languages.GoClientCodegen;
23-
import org.testng.Assert;
2423
import org.testng.annotations.Test;
2524

2625
import java.io.File;
2726
import java.io.IOException;
2827
import java.nio.file.Files;
29-
import java.nio.file.Path;
3028
import java.nio.file.Paths;
31-
import java.util.HashMap;
3229
import java.util.List;
33-
import java.util.Map;
3430

3531

3632
public class GoGinServerCodegenTest {
@@ -40,13 +36,8 @@ public void verifyGoMod() throws IOException {
4036
File output = Files.createTempDirectory("test").toFile();
4137
output.deleteOnExit();
4238

43-
final CodegenConfigurator configurator = new CodegenConfigurator()
44-
.setGeneratorName("go-gin-server")
45-
.setGitUserId("my-user")
46-
.setGitRepoId("my-repo")
47-
.setPackageName("my-package")
48-
.setInputSpec("src/test/resources/3_0/petstore.yaml")
49-
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
39+
final CodegenConfigurator configurator = createDefaultCodegenConfigurator(output)
40+
.setInputSpec("src/test/resources/3_0/petstore.yaml");
5041

5142
DefaultGenerator generator = new DefaultGenerator();
5243
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
@@ -64,13 +55,8 @@ public void webhooks() throws IOException {
6455
File output = Files.createTempDirectory("test").toFile();
6556
output.deleteOnExit();
6657

67-
final CodegenConfigurator configurator = new CodegenConfigurator()
68-
.setGeneratorName("go-gin-server")
69-
.setGitUserId("my-user")
70-
.setGitRepoId("my-repo")
71-
.setPackageName("my-package")
72-
.setInputSpec("src/test/resources/3_1/webhooks.yaml")
73-
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
58+
final CodegenConfigurator configurator = createDefaultCodegenConfigurator(output)
59+
.setInputSpec("src/test/resources/3_1/webhooks.yaml");
7460

7561
DefaultGenerator generator = new DefaultGenerator();
7662
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
@@ -82,4 +68,30 @@ public void webhooks() throws IOException {
8268
" c.JSON(200, gin.H{\"status\": \"OK\"})");
8369
}
8470

71+
@Test
72+
public void verifyInterfaceOnly() throws IOException {
73+
File output = Files.createTempDirectory("test").toFile();
74+
output.deleteOnExit();
75+
76+
final CodegenConfigurator configurator = createDefaultCodegenConfigurator(output)
77+
.setInputSpec("src/test/resources/3_0/petstore.yaml")
78+
.addAdditionalProperty("interfaceOnly", true);
79+
80+
DefaultGenerator generator = new DefaultGenerator();
81+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
82+
files.forEach(File::deleteOnExit);
83+
84+
TestUtils.assertFileContains(Paths.get(output + "/go/api_pet.go"),
85+
"type PetAPI interface");
86+
}
87+
88+
private static CodegenConfigurator createDefaultCodegenConfigurator(File output) {
89+
return new CodegenConfigurator()
90+
.setGeneratorName("go-gin-server")
91+
.setGitUserId("my-user")
92+
.setGitRepoId("my-repo")
93+
.setPackageName("my-package")
94+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
95+
}
96+
8597
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Dockerfile
2+
api/openapi.yaml
3+
go.mod
4+
go/README.md
5+
go/api_pet.go
6+
go/api_store.go
7+
go/api_user.go
8+
go/model_api_response.go
9+
go/model_category.go
10+
go/model_order.go
11+
go/model_pet.go
12+
go/model_tag.go
13+
go/model_user.go
14+
go/routers.go
15+
main.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.4.0-SNAPSHOT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.19 AS build
2+
WORKDIR /go/src
3+
COPY go ./go
4+
COPY main.go .
5+
COPY go.sum .
6+
COPY go.mod .
7+
8+
ENV CGO_ENABLED=0
9+
10+
RUN go build -o petstoreserver .
11+
12+
FROM scratch AS runtime
13+
ENV GIN_MODE=release
14+
COPY --from=build /go/src/petstoreserver ./
15+
EXPOSE 8080/tcp
16+
ENTRYPOINT ["./petstoreserver"]

0 commit comments

Comments
 (0)