Skip to content

Commit 0a4289f

Browse files
authored
Generate OpenAPI documentation (#353)
**What changed?** We now generate openapi [v2 (swagger)](https://swagger.io/specification/v2/) and [v3](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md) specs as a part of proto compilation. **Why?** So that our customers can understand how to use our HTTP API
1 parent 4f491a7 commit 0a4289f

File tree

6 files changed

+14336
-8
lines changed

6 files changed

+14336
-8
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
with:
1414
go-version: '^1.21'
1515
- uses: arduino/setup-protoc@v2
16+
- name: 'Setup jq'
17+
uses: dcarbone/install-jq-action@v2
1618
- run: make ci-build
1719
- name: Fail if the repo is dirty
1820
run: |

Makefile

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
$(VERBOSE).SILENT:
22
############################# Main targets #############################
3-
ci-build: install proto
3+
ci-build: install proto http-api-docs
44

55
# Install dependencies.
66
install: grpc-install api-linter-install buf-install
77

88
# Run all linters and compile proto files.
9-
proto: grpc
9+
proto: grpc http-api-docs
1010
########################################################################
1111

1212
##### Variables ######
@@ -38,6 +38,9 @@ PROTO_IMPORTS = \
3838
-I=$(PROTO_ROOT)
3939
PROTO_PATHS = paths=source_relative:$(PROTO_OUT)
4040

41+
OAPI_OUT := openapi
42+
OAPI3_PATH := .components.schemas.Payload
43+
4144
$(PROTO_OUT):
4245
mkdir $(PROTO_OUT)
4346

@@ -46,23 +49,44 @@ grpc: buf-lint api-linter buf-breaking clean go-grpc fix-path
4649

4750
go-grpc: clean $(PROTO_OUT)
4851
printf $(COLOR) "Compile for go-gRPC..."
49-
$(foreach PROTO_DIR,$(PROTO_DIRS),\
50-
protoc --fatal_warnings $(PROTO_IMPORTS) \
51-
--go_out=$(PROTO_PATHS) \
52-
--grpc-gateway_out=allow_patch_feature=false,$(PROTO_PATHS)\
53-
--doc_out=html,index.html,source_relative:$(PROTO_OUT) \
54-
$(PROTO_DIR)*.proto;)
52+
protogen \
53+
--root=$(PROTO_ROOT) \
54+
--output=$(PROTO_OUT) \
55+
--exclude=internal \
56+
--exclude=proto/api/google \
57+
-I $(PROTO_ROOT) \
58+
-p go-grpc_out=$(PROTO_PATHS) \
59+
-p grpc-gateway_out=allow_patch_feature=false,$(PROTO_PATHS) \
60+
-p doc_out=html,index.html,source_relative:$(PROTO_OUT) \
61+
-p openapi_out=$(OAPI_OUT) \
62+
-p openapi_opt=enum_type=string \
63+
-p openapiv2_out=openapi \
64+
-p openapiv2_opt=allow_merge=true,merge_file_name=openapiv2
5565

5666
fix-path:
5767
mv -f $(PROTO_OUT)/temporal/api/* $(PROTO_OUT) && rm -rf $(PROTO_OUT)/temporal
5868

69+
# We need to rewrite bits of this to support our shorthand payload format
70+
# We use both yq and jq here as they preserve comments and the ordering of the original
71+
# document
72+
http-api-docs: go-grpc
73+
jq --rawfile desc $(OAPI_OUT)/payload_description.txt < $(OAPI_OUT)/openapiv2.swagger.json '.definitions.v1Payload={description: $$desc}' > $(OAPI_OUT)/v2.tmp
74+
mv -f $(OAPI_OUT)/v2.tmp $(OAPI_OUT)/openapiv2.json
75+
rm -f $(OAPI_OUT)/openapiv2.swagger.json
76+
DESC=$$(cat $(OAPI_OUT)/payload_description.txt) yq e -i '$(OAPIV3_PATH).description = strenv(DESC) | del($(OAPI3_PATH).type) | del($(OAPI3_PATH).properties)' $(OAPI_OUT)/openapi.yaml
77+
mv $(OAPI_OUT)/openapi.yaml $(OAPI_OUT)/openapiv3.yaml
78+
5979
##### Plugins & tools #####
6080
grpc-install:
6181
@printf $(COLOR) "Install/update protoc and plugins..."
82+
@go install go.temporal.io/api/cmd/protogen@latest
6283
@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
6384
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
6485
@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
6586
@go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest
87+
@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
88+
@go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
89+
@go install github.com/mikefarah/yq/v4@latest
6690

6791
api-linter-install:
6892
printf $(COLOR) "Install/update api-linter..."

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Temporal proto files
22

3+
This repository contains both the protobuf descriptors and OpenAPI documentation for the Temporal platform.
4+
35
## How to use
46

57
Install as git submodule to the project.

0 commit comments

Comments
 (0)