Skip to content
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

feat: swagger.yaml #2818

Closed
wants to merge 17 commits into from
Closed

Conversation

gregnuj
Copy link

@gregnuj gregnuj commented Nov 7, 2023

Overview

Add cosmos sdk standard scripts/protoc-swagger-gen.sh and supporting config.json file to enable the generation of lcd swagger.yaml file

Checklist

  • [ *] New and updated code has appropriate documentation
  • [ *] New and updated code has new and/or updated testing
  • [ *] Required CI checks are passing
  • [ *] Visual proof for any user facing features like CLI or documentation updates
  • [ *] Linked issues closed with keywords

@rootulp
Copy link
Collaborator

rootulp commented Nov 8, 2023

@gregnuj can you provide more context on why we want this PR? Is there an issue this closes? What does the "lcd swagger.yaml file" do?

scripts/protoc-swagger-gen.sh Outdated Show resolved Hide resolved
scripts/protoc-swagger-gen.sh Outdated Show resolved Hide resolved
scripts/protoc-swagger-gen.sh Outdated Show resolved Hide resolved
@celestia-bot celestia-bot requested a review from a team November 8, 2023 16:55
@gregnuj
Copy link
Author

gregnuj commented Nov 8, 2023

The swagger.yaml file describes the available endpoints to your developers and provides them a ui to test api endpoints.
for example:
https://osmosis-api.polkachu.com/swagger/
https://cosmos-api.polkachu.com/swagger/
https://terra-api.polkachu.com/swagger/
https://neutron-api.polkachu.com/swagger/
https://injective-api.polkachu.com/swagger/

This is standard practice in cosmos-sdk which, given the proper path to the file in the code, will provide an interface as linked above when enabled via the app.toml file or via the cli option for example:

[root@celestia-0 bin]# ./celestia-appd start --help |  grep swagger
      --api.swagger                                     Define if swagger documentation should automatically be registered (Note: the API must also be enabled)

@gregnuj
Copy link
Author

gregnuj commented Nov 8, 2023

As far as issues I see none open in github, but search your discord for "swagger", multiple inquiries regarding the missing swagger doc.

@rootulp
Copy link
Collaborator

rootulp commented Nov 8, 2023

the generation of lcd swagger.yaml file

What does lcd stand for?

Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Confirmed Osmosis and Cosmos Hub have something similar:

  1. https://github.com/osmosis-labs/osmosis/blob/main/client/docs/config.json
  2. https://github.com/cosmos/gaia/blob/be7129bb6bdf3099e000b9786b15b98ec1defe54/client/docs/config.json

so I think we want this, but I want to verify the generated config.json before approving. Is there an easy way to do that?

scripts/protoc-swagger-gen.sh Outdated Show resolved Hide resolved
scripts/protoc-swagger-gen.sh Outdated Show resolved Hide resolved
docs/swagger-ui/config.json Outdated Show resolved Hide resolved
@rootulp rootulp changed the title add protoc-swagger-gen script and supporting files feat: swagger config.json Nov 8, 2023
@gregnuj
Copy link
Author

gregnuj commented Nov 9, 2023

Thanks for the contribution! Confirmed Osmosis and Cosmos Hub have something similar:

  1. https://github.com/osmosis-labs/osmosis/blob/main/client/docs/config.json
  2. https://github.com/cosmos/gaia/blob/be7129bb6bdf3099e000b9786b15b98ec1defe54/client/docs/config.json

so I think we want this, but I want to verify the generated config.json before approving. Is there an easy way to do that?

the scripts generates a file called "swagger.yaml" the config.json file is just some instructions how to structure the output file...

@gregnuj
Copy link
Author

gregnuj commented Nov 9, 2023

the generation of lcd swagger.yaml file

What does lcd stand for?

lcd = light client daemon, although technically the name of the rest client in cosmos-sdk has been renamed from lcd to grpc-gateway

@gregnuj
Copy link
Author

gregnuj commented Nov 9, 2023

Thanks for the contribution! Confirmed Osmosis and Cosmos Hub have something similar:

  1. https://github.com/osmosis-labs/osmosis/blob/main/client/docs/config.json
  2. https://github.com/cosmos/gaia/blob/be7129bb6bdf3099e000b9786b15b98ec1defe54/client/docs/config.json

so I think we want this, but I want to verify the generated config.json before approving. Is there an easy way to do that?

run the script and you can test it here https://editor.swagger.io

1 similar comment
@gregnuj
Copy link
Author

gregnuj commented Nov 9, 2023

Thanks for the contribution! Confirmed Osmosis and Cosmos Hub have something similar:

  1. https://github.com/osmosis-labs/osmosis/blob/main/client/docs/config.json
  2. https://github.com/cosmos/gaia/blob/be7129bb6bdf3099e000b9786b15b98ec1defe54/client/docs/config.json

so I think we want this, but I want to verify the generated config.json before approving. Is there an easy way to do that?

run the script and you can test it here https://editor.swagger.io

@celestia-bot celestia-bot requested a review from a team November 9, 2023 01:13
@codecov-commenter
Copy link

Codecov Report

Merging #2818 (0c9559e) into main (75f9393) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main    #2818   +/-   ##
=======================================
  Coverage   19.80%   19.80%           
=======================================
  Files         144      144           
  Lines       17398    17398           
=======================================
  Hits         3446     3446           
  Misses      13631    13631           
  Partials      321      321           

@rootulp rootulp changed the title feat: swagger config.json feat: swagger.yaml Nov 9, 2023
@rootulp
Copy link
Collaborator

rootulp commented Nov 9, 2023

@rootulp rootulp marked this pull request as draft November 15, 2023 15:46
@rootulp
Copy link
Collaborator

rootulp commented Nov 15, 2023

Converting to draft until this error is resolved ^

@gregnuj gregnuj force-pushed the feat/protoc-swagger-gen branch from c5c566e to c917e5c Compare November 16, 2023 16:45
Copy link
Contributor

coderabbitai bot commented Nov 16, 2023

Walkthrough

The changes involve updates to scripts used for generating Swagger documentation from Protoc files. A Python script merge_swagger.py has been introduced to merge individual Swagger JSON files into a single YAML file, with functionality to handle duplicate keys and retrieve project versions. The Bash script protoc-swagger-gen.sh has been updated to include error handling, temporary directory management, and the use of the Python script to merge Swagger files into a final JSON file.

Changes

File Path Change Summary
scripts/merge_swagger.py Introduced a Python script for merging Swagger JSON files into a YAML file, handling duplicates, and version retrieval from go.mod.
scripts/protoc-swagger-gen.sh Updated Bash script to generate Swagger files with error handling, directory management, and integration of the Python merging script.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

@gregnuj
Copy link
Author

gregnuj commented Nov 16, 2023

When I put https://github.com/gregnuj/celestia-app/blob/main/docs/swagger-ui/swagger.yaml into https://editor.swagger.io/ I observe errors:

Screenshot 2023-11-09 at 3 50 08 PM

I added a title, I cannot resolve the other error without removing valid endpoints
the swagger editor considers these equivalent paths since the parameter is a string in either case

/cosmos/auth/v1beta1/bech32/{address_bytes}:
/cosmos/auth/v1beta1/bech32/{address_string}:

@gregnuj gregnuj marked this pull request as ready for review November 28, 2023 00:03
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 7

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7f3e4ec and 477e825.
Files ignored due to filter (1)
  • docs/swagger-ui/swagger.yaml
Files selected for processing (2)
  • scripts/merge_swagger.py (1 hunks)
  • scripts/protoc-swagger-gen.sh (1 hunks)

scripts/protoc-swagger-gen.sh Outdated Show resolved Hide resolved
scripts/protoc-swagger-gen.sh Show resolved Hide resolved
Comment on lines +94 to +97
parser.add_argument('-d', '--directory', help='Directory containing swagger files')
parser.add_argument('-t', '--title', help='Title for the swagger file')
parser.add_argument('-v', '--version', help='Version for the swagger file')
parser.add_argument('-o', '--output', help='Output file')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script should validate that all required command-line arguments are provided and handle cases where they are not, to prevent runtime errors.

Comment on lines +33 to +41
with open(os.path.join(project_root, "go.mod"), "r") as f:
for line in f.readlines():
if line.startswith("module"):
version = line.split("/")[-1].strip()
break

if not version:
print("Could not find version in go.mod")
exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding exception handling around file operations to gracefully handle cases where the go.mod file does not exist or cannot be read due to permissions or other issues.

rootulp and others added 2 commits December 1, 2023 15:35
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
# This script generates the swagger.yaml documentation for the rest API on port 1317
#
# Prior to running this script, please install the following:
# - Install Go 1.21+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Go version specified in the comment is 1.21+, which does not exist. Please correct it to a valid Go version, such as 1.21 (if it's a typo) or the actual minimum required version.


set -eo pipefail

work_dir="$(dirname "$(dirname "$(realpath "$0")")")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The realpath command is not available by default on all systems, such as macOS. Consider using a more portable alternative or ensure that realpath is installed as part of the prerequisites.

go build -o $tmp_dir/protoc-gen-swagger protoc-gen-swagger/main.go
cd $tmp_dir

PATH=./:$PATH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the current directory to the PATH can be a security risk. Consider specifying the full path to the protoc-gen-swagger binary instead of modifying the PATH.

Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run this script locally, I observe changes to the generated swagger.yaml. See https://pastebin.com/K05t1qbr

I would expect no changes. Can we update the generator or swagger.yaml so that subsequent invocations to ./scripts/protoc-swagger-gen.sh don't cause a dirty git diff.

Even though it's late in the review process, I think a GitHub issue with more context on what this PR is attempting to address would help. Ideally the GitHub issue would contain context, a problem statement, a proposal for how to resolve, etc.

scripts/merge_swagger.py Outdated Show resolved Hide resolved
#
# Prior to running this script, please install the following:
# - Install Go 1.21+
# - Install pyyaml: `pip3 install pyyaml`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway to do this via Go or bash so that maintainers don't have to set up Python to run this? AFAIK no other files in this repo use Python so it would be preferable to not introduce a new language / environment set up.

@celestia-bot celestia-bot requested a review from a team December 7, 2023 03:49
Comment on lines +65 to +66
for key in data["definitions"]:
definitions[key] = data["definitions"][key]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script still merges definitions without checking for duplicates, which could lead to data loss if two definitions with the same key but different contents exist. Consider adding a check similar to the one for paths to handle duplicate definitions appropriately.

+            if key in definitions and definitions[key] != data["definitions"][key]:
+                print(f"Duplicate definition found for {key}, which may lead to unexpected behavior.")
+                continue
+            definitions[key] = data["definitions"][key]

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
for key in data["definitions"]:
definitions[key] = data["definitions"][key]
for key in data["definitions"]:
if key in definitions and definitions[key] != data["definitions"][key]:
print(f"Duplicate definition found for {key}, which may lead to unexpected behavior.")
continue
definitions[key] = data["definitions"][key]

@rootulp rootulp marked this pull request as draft December 7, 2023 16:29
@rootulp
Copy link
Collaborator

rootulp commented Dec 28, 2023

Closing because stale. If this is still something we want, please re-open the PR or create a GH issue.

@rootulp rootulp closed this Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants