Skip to content

Commit 2fa63cf

Browse files
authored
Merge pull request #1907 from swaggo/v2-merge
V2 merge
2 parents 37834cd + 6106783 commit 2fa63cf

Some content is hidden

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

68 files changed

+4141
-721
lines changed

.github/workflows/ci.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
test:
1111
strategy:
1212
matrix:
13-
go: [ '1.18.x', '1.19.x', '1.20.x' ]
13+
go: [ '1.19.x', '1.20.x', '1.21.x', '1.22.x' ]
1414
platform: [ubuntu-latest, macos-latest]
1515
runs-on: ${{ matrix.platform }}
1616
steps:
@@ -22,8 +22,7 @@ jobs:
2222
- name: deps
2323
run: make deps
2424
- name: static program analysis
25-
run: |
26-
make fmt-check lint vet
25+
run: make fmt-check vet
2726
- name: build
2827
run: make build
2928
- name: test

.github/workflows/docker.yml

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,44 @@ on:
55
tags:
66
- 'v*'
77

8+
permissions:
9+
contents: read
10+
packages: write
11+
812
jobs:
913
docker-build:
1014
runs-on: ubuntu-latest
1115
steps:
1216
- name: Checkout code
13-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1418

1519
- name: Set up Docker Buildx
1620
id: buildx
17-
uses: docker/setup-buildx-action@v2
21+
uses: docker/setup-buildx-action@v3
1822

1923
- name: Login to Github Packages
20-
uses: docker/login-action@v2
24+
uses: docker/login-action@v3
2125
with:
2226
registry: ghcr.io
2327
username: ${{ github.actor }}
2428
password: ${{ secrets.GITHUB_TOKEN }}
2529

2630
- name: Docker meta
2731
id: meta
28-
uses: docker/metadata-action@v4
32+
uses: docker/metadata-action@v5
2933
with:
3034
images: ghcr.io/swaggo/swag
3135

3236
- name: Build image and push to GitHub Container Registry
33-
uses: docker/build-push-action@v2
37+
id: docker_build
38+
uses: docker/build-push-action@v5
3439
with:
3540
context: .
41+
platforms: linux/amd64,linux/arm64
3642
push: true
3743
tags: |
38-
ghcr.io/swaggo/swag:latest
39-
ghcr.io/swaggo/swag:${{github.ref_name}}
44+
ghcr.io/${{ github.repository }}:latest
45+
ghcr.io/${{ github.repository }}:${{github.ref_name}}
4046
labels: ${{ steps.meta.outputs.labels }}
4147

4248
- name: Image digest

Dockerfile

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Dockerfile References: https://docs.docker.com/engine/reference/builder/
22

33
# Start from the latest golang base image
4-
FROM golang:1.18.3-alpine as builder
4+
FROM --platform=$BUILDPLATFORM golang:1.21-alpine as builder
55

66
# Set the Current Working Directory inside the container
77
WORKDIR /app
@@ -15,14 +15,22 @@ RUN go mod download
1515
# Copy the source from the current directory to the Working Directory inside the container
1616
COPY . .
1717

18+
# Configure go compiler target platform
19+
ARG TARGETOS
20+
ARG TARGETARCH
21+
ENV GOARCH=$TARGETARCH \
22+
GOOS=$TARGETOS
23+
1824
# Build the Go app
1925
RUN CGO_ENABLED=0 GOOS=linux go build -v -a -installsuffix cgo -o swag cmd/swag/main.go
2026

2127

2228
######## Start a new stage from scratch #######
23-
FROM scratch
29+
FROM --platform=$TARGETPLATFORM scratch
2430

25-
WORKDIR /root/
31+
WORKDIR /code/
2632

2733
# Copy the Pre-built binary file from the previous stage
28-
COPY --from=builder /app/swag .
34+
COPY --from=builder /app/swag /bin/swag
35+
36+
ENTRYPOINT ["/bin/swag"]

Makefile

+3-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ GOBUILD:=$(GOCMD) build
66
GOINSTALL:=$(GOCMD) install
77
GOCLEAN:=$(GOCMD) clean
88
GOTEST:=$(GOCMD) test
9+
GOMODTIDY:=$(GOCMD) mod tidy
910
GOGET:=$(GOCMD) get
1011
GOLIST:=$(GOCMD) list
1112
GOVET:=$(GOCMD) vet
@@ -16,8 +17,6 @@ BINARY_NAME:=swag
1617
PACKAGES:=$(shell $(GOLIST) github.com/swaggo/swag/v2 github.com/swaggo/swag/v2/cmd/swag github.com/swaggo/swag/v2/gen github.com/swaggo/swag/v2/format)
1718
GOFILES:=$(shell find . -name "*.go" -type f)
1819

19-
export GO111MODULE := on
20-
2120
all: test build
2221

2322
.PHONY: build
@@ -54,25 +53,10 @@ clean:
5453

5554
.PHONY: deps
5655
deps:
57-
$(GOGET) github.com/swaggo/cli
58-
$(GOGET) sigs.k8s.io/yaml
59-
$(GOGET) github.com/KyleBanks/depth
60-
$(GOGET) github.com/go-openapi/jsonreference
61-
$(GOGET) github.com/go-openapi/spec
62-
$(GOGET) github.com/stretchr/testify/assert
63-
$(GOGET) golang.org/x/tools/go/loader
64-
65-
.PHONY: devel-deps
66-
devel-deps:
67-
GO111MODULE=off $(GOGET) -v -u \
68-
golang.org/x/lint/golint
69-
70-
.PHONY: lint
71-
lint: devel-deps
72-
for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
56+
$(GOMODTIDY)
7357

7458
.PHONY: vet
75-
vet: deps devel-deps
59+
vet: deps
7660
$(GOVET) $(PACKAGES)
7761

7862
.PHONY: fmt

README.md

+70-23
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie
4242
- [User defined structure with an array type](#user-defined-structure-with-an-array-type)
4343
- [Function scoped struct declaration](#function-scoped-struct-declaration)
4444
- [Model composition in response](#model-composition-in-response)
45-
- [Add a headers in response](#add-a-headers-in-response)
45+
- [Add headers in request](#add-request-headers)
46+
- [Add headers in response](#add-a-headers-in-response)
4647
- [Use multiple path params](#use-multiple-path-params)
4748
- [Add multiple paths](#add-multiple-paths)
4849
- [Example value of struct](#example-value-of-struct)
@@ -56,6 +57,7 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie
5657
- [How to use security annotations](#how-to-use-security-annotations)
5758
- [Add a description for enum items](#add-a-description-for-enum-items)
5859
- [Generate only specific docs file types](#generate-only-specific-docs-file-types)
60+
- [How to use Go generic types](#how-to-use-generics)
5961
- [Change the default Go Template action delimiters](#change-the-default-go-template-action-delimiters)
6062
- [About the Project](#about-the-project)
6163
- [Contributors](#contributors)
@@ -67,11 +69,16 @@ Swag converts Go annotations to Swagger Documentation 2.0. We've created a varie
6769

6870
1. Add comments to your API source code, See [Declarative Comments Format](#declarative-comments-format).
6971

70-
2. Download swag by using:
72+
2. Install swag by using:
7173
```sh
7274
go install github.com/swaggo/swag/v2/cmd/swag@latest
7375
```
74-
To build from source you need [Go](https://golang.org/dl/) (1.18 or newer).
76+
To build from source you need [Go](https://golang.org/dl/) (1.19 or newer).
77+
78+
Alternatively you can run the docker image:
79+
```sh
80+
docker run --rm -v $(pwd):/code ghcr.io/swaggo/swag:latest
81+
```
7582

7683
Or download a pre-compiled binary from the [release page](https://github.com/swaggo/swag/releases).
7784

@@ -81,6 +88,9 @@ swag init
8188
```
8289

8390
Make sure to import the generated `docs/docs.go` so that your specific configuration gets `init`'ed. If your General API annotations do not live in `main.go`, you can let swag know with `-g` flag.
91+
```go
92+
import _ "example-module-name/docs"
93+
```
8494
```sh
8595
swag init -g http/api.go
8696
```
@@ -112,6 +122,7 @@ OPTIONS:
112122
--outputTypes value, --ot value Output types of generated files (docs.go, swagger.json, swagger.yaml) like go,json,yaml (default: "go,json,yaml")
113123
--parseVendor Parse go files in 'vendor' folder, disabled by default (default: false)
114124
--parseDependency, --pd Parse go files inside dependency folder, disabled by default (default: false)
125+
--parseDependencyLevel, --pdl Enhancement of '--parseDependency', parse go files inside dependency folder, 0 disabled, 1 only parse models, 2 only parse operations, 3 parse all (default: 0)
115126
--markdownFiles value, --md value Parse folder containing markdown files to use as description, disabled by default
116127
--codeExampleFiles value, --cef value Parse folder containing code example files to use for the x-codeSamples extension, disabled by default
117128
--parseInternal Parse go files in internal packages, disabled by default (default: false)
@@ -125,6 +136,9 @@ OPTIONS:
125136
--tags value, -t value A comma-separated list of tags to filter the APIs for which the documentation is generated.Special case if the tag is prefixed with the '!' character then the APIs with that tag will be excluded
126137
--v3.1 Generate OpenAPI V3.1 spec (default: false)
127138
--templateDelims value, --td value Provide custom delimeters for Go template generation. The format is leftDelim,rightDelim. For example: "[[,]]"
139+
--collectionFormat value, --cf value Set default collection format (default: "csv")
140+
--state value Initial state for the state machine (default: ""), @HostState in root file, @State in other files
141+
--parseFuncBody Parse API info within body of functions in go files, disabled by default (default: false)
128142
--packageName --output A package name of docs.go, using output directory name by default (check --output option)
129143
--collectionFormat value, --cf value Set default collection format (default: "csv")
130144
--help, -h show help
@@ -163,6 +177,7 @@ OPTIONS:
163177
164178
Find the example source code [here](https://github.com/swaggo/swag/tree/master/example/celler).
165179
180+
Finish the steps in [Getting started](#getting-started)
166181
1. After using `swag init` to generate Swagger 2.0 docs, import the following packages:
167182
```go
168183
import "github.com/swaggo/gin-swagger" // gin-swagger middleware
@@ -443,25 +458,26 @@ The following annotations are only available if you set the -v3.1 flag in the CL
443458
[celler/controller](https://github.com/swaggo/swag/tree/master/example/celler/controller)
444459
445460
446-
| annotation | description |
447-
|-------------|----------------------------------------------------------------------------------------------------------------------------|
448-
| description | A verbose explanation of the operation behavior. |
449-
| description.markdown | A short description of the application. The description will be read from a file. E.g. `@description.markdown details` will load `details.md`| // @description.file endpoint.description.markdown |
450-
| id | A unique string used to identify the operation. Must be unique among all API operations. |
451-
| tags | A list of tags to each API operation that separated by commas. |
452-
| summary | A short summary of what the operation does. |
453-
| accept | A list of MIME types the APIs can consume. Note that Accept only affects operations with a request body, such as POST, PUT and PATCH. Value MUST be as described under [Mime Types](#mime-types). |
454-
| produce | A list of MIME types the APIs can produce. Value MUST be as described under [Mime Types](#mime-types). |
455-
| param | Parameters that separated by spaces. `param name`,`param type`,`data type`,`is mandatory?`,`comment` `attribute(optional)` |
456-
| security | [Security](#security) to each API operation. |
457-
| success | Success response that separated by spaces. `return code or default`,`{param type}`,`data type`,`comment` |
458-
| failure | Failure response that separated by spaces. `return code or default`,`{param type}`,`data type`,`comment` |
459-
| response | As same as `success` and `failure` |
460-
| header | Header in response that separated by spaces. `return code`,`{param type}`,`data type`,`comment` |
461-
| router | Path definition that separated by spaces. `path`,`[httpMethod]` |
462-
| x-name | The extension key, must be start by x- and take only json value. |
463-
| x-codeSample | Optional Markdown usage. take `file` as parameter. This will then search for a file named like the summary in the given folder. |
464-
| deprecated | Mark endpoint as deprecated. |
461+
| annotation | description |
462+
|----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
463+
| description | A verbose explanation of the operation behavior. |
464+
| description.markdown | A short description of the application. The description will be read from a file. E.g. `@description.markdown details` will load `details.md` | // @description.file endpoint.description.markdown |
465+
| id | A unique string used to identify the operation. Must be unique among all API operations. |
466+
| tags | A list of tags to each API operation that separated by commas. |
467+
| summary | A short summary of what the operation does. |
468+
| accept | A list of MIME types the APIs can consume. Note that Accept only affects operations with a request body, such as POST, PUT and PATCH. Value MUST be as described under [Mime Types](#mime-types). |
469+
| produce | A list of MIME types the APIs can produce. Value MUST be as described under [Mime Types](#mime-types). |
470+
| param | Parameters that separated by spaces. `param name`,`param type`,`data type`,`is mandatory?`,`comment` `attribute(optional)` |
471+
| security | [Security](#security) to each API operation. |
472+
| success | Success response that separated by spaces. `return code or default`,`{param type}`,`data type`,`comment` |
473+
| failure | Failure response that separated by spaces. `return code or default`,`{param type}`,`data type`,`comment` |
474+
| response | As same as `success` and `failure` |
475+
| header | Header in response that separated by spaces. `return code`,`{param type}`,`data type`,`comment` |
476+
| router | Path definition that separated by spaces. `path`,`[httpMethod]` |
477+
| deprecatedrouter | As same as router, but deprecated. |
478+
| x-name | The extension key, must be start by x- and take only json value. |
479+
| x-codeSample | Optional Markdown usage. take `file` as parameter. This will then search for a file named like the summary in the given folder. |
480+
| deprecated | Mark endpoint as deprecated. |
465481
466482
467483
@@ -663,7 +679,14 @@ type DeepObject struct { //in `proto` package
663679
}
664680
@success 200 {object} jsonresult.JSONResult{data1=proto.Order{data=proto.DeepObject},data2=[]proto.Order{data=[]proto.DeepObject}} "desc"
665681
```
666-
### Add a headers in response
682+
### Add response request
683+
684+
```go
685+
// @Param X-MyHeader header string true "MyHeader must be set for valid response"
686+
// @Param X-API-VERSION header string true "API version eg.: 1.0"
687+
```
688+
689+
### Add response headers
667690
668691
```go
669692
// @Success 200 {string} string "ok"
@@ -939,6 +962,19 @@ By default `swag` command generates Swagger specification in three different fil
939962
940963
If you would like to limit a set of file types which should be generated you can use `--outputTypes` (short `-ot`) flag. Default value is `go,json,yaml` - output types separated with comma. To limit output only to `go` and `yaml` files, you would write `go,yaml`. With complete command that would be `swag init --outputTypes go,yaml`.
941964
965+
### How to use Generics
966+
967+
```go
968+
// @Success 200 {object} web.GenericNestedResponse[types.Post]
969+
// @Success 204 {object} web.GenericNestedResponse[types.Post, Types.AnotherOne]
970+
// @Success 201 {object} web.GenericNestedResponse[web.GenericInnerType[types.Post]]
971+
func GetPosts(w http.ResponseWriter, r *http.Request) {
972+
_ = web.GenericNestedResponse[types.Post]{}
973+
}
974+
```
975+
See [this file](https://github.com/swaggo/swag/blob/master/testdata/generics_nested/api/api.go) for more details
976+
and other examples.
977+
942978
### Change the default Go Template action delimiters
943979
[#980](https://github.com/swaggo/swag/issues/980)
944980
[#1177](https://github.com/swaggo/swag/issues/1177)
@@ -951,6 +987,17 @@ swag init -g http/api.go -td "[[,]]"
951987
```
952988
The new delimiter is a string with the format "`<left delimiter>`,`<right delimiter>`".
953989
990+
### Parse Internal and Dependency Packages
991+
992+
If the struct is defined in a dependency package, use `--parseDependency`.
993+
994+
If the struct is defined in your main project, use `--parseInternal`.
995+
996+
if you want to include both internal and from dependencies use both flags
997+
```
998+
swag init --parseDependency --parseInternal
999+
```
1000+
9541001
## About the Project
9551002
This project was inspired by [yvasiyarov/swagger](https://github.com/yvasiyarov/swagger) but we simplified the usage and added support a variety of [web frameworks](#supported-web-frameworks). Gopher image source is [tenntenn/gopher-stickers](https://github.com/tenntenn/gopher-stickers). It has licenses [creative commons licensing](http://creativecommons.org/licenses/by/3.0/deed.en).
9561003
## Contributors

0 commit comments

Comments
 (0)