Skip to content

Commit

Permalink
Generate Swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
alexh-scrt committed Mar 22, 2024
1 parent a07b707 commit 9637022
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ proto-gen:
@$(protoImage) sh ./scripts/protocgen.sh

proto-swagger-gen:
@echo "Generating Protobuf Swagger"
@$(protoImage) sh ./scripts/protoc-swagger-gen.sh
@echo "Generating Protobuf Swagger: $@"
@$(shell ./scripts/protoc-swagger-gen.sh $@)

proto-format:
@$(protoImage) find ./proto -name "*.proto" -exec clang-format -i {} \;
Expand Down
12 changes: 0 additions & 12 deletions buf.gen.swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
version: v1

plugins:

- name: gocosmos
out: .
opt:
- plugins=grpc

- name: grpc-gateway
out: .
opt:
- logtostderr=true

- name: swagger
out: ./tmp-swagger-gen
opt:
Expand Down
23 changes: 23 additions & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This module represents buf.build/cosmos/cosmos-sdk
version: v1
deps:
- buf.build/cosmos/cosmos-proto
- buf.build/cosmos/cosmos-sdk
- buf.build/cosmos/gogo-proto
- buf.build/googleapis/googleapis
breaking:
use:
- FILE
lint:
use:
- DEFAULT
- COMMENTS
- FILE_LOWER_SNAKE_CASE
except:
- UNARY_RPC
- COMMENT_FIELD
- SERVICE_SUFFIX
- PACKAGE_VERSION_SUFFIX
- RPC_REQUEST_STANDARD_NAME
ignore:
- tendermint
50 changes: 26 additions & 24 deletions scripts/protoc-swagger-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -eo pipefail

mkdir -p ./tmp-swagger-gen
# Note: need to add ./third_party/proto/cosmos ./third_party/proto/ibc to a list of proto dirs when imports are fixed
proto_dirs=$(find ./proto ./third_party/proto/cosmos_proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
proto_dirs=$(find ./proto ./third_party/proto -path -prune -o -name '*.proto' -print0 | xargs -0 -n1 dirname | sort | uniq)
for dir in $proto_dirs; do
# generate swagger files (filter query files)
query_file=$(find "${dir}" -maxdepth 1 \( -name 'query.proto' -o -name 'service.proto' \))
Expand All @@ -13,33 +13,35 @@ for dir in $proto_dirs; do
fi
done

#jq 'del(.definitions["cosmos.tx.v1beta1.ModeInfo.Multi"].properties.mode_infos.items["$ref"])' ./tmp-swagger-gen/cosmos/tx/v1beta1/service.swagger.json > ./tmp-swagger-gen/cosmos/tx/v1beta1/fixed-service.swagger.json
jq 'del(.definitions["cosmos.tx.v1beta1.ModeInfo.Multi"].properties.mode_infos.items["$ref"])' ./tmp-swagger-gen/cosmos/tx/v1beta1/service.swagger.json > ./tmp-swagger-gen/cosmos/tx/v1beta1/fixed-service.swagger.json
# # Tag everything as "gRPC Gateway API"
# perl -i -pe 's/"(Query|Service)"/"gRPC Gateway API"/' $(find ./tmp-swagger-gen -name '*.swagger.json' -print0 | xargs -0)

# (
# cd ./client/docs

# # Generate config.json
# # There's some operationIds naming collision, for sake of automation we're
# # giving all of them a unique name
# find ../../tmp-swagger-gen -name 'query.swagger.json' -o -name 'fixed-service.swagger.json' |
# sort |
# awk '{print "{\"url\":\""$1"\",\"operationIds\":{\"rename\":{\"Params\":\""$1"Params\",\"Pool\":\""$1"Pool\",\"DelegatorValidators\":\""$1"DelegatorValidators\",\"UpgradedConsensusState\":\""$1"UpgradedConsensusState\"}}}"}' |
# jq -s '{swagger:"2.0","info":{"title":"Secret Network","description":"A REST interface for queries and transactions","version":"'"${CHAIN_VERSION}"'"},apis:.} | .apis += [{"url":"./swagger_legacy.yaml","dereference":{"circular":"ignore"}}]' > ./config.json

# # Derive openapi & swagger from config.json
# yarn install
# yarn combine
# yarn convert
# yarn build
# )

# cd ..
perl -i -pe 's/"(Query|Service)"/"gRPC Gateway API"/' $(find ./tmp-swagger-gen -name '*.swagger.json' -print0 | xargs -0)

(
cd ./client/docs

# Generate config.json
# There's some operationIds naming collision, for sake of automation we're
# giving all of them a unique name
find ../../tmp-swagger-gen -name 'query.swagger.json' -o -name 'fixed-service.swagger.json' |
sort |
awk '{print "{\"url\":\""$1"\",\"operationIds\":{\"rename\":{\"Params\":\""$1"Params\",\"Pool\":\""$1"Pool\",\"DelegatorValidators\":\""$1"DelegatorValidators\",\"UpgradedConsensusState\":\""$1"UpgradedConsensusState\"}}}"}' |
jq -s '{swagger:"2.0","info":{"title":"Secret Network","description":"A REST interface for queries and transactions","version":"'"${CHAIN_VERSION}"'"},apis:.} | .apis += [{"url":"./swagger_legacy.yaml","dereference":{"circular":"ignore"}}]' > ./config.json

# Derive openapi & swagger from config.json
# yarn install
# yarn combine
# yarn convert
# yarn build
)

cd ./client/docs
mkdir -p swagger-ui
# # combine swagger files
# # uses nodejs package `swagger-combine`.
# # all the individual swagger files need to be configured in `config.json` for merging
# swagger-combine ./client/docs/config.json -o ./client/docs/swagger-ui/swagger.yaml -f yaml --continueOnConflictingPaths true --includeDefinitions true
swagger-combine ./config.json -o ./swagger-ui/swagger.yaml -f yaml --continueOnConflictingPaths true --includeDefinitions true

cd ../..
# # clean swagger files
rm -rf ./tmp-swagger-gen
13 changes: 13 additions & 0 deletions third_party/proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
ignore:
- tendermint
- confio
- gogoproto
- google
- cosmos_proto

0 comments on commit 9637022

Please sign in to comment.