Skip to content

Commit 10897ca

Browse files
authored
[go-server] fix imports with go generation (#18514)
* fix imports with go generation * Wrong copy over * Missing new line * tab vs space * Fix new line between router and std go libs * Add both use case, add samples to CI validation * Update samples
1 parent afd3a78 commit 10897ca

29 files changed

Lines changed: 1480 additions & 2 deletions

File tree

.github/workflows/samples-go.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
name: Samples Go
1+
name: Samples Go
22

33
on:
44
push:
55
paths:
66
- 'samples/server/petstore/go-echo-server/**'
77
- 'samples/server/petstore/go-api-server/**'
88
- 'samples/server/petstore/go-chi-server/**'
9+
- 'samples/server/others/go-server/no-body-path-params/**'
910
pull_request:
1011
paths:
1112
- 'samples/server/petstore/go-echo-server/**'
1213
- 'samples/server/petstore/go-api-server/**'
1314
- 'samples/server/petstore/go-chi-server/**'
15+
- 'samples/server/others/go-server/no-body-path-params/**'
1416

1517
jobs:
1618
build:
17-
name: Build Go
19+
name: Build Go
1820
runs-on: ubuntu-latest
1921
strategy:
2022
fail-fast: false
@@ -23,6 +25,7 @@ jobs:
2325
- samples/server/petstore/go-echo-server/
2426
- samples/server/petstore/go-api-server/
2527
- samples/server/petstore/go-chi-server/
28+
- samples/server/others/go-server/no-body-path-params/
2629
steps:
2730
- uses: actions/checkout@v4
2831
- uses: actions/setup-go@v5
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
generatorName: go-server
2+
outputDir: samples/server/others/go-server/no-body-path-params
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/go-server/no-body-path-params.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/go-server
5+
additionalProperties:
6+
hideGenerationTimestamp: "true"
7+
packageName: petstoreserver
8+
addResponseHeaders: true

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,29 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
398398
}
399399
}
400400

401+
this.addConditionalImportInformation(objs);
402+
401403
return objs;
402404
}
403405

406+
private void addConditionalImportInformation(OperationsMap operations) {
407+
boolean hasPathParams = false;
408+
boolean hasBodyParams = false;
409+
410+
for (CodegenOperation op : operations.getOperations().getOperation()) {
411+
if (op.getHasPathParams()) {
412+
hasPathParams = true;
413+
}
414+
if (op.getHasBodyParam()) {
415+
hasBodyParams = true;
416+
}
417+
}
418+
419+
additionalProperties.put("hasPathParams", hasPathParams);
420+
additionalProperties.put("hasBodyParams", hasBodyParams);
421+
}
422+
423+
404424
@Override
405425
public String apiPackage() {
406426
return sourceFolder;

modules/openapi-generator/src/main/resources/go-server/controller-api.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package {{packageName}}
33

44
{{#operations}}
55
import (
6+
{{#hasBodyParams}}
67
"encoding/json"
8+
{{/hasBodyParams}}
79
{{#isBodyParam}}
810
{{^required}}
911
"errors"
@@ -14,6 +16,7 @@ import (
1416
"strings"
1517
{{#imports}} "{{import}}"
1618
{{/imports}}
19+
{{#hasPathParams}}
1720

1821
{{#routers}}
1922
{{#mux}}
@@ -23,6 +26,7 @@ import (
2326
"github.com/go-chi/chi/v5"
2427
{{/chi}}
2528
{{/routers}}
29+
{{/hasPathParams}}
2630
)
2731
{{/operations}}
2832

modules/openapi-generator/src/main/resources/go-server/go.mod.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module {{gitHost}}/{{gitUserId}}/{{gitRepoId}}
22

33
go 1.18
44

5+
{{#hasPathParams}}
56
{{#routers}}
67
{{#mux}}
78
require github.com/gorilla/mux v1.8.0
@@ -16,3 +17,4 @@ require github.com/go-chi/cors v1.2.1
1617
{{/featureCORS}}
1718
{{/chi}}
1819
{{/routers}}
20+
{{/hasPathParams}}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
openapi: 3.0.0
2+
3+
info:
4+
version: 1.0.0
5+
title: Simple no path and body param spec
6+
7+
paths:
8+
/none/endpoint:
9+
get:
10+
tags:
11+
- none
12+
summary: summary
13+
description: description
14+
operationId: one
15+
responses:
16+
'204':
17+
description: successful operation
18+
/path/endpoint/{pathParam}:
19+
get:
20+
tags:
21+
- path
22+
summary: summary
23+
description: description
24+
operationId: path
25+
parameters:
26+
- name: pathParam
27+
in: path
28+
required: true
29+
style: form
30+
explode: false
31+
schema:
32+
type: string
33+
responses:
34+
'204':
35+
description: successful operation
36+
37+
/body/endpoint:
38+
post:
39+
tags:
40+
- body
41+
summary: summary
42+
description: description
43+
operationId: body
44+
requestBody:
45+
description: Optional description in *Markdown*
46+
required: true
47+
content:
48+
application/json:
49+
schema:
50+
type: object
51+
properties:
52+
param:
53+
type: string
54+
text/plain:
55+
schema:
56+
type: string
57+
responses:
58+
'204':
59+
description: successful operation
60+
61+
/both/endpoint/{pathParam}:
62+
post:
63+
tags:
64+
- both
65+
summary: summary
66+
description: description
67+
operationId: both
68+
parameters:
69+
- name: pathParam
70+
in: path
71+
required: true
72+
style: form
73+
explode: false
74+
schema:
75+
type: string
76+
requestBody:
77+
description: Optional description in *Markdown*
78+
required: true
79+
content:
80+
application/json:
81+
schema:
82+
type: object
83+
properties:
84+
param:
85+
type: string
86+
text/plain:
87+
schema:
88+
type: string
89+
responses:
90+
'204':
91+
description: successful operation
Lines changed: 23 additions & 0 deletions
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
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Dockerfile
2+
README.md
3+
api/openapi.yaml
4+
go.mod
5+
go/api.go
6+
go/api_body.go
7+
go/api_body_service.go
8+
go/api_both.go
9+
go/api_both_service.go
10+
go/api_none.go
11+
go/api_none_service.go
12+
go/api_path.go
13+
go/api_path_service.go
14+
go/error.go
15+
go/helpers.go
16+
go/impl.go
17+
go/logger.go
18+
go/model_body_request.go
19+
go/routers.go
20+
main.go
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.6.0-SNAPSHOT
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
COPY --from=build /go/src/petstoreserver ./
14+
EXPOSE 8080/tcp
15+
ENTRYPOINT ["./petstoreserver"]

0 commit comments

Comments
 (0)