Skip to content

Commit

Permalink
Add swagger for gRPC REST (cosmos#7075)
Browse files Browse the repository at this point in the history
* swagger gen command added

* proto file changed

* Add filter for swagger files

* Remove loop

* Add go-swagger installation

* Add swagger mixer command

* add swagger options

* remove files

* Fix swagger-combine

* Fix description

* remove unnecessary config option

* remove go-swagger dependency

* refactor

* Add proto-gen-swagger installation tool

* fix tool

* refactor

* don't push individual swagger files to repo

* refactor

* Fix doc

* move proto-swagger-gen to a separate target

* Fix permissions

* Add ibc swagger gen

* Update swagger generation doc

* cleanup

* gofmt

* refactor

* update Makefile

Co-authored-by: anilCSE <[email protected]>
Co-authored-by: Federico Kunze <[email protected]>
  • Loading branch information
3 people authored Aug 28, 2020
1 parent 4de5e28 commit d02cd16
Show file tree
Hide file tree
Showing 10 changed files with 14,044 additions and 571 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ go.sum: go.mod
###############################################################################

update-swagger-docs: statik
$(BINDIR)/statik -src=client/lcd/swagger-ui -dest=client/lcd -f -m
$(BINDIR)/statik -src=client/grpc-gateway -dest=client/grpc-gateway -f -m
@if [ -n "$(git status --porcelain)" ]; then \
echo "\033[91mSwagger docs are out of sync!!!\033[0m";\
exit 1;\
Expand Down Expand Up @@ -312,7 +312,7 @@ devdoc-update:
### Protobuf ###
###############################################################################

proto-all: proto-tools proto-gen proto-lint proto-check-breaking
proto-all: proto-tools proto-gen proto-lint proto-check-breaking proto-swagger-gen

proto-gen:
@./scripts/protocgen.sh
Expand All @@ -321,6 +321,9 @@ proto-gen:
proto-gen-any:
@./scripts/protocgen-any.sh

proto-swagger-gen:
@./scripts/protoc-swagger-gen.sh

proto-lint:
@buf check lint --error-format=json

Expand Down
122 changes: 122 additions & 0 deletions client/grpc-gateway/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"swagger": "2.0",
"info": {
"title": "Cosmos SDK - GRPC Gateway",
"version": "1.0.0"
},
"apis": [
{
"url": "./cosmos/auth/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "AuthParams"
}
}
},
{
"url": "./cosmos/bank/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "BankParams"
}
}
},
{
"url": "./cosmos/distribution/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "DistributionParams"
}
}
},
{
"url": "./cosmos/evidence/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "EvidenceParams"
}
}
},
{
"url": "./cosmos/gov/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "GovParams"
}
}
},
{
"url": "./cosmos/mint/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "MintParams"
}
}
},
{
"url": "./cosmos/params/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "Params"
}
}
},
{
"url": "./cosmos/slashing/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "SlashingParams"
}
}
},
{
"url": "./cosmos/staking/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "StakingParams",
"DelegatorValidators": "StakingDelegatorValidators"
}
}
},
{
"url": "./cosmos/upgrade/v1beta1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "UpgradeParams"
}
}
},
{
"url": "./ibc/channel/query.swagger.json",
"operationIds": {
"rename": {
"Params": "IBCChannelParams"
}
}
},
{
"url": "./ibc/client/query.swagger.json",
"operationIds": {
"rename": {
"Params": "IBCClientParams"
}
}
},
{
"url": "./ibc/connection/query.swagger.json",
"operationIds": {
"rename": {
"Params": "IBCConnectionParams"
}
}
},
{
"url": "./ibc/transfer/query.swagger.json",
"operationIds": {
"rename": {
"Params": "IBCTransferParams"
}
}
}
]
}
13 changes: 13 additions & 0 deletions client/grpc-gateway/statik/statik.go

Large diffs are not rendered by default.

13,302 changes: 13,302 additions & 0 deletions client/grpc-gateway/swagger.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions contrib/devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ else
@echo "protoc-gen-grpc-gateway already installed; skipping..."
endif

ifeq (, $(shell which protoc-gen-swagger))
@echo "Installing protoc-gen-swagger..."
@go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
@npm install -g swagger-combine
else
@echo "protoc-gen-grpc-gateway already installed; skipping..."
endif

buf: buf-stamp

buf-stamp:
Expand Down
25 changes: 25 additions & 0 deletions scripts/protoc-swagger-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

set -eo pipefail

proto_dirs=$(find ./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')
if [[ ! -z "$query_file" ]]; then
protoc \
-I "proto" \
-I "third_party/proto" \
"$query_file" \
--swagger_out=logtostderr=true,stderrthreshold=1000,fqn_for_swagger_name=true,simple_operation_ids=true:.
fi
done

# 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/grpc-gateway/config.json -o ./client/grpc-gateway/swagger.json --continueOnConflictingPaths true --includeDefinitions true

# clean swagger files
find ./ -name 'query.swagger.json' -exec rm {} \;
3 changes: 1 addition & 2 deletions scripts/protocgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:. \
-I "third_party/proto" \
--grpc-gateway_out=logtostderr=true:. \
$(find "${dir}" -maxdepth 1 -name '*.proto')

done

# generate codec/testdata proto code
Expand All @@ -26,5 +27,3 @@ Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types:. ./testutil
# move proto files to the right places
cp -r github.com/cosmos/cosmos-sdk/* ./
rm -rf github.com


6 changes: 3 additions & 3 deletions x/gov/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func TestRandomizedGenState1(t *testing.T) {
module.SimulationState{}, "invalid memory address or nil pointer dereference"},
{ // panic => reason: incomplete initialization of the simState
module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Rand: r,
AppParams: make(simtypes.AppParams),
Cdc: cdc,
Rand: r,
}, "assignment to entry in nil map"},
}

Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
suite.Equal(tc.numVals, len(valsResp.Validators))
suite.Equal(uint64(len(vals)), valsResp.Pagination.Total)

if (tc.hasNext) {
if tc.hasNext {
suite.NotNil(valsResp.Pagination.NextKey)
} else {
suite.Nil(valsResp.Pagination.NextKey)
Expand Down
Loading

0 comments on commit d02cd16

Please sign in to comment.