Skip to content

Commit 4adb7ca

Browse files
Improve swagger generation (go-gitea#33664)
Remove most "sed" tricks --------- Co-authored-by: silverwind <[email protected]>
1 parent f2fbb89 commit 4adb7ca

File tree

6 files changed

+17
-12
lines changed

6 files changed

+17
-12
lines changed

.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ insert_final_newline = false
1717

1818
[templates/swagger/v1_json.tmpl]
1919
indent_style = space
20+
insert_final_newline = false
2021

2122
[templates/user/auth/oidc_wellknown.tmpl]
2223
indent_style = space

.github/workflows/files-changed.yml

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ jobs:
8585
8686
swagger:
8787
- "templates/swagger/v1_json.tmpl"
88+
- "templates/swagger/v1_input.json"
8889
- "Makefile"
8990
- "package.json"
9091
- "package-lock.json"

Makefile

+8-9
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,8 @@ ifdef DEPS_PLAYWRIGHT
165165
endif
166166

167167
SWAGGER_SPEC := templates/swagger/v1_json.tmpl
168-
SWAGGER_SPEC_S_TMPL := s|"basePath": *"/api/v1"|"basePath": "{{AppSubUrl \| JSEscape}}/api/v1"|g
169-
SWAGGER_SPEC_S_JSON := s|"basePath": *"{{AppSubUrl \| JSEscape}}/api/v1"|"basePath": "/api/v1"|g
168+
SWAGGER_SPEC_INPUT := templates/swagger/v1_input.json
170169
SWAGGER_EXCLUDE := code.gitea.io/sdk
171-
SWAGGER_NEWLINE_COMMAND := -e '$$a\'
172170

173171
TEST_MYSQL_HOST ?= mysql:3306
174172
TEST_MYSQL_DBNAME ?= testgitea
@@ -271,10 +269,8 @@ endif
271269
.PHONY: generate-swagger
272270
generate-swagger: $(SWAGGER_SPEC) ## generate the swagger spec from code comments
273271

274-
$(SWAGGER_SPEC): $(GO_SOURCES_NO_BINDATA)
275-
$(GO) run $(SWAGGER_PACKAGE) generate spec -x "$(SWAGGER_EXCLUDE)" -o './$(SWAGGER_SPEC)'
276-
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
277-
$(SED_INPLACE) $(SWAGGER_NEWLINE_COMMAND) './$(SWAGGER_SPEC)'
272+
$(SWAGGER_SPEC): $(GO_SOURCES_NO_BINDATA) $(SWAGGER_SPEC_INPUT)
273+
$(GO) run $(SWAGGER_PACKAGE) generate spec --exclude "$(SWAGGER_EXCLUDE)" --input "$(SWAGGER_SPEC_INPUT)" --output './$(SWAGGER_SPEC)'
278274

279275
.PHONY: swagger-check
280276
swagger-check: generate-swagger
@@ -287,9 +283,11 @@ swagger-check: generate-swagger
287283

288284
.PHONY: swagger-validate
289285
swagger-validate: ## check if the swagger spec is valid
290-
$(SED_INPLACE) '$(SWAGGER_SPEC_S_JSON)' './$(SWAGGER_SPEC)'
286+
@# swagger "validate" requires that the "basePath" must start with a slash, but we are using Golang template "{{...}}"
287+
@$(SED_INPLACE) -E -e 's|"basePath":( *)"(.*)"|"basePath":\1"/\2"|g' './$(SWAGGER_SPEC)' # add a prefix slash to basePath
288+
@# FIXME: there are some warnings
291289
$(GO) run $(SWAGGER_PACKAGE) validate './$(SWAGGER_SPEC)'
292-
$(SED_INPLACE) '$(SWAGGER_SPEC_S_TMPL)' './$(SWAGGER_SPEC)'
290+
@$(SED_INPLACE) -E -e 's|"basePath":( *)"/(.*)"|"basePath":\1"\2"|g' './$(SWAGGER_SPEC)' # remove the prefix slash from basePath
293291

294292
.PHONY: checks
295293
checks: checks-frontend checks-backend ## run various consistency checks
@@ -380,6 +378,7 @@ lint-go-gopls: ## lint go files with gopls
380378

381379
.PHONY: lint-editorconfig
382380
lint-editorconfig:
381+
@echo "Running editorconfig check..."
383382
@$(GO) run $(EDITORCONFIG_CHECKER_PACKAGE) $(EDITORCONFIG_FILES)
384383

385384
.PHONY: lint-actions

routers/api/v1/api.go

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// This documentation describes the Gitea API.
88
//
99
// Schemes: https, http
10-
// BasePath: /api/v1
11-
// Version: {{AppVer | JSEscape}}
1210
// License: MIT http://opensource.org/licenses/MIT
1311
//
1412
// Consumes:

templates/swagger/v1_input.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info": {
3+
"version": "{{AppVer | JSEscape}}"
4+
},
5+
"basePath": "{{AppSubUrl | JSEscape}}/api/v1"
6+
}

templates/swagger/v1_json.tmpl

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)