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

Add Config.FileContent #1069

Closed
wants to merge 1 commit into from
Closed

Add Config.FileContent #1069

wants to merge 1 commit into from

Conversation

willfaught
Copy link
Contributor

@willfaught willfaught commented Apr 5, 2024

I need to be able to embed a swagger.json file into a Go program using the embed package. Config.FileContent enables that.

Summary by CodeRabbit

  • New Features
    • Introduced the ability to directly specify Swagger file content in configurations, enhancing flexibility.
  • Tests
    • Added comprehensive tests for Swagger middleware configurations, including custom paths and multiple instances.

@willfaught willfaught requested a review from a team as a code owner April 5, 2024 22:22
@willfaught willfaught requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team April 5, 2024 22:22
Copy link
Contributor

coderabbitai bot commented Apr 5, 2024

Walkthrough

The update introduces a significant enhancement to the Swagger middleware, focusing on increased flexibility in handling Swagger specifications. It now allows the direct inclusion of Swagger file content within the configuration, reducing dependency on file paths. Additionally, comprehensive testing has been expanded to cover new functionalities, including custom paths and configurations, ensuring robust handling of multiple Swagger instances and custom routes.

Changes

Files Change Summary
swagger/swagger.go Added FileContent field in Config for direct Swagger content specification; updated logic for file handling.
swagger/swagger_test.go Enhanced tests for various configurations, custom paths, and file content; added support for multiple instances.

🐰✨
In the realm of code, where the documents swagger,
A rabbit hopped in, with a change that's rather dapper.
"No more paths to tread, for the specs we now embed,
In configs, they'll lay, making deployment a brighter day."
With tests to ensure, that every route is pure,
This rabbit's work, for sure, makes the middleware endure.
🌟📄

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 3

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 907ca33 and 59764dd.
Files selected for processing (2)
  • swagger/swagger.go (3 hunks)
  • swagger/swagger_test.go (3 hunks)
Additional comments not posted (2)
swagger/swagger.go (1)

35-39: Consider documenting the expected format of FileContent (JSON or YAML) for clarity.

swagger/swagger_test.go (1)

4-4: Good use of the embed package to include swagger files directly in the test code.

swagger/swagger.go Show resolved Hide resolved
swagger/swagger.go Show resolved Hide resolved
Comment on lines +270 to +493
cfg := Config{FileContent: swaggerJSON}

app.Use(New(cfg))

w1 := performRequest("GET", "/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Swagger file content not specified", func(t *testing.T) {
app := fiber.New()

cfg := Config{
FilePath: "./docs/swagger.json",
}

require.Panics(t, func() {
app.Use(New(cfg))
}, "content not specified")
})

t.Run("Endpoint check with multiple Swagger instances", func(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
BasePath: "/api/v1",
FileContent: swaggerJSON,
}))

app.Use(New(Config{
BasePath: "/api/v2",
FileContent: swaggerJSON,
}))

w1 := performRequest("GET", "/api/v1/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/api/v1/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/api/v2/docs", app)
require.Equal(t, 200, w3.StatusCode)

w4 := performRequest("GET", "/api/v2/swagger.json", app)
require.Equal(t, 200, w4.StatusCode)

w5 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w5.StatusCode)
})

t.Run("Endpoint check with custom routes", func(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
BasePath: "/api/v1",
FileContent: swaggerJSON,
}))

app.Get("/api/v1/tasks", func(c *fiber.Ctx) error {
return c.SendString("success")
})

app.Get("/api/v1", func(c *fiber.Ctx) error {
return c.SendString("success")
})

w1 := performRequest("GET", "/api/v1/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/api/v1/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)

// Verify we can send request to handler with the same BasePath as the middleware
w4 := performRequest("GET", "/api/v1/tasks", app)
bodyBytes, err := io.ReadAll(w4.Body)
require.NoError(t, err)
require.Equal(t, 200, w4.StatusCode)
require.Equal(t, "success", string(bodyBytes))

// Verify handler in BasePath still works
w5 := performRequest("GET", "/api/v1", app)
bodyBytes, err = io.ReadAll(w5.Body)
require.NoError(t, err)
require.Equal(t, 200, w5.StatusCode)
require.Equal(t, "success", string(bodyBytes))

w6 := performRequest("GET", "/api/v1/", app)
bodyBytes, err = io.ReadAll(w6.Body)
require.NoError(t, err)
require.Equal(t, 200, w6.StatusCode)
require.Equal(t, "success", string(bodyBytes))
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Add negative test cases to ensure robust error handling, such as providing invalid JSON/YAML in FileContent.

Would you like me to help by adding these test cases?

@willfaught
Copy link
Contributor Author

Note that I tried to follow the way the existing code does and tests things.

if len(rawSpec) == 0 {
// Verify Swagger file exists
_, err := os.Stat(cfg.FilePath)
if os.IsNotExist(err) {
Copy link
Member

Choose a reason for hiding this comment

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

Use errors.Is(err, fs.ErrNotExist) instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is existing code. I tried to minimize the changes. Do you still want me to change it?

@gaby
Copy link
Member

gaby commented Apr 20, 2024

@willfaught Want me to take over and get this merged?

@willfaught
Copy link
Contributor Author

I forgot I had this PR open. If a maintainer is willing to attend to this PR to get it merged, I can reopen the PR.

@gaby
Copy link
Member

gaby commented Apr 20, 2024

@willfaught I will take a look if you can re-open it, thanks!

@willfaught
Copy link
Contributor Author

GitHub won't let me reopen this PR. Opened a new PR: #1085.

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