-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Serve OpenAPI 3.0 spec at /openapi.v1.json #37038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3521699
9face95
d47935a
a08c425
320c8e8
e03a8d4
46bc47d
fd2baba
c59294d
fb11744
9082a57
bcf35ef
484c29f
29b8fd3
83a048d
c549c77
ecc5d66
6961542
b815430
c320a74
4dfb578
b766a69
07e326a
6e94d86
0529956
384c6dc
8af52d4
1427a91
3fa0484
ada251a
d79b237
d6c37a5
c4213e2
5f46aa4
5b4d949
20899c8
8fdb180
ff61113
c9d6500
1343799
a4cb68d
82808e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,6 +151,7 @@ ESLINT_CONCURRENCY ?= 2 | |
| SWAGGER_SPEC := templates/swagger/v1_json.tmpl | ||
| SWAGGER_SPEC_INPUT := templates/swagger/v1_input.json | ||
| SWAGGER_EXCLUDE := code.gitea.io/sdk | ||
| OPENAPI3_SPEC := templates/swagger/v1_openapi3_json.tmpl | ||
|
|
||
| TEST_MYSQL_HOST ?= mysql:3306 | ||
| TEST_MYSQL_DBNAME ?= testgitea | ||
|
|
@@ -233,7 +234,7 @@ TAGS_PREREQ := $(TAGS_EVIDENCE) | |
| endif | ||
|
|
||
| .PHONY: generate-swagger | ||
| generate-swagger: $(SWAGGER_SPEC) ## generate the swagger spec from code comments | ||
| generate-swagger: $(SWAGGER_SPEC) $(OPENAPI3_SPEC) ## generate the swagger spec from code comments | ||
|
|
||
| $(SWAGGER_SPEC): $(GO_SOURCES) $(SWAGGER_SPEC_INPUT) | ||
| $(GO) run $(SWAGGER_PACKAGE) generate spec --exclude "$(SWAGGER_EXCLUDE)" --input "$(SWAGGER_SPEC_INPUT)" --output './$(SWAGGER_SPEC)' | ||
|
|
@@ -255,14 +256,29 @@ swagger-validate: ## check if the swagger spec is valid | |
| $(GO) run $(SWAGGER_PACKAGE) validate './$(SWAGGER_SPEC)' | ||
| @$(SED_INPLACE) -E -e 's|"basePath":( *)"/(.*)"|"basePath":\1"\2"|g' './$(SWAGGER_SPEC)' # remove the prefix slash from basePath | ||
|
|
||
| .PHONY: generate-openapi3 | ||
| generate-openapi3: $(OPENAPI3_SPEC) ## generate the OpenAPI 3.0 spec from the Swagger 2.0 spec | ||
|
|
||
| $(OPENAPI3_SPEC): $(SWAGGER_SPEC) build/generate-openapi.go $(wildcard build/openapi3gen/*.go) | ||
| $(GO) run build/generate-openapi.go | ||
|
|
||
| .PHONY: openapi3-check | ||
| openapi3-check: generate-openapi3 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still think these targets should not have a version, e.g.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. swagger == openapi 2.0, so something labeled only openapi could be v2 or v3. The version number makes it clear
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I never heard about openapi 2.0, and we don't want to rename this make target once we switch to openapi 3.1, 3.2 or 4.0. there will only ever be one openapi output.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The project already have a OpenAPI spec: the swagger spec. Swagger became OpenAPI 2.0, then OpenAPI came out with a new version, 3.0. If we rename the OpenAPI 3.0 spec "openapi", then it will be unclear what version this end point is.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think generate-openapi3 is a right name
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why? 3.1 literally belongs to 3, right? 4 doesn't belong to 3 and it breaks from 3, right? So
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OAS 3.1 is not strictly backwards-compatible with 3.0, there are subtle breaking changes. I don't get this discussion, we have a "swagger spec" and we have a "openapi spec", versions are irrelevant in both for the generation purpose. I would not recommend to have v3 and v4 specs to co-exist.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But now v2 (swagger) and and v3 do co-exist. I do not see anything wrong with the name "openapi3". If "OAS 3.1 is not strictly backwards-compatible with 3.0", then "openapi3" is just even better. Why you prefer to mix unclear things together? What's wrong with the number "3" to end users? Who will be hurt or confused?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Then why have it as |
||
| @diff=$$(git diff --color=always '$(OPENAPI3_SPEC)'); \ | ||
| if [ -n "$$diff" ]; then \ | ||
| echo "Please run 'make generate-openapi3' and commit the result:"; \ | ||
| printf "%s" "$${diff}"; \ | ||
| exit 1; \ | ||
| fi | ||
|
|
||
|
myers marked this conversation as resolved.
|
||
| .PHONY: checks | ||
| checks: checks-frontend checks-backend ## run various consistency checks | ||
|
|
||
| .PHONY: checks-frontend | ||
| checks-frontend: lockfile-check svg-check ## check frontend files | ||
|
|
||
| .PHONY: checks-backend | ||
| checks-backend: tidy-check swagger-check fmt-check swagger-validate security-check ## check backend files | ||
| checks-backend: tidy-check swagger-check openapi3-check fmt-check swagger-validate security-check ## check backend files | ||
|
|
||
| .PHONY: lint | ||
| lint: lint-frontend lint-backend lint-spell ## lint everything | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| // generate-openapi converts Gitea's Swagger 2.0 spec into an OpenAPI 3.0 spec. | ||
| // | ||
| // Gitea generates a Swagger 2.0 spec from code annotations (make generate-swagger). | ||
| // This tool converts it to OAS3 so that SDK generators and tools that require | ||
| // OAS3 (e.g. progenitor for Rust) can consume it directly. The conversion also | ||
| // deduplicates inline enum definitions into named schema components, producing | ||
| // cleaner SDK output with proper enum types instead of anonymous strings. | ||
| // | ||
| // Run: go run build/generate-openapi.go | ||
| // Output: templates/swagger/v1_openapi3_json.tmpl | ||
|
|
||
| //go:build ignore | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "regexp" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "code.gitea.io/gitea/build/openapi3gen" | ||
|
|
||
| "github.com/getkin/kin-openapi/openapi3" | ||
| ) | ||
|
|
||
| const ( | ||
| swaggerSpecPath = "templates/swagger/v1_json.tmpl" | ||
| openapi3OutPath = "templates/swagger/v1_openapi3_json.tmpl" | ||
|
|
||
| appSubUrlVar = "{{.SwaggerAppSubUrl}}" | ||
| appVerVar = "{{.SwaggerAppVer}}" | ||
|
|
||
| appSubUrlPlaceholder = "GITEA_APP_SUB_URL_PLACEHOLDER" | ||
| appVerPlaceholder = "0.0.0-gitea-placeholder" | ||
| ) | ||
|
|
||
| var ( | ||
| appSubUrlRe = regexp.MustCompile(regexp.QuoteMeta(appSubUrlVar)) | ||
| appVerRe = regexp.MustCompile(regexp.QuoteMeta(appVerVar)) | ||
|
|
||
| enumScanDirs = []string{ | ||
| "modules/structs", | ||
| "modules/commitstatus", | ||
| } | ||
| ) | ||
|
|
||
| func main() { | ||
| astEnumMap, err := openapi3gen.ScanSwaggerEnumTypes(enumScanDirs) | ||
| if err != nil { | ||
| log.Fatalf("scanning swagger:enum annotations: %v", err) | ||
| } | ||
| names := make([]string, 0, len(astEnumMap)) | ||
| for _, n := range astEnumMap { | ||
| names = append(names, n) | ||
| } | ||
| sort.Strings(names) | ||
| fmt.Fprintf(os.Stderr, "discovered %d swagger:enum types: %s\n", len(names), strings.Join(names, ", ")) | ||
|
|
||
| data, err := os.ReadFile(swaggerSpecPath) | ||
| if err != nil { | ||
| log.Fatalf("reading swagger spec: %v", err) | ||
| } | ||
|
|
||
| cleaned := appSubUrlRe.ReplaceAll(data, []byte(appSubUrlPlaceholder)) | ||
| cleaned = appVerRe.ReplaceAll(cleaned, []byte(appVerPlaceholder)) | ||
|
|
||
| oas3, err := openapi3gen.Convert(cleaned, astEnumMap) | ||
| if err != nil { | ||
| log.Fatalf("converting to openapi 3.0: %v", err) | ||
| } | ||
|
|
||
| oas3.Servers = openapi3.Servers{ | ||
| {URL: appSubUrlPlaceholder + "/api/v1"}, | ||
| } | ||
|
|
||
| out, err := json.MarshalIndent(oas3, "", " ") | ||
| if err != nil { | ||
| log.Fatalf("marshaling openapi 3.0: %v", err) | ||
| } | ||
|
|
||
| result := strings.ReplaceAll(string(out), appSubUrlPlaceholder, appSubUrlVar) | ||
| result = strings.ReplaceAll(result, appVerPlaceholder, appVerVar) | ||
| result = strings.TrimSpace(result) | ||
|
|
||
| if err := os.WriteFile(openapi3OutPath, []byte(result), 0o644); err != nil { | ||
| log.Fatalf("writing openapi 3.0 spec: %v", err) | ||
| } | ||
|
|
||
| fmt.Printf("Generated %s\n", openapi3OutPath) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.