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

Fix missing URI scheme in Swagger when using PollConsul service discovery #308

Merged
merged 2 commits into from
Oct 4, 2024

Conversation

rabdulatif
Copy link
Contributor

@rabdulatif rabdulatif commented Oct 4, 2024

This PR addresses an issue where the scheme is missing from the Swagger URI when using the PollConsul service discovery provider.

Changes:

  • Updated SwaggerServiceDiscoveryProvider.GetSwaggerUri(...) to set the scheme if missing.
  • Added additional condition to avoid clearing Swagger content when the ServiceProviderType is PollConsul.

This fixes issue #307 .

Summary by CodeRabbit

  • New Features

    • Added a null or empty check for the Scheme property in the Swagger URI builder, improving configuration handling.
  • Improvements

    • Enhanced readability of the codebase through formatting adjustments in several methods.
  • Bug Fixes

    • Ensured default scheme is applied when the Scheme property is null or empty.

Copy link

update-docs bot commented Oct 4, 2024

Thanks for opening this pull request! If you have implemented new functions, write about them in the readme file.

Copy link

coderabbitai bot commented Oct 4, 2024

Walkthrough

The pull request introduces modifications to the SwaggerForOcelotMiddleware and SwaggerServiceDiscoveryProvider classes. Key changes include enhancements to control flow and readability in the Invoke, ReconfigureUpstreamSwagger, and GetSwaggerUri methods. Notably, a null or empty check for the Scheme property in the GetSwaggerUri method has been added to ensure proper URI construction. Additionally, formatting adjustments have been made throughout the code to improve clarity while preserving existing logic and functionality.

Changes

File Change Summary
src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs - Reformatted variable assignments in Invoke method for readability.
- Expanded conditional check for ServiceProviderType to include "PollConsul".
- Reformatted GetServerName method for clarity.
- Improved formatting in ReconfigureUpstreamSwagger method.
src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs - Added null or empty check for Scheme in GetSwaggerUri method.
- Reformatted GetErrorMessage method for readability.
- Adjusted indentation in GetScheme method.

Possibly related issues

Possibly related PRs

Poem

🐇 In the code where bunnies hop,
We fixed the URIs, made them stop!
With schemes in place, no more despair,
Swagger's ready, hop with flair!
Readable lines, a joyful sight,
Code's now clear, and all feels right! 🌼


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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/schema.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

@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: 0

🧹 Outside diff range and nitpick comments (1)
src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs (1)

141-142: Approved: Improved method formatting with a suggestion

The reformatting of GetErrorMessage to use an expression-bodied member improves code conciseness and readability, which is a good practice in modern C#.

Consider fixing the typo in the error message:

- $"Service with swagger documentation '{endPoint.Service.Name}' cann't be discovered";
+ $"Service with swagger documentation '{endPoint.Service.Name}' cannot be discovered";
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3066153 and 04778d0.

📒 Files selected for processing (2)
  • src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs (4 hunks)
  • src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs (2 hunks)
🔇 Additional comments (6)
src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs (3)

110-112: Excellent addition to handle missing URI scheme

This change directly addresses the PR objective of fixing the missing URI scheme issue when using PollConsul service discovery. The null check and fallback mechanism ensure that a scheme is always set, improving the robustness of the code.


128-137: Improved code formatting enhances readability

The reformatting of the GetScheme method improves code readability without changing its functionality. The use of pattern matching with the switch expression is maintained, which is a good practice in modern C#.


139-139: Approval of nullable ServiceProviderType property

The modification of ServiceProviderType to be nullable aligns with modern C# practices for better null handling. This change might be related to the PR objectives, potentially allowing for null values when the service provider type is not set.

Could you please clarify the specific reason for making this property nullable? It would be helpful to understand how this change relates to the overall PR objectives.

src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs (3)

72-73: Improved code readability

The split of the tuple assignment into two lines enhances code readability without changing the functionality. This change aligns with good coding practices.


Line range hint 1-180: Summary of changes and their impact

The changes in this file successfully address the PR objective of fixing the missing URI scheme issue when using PollConsul service discovery. The modifications ensure that the Swagger content is not cleared when using PollConsul, which is consistent with the PR description.

Key points:

  1. The code readability has been improved by splitting a tuple assignment.
  2. The condition for handling different service discovery providers has been updated to include PollConsul.

These changes appear to solve the reported issue without introducing new problems. However, consider the suggested refactoring of the condition structure for better maintainability in the future.

Overall, the implementation looks solid and achieves the intended goal of the PR.


96-97: Approved change with suggestion for improvement

The addition of the PollConsul condition addresses the PR objective of fixing the missing URI scheme issue when using PollConsul service discovery. This change ensures that the Swagger content is not cleared when using PollConsul, which is consistent with the PR description.

However, I suggest refactoring the condition for better readability and maintainability:

-if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul" &&
-    SwaggerServiceDiscoveryProvider.ServiceProviderType != "PollConsul")
+if (!new[] { "Consul", "PollConsul" }.Contains(SwaggerServiceDiscoveryProvider.ServiceProviderType))

This refactoring makes it easier to add or remove service provider types in the future without changing the condition structure.

To ensure this change doesn't have unintended consequences, let's verify the usage of SwaggerServiceDiscoveryProvider.ServiceProviderType across the codebase:

This will help us identify any other places in the code that might need similar updates.

✅ Verification successful

Verified the refactoring suggestion

The search results confirm that SwaggerServiceDiscoveryProvider.ServiceProviderType is only used in the reviewed condition within SwaggerForOcelotMiddleware.cs. This ensures that the proposed refactoring will not affect other parts of the codebase.

Approved the refactoring suggestion.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage of SwaggerServiceDiscoveryProvider.ServiceProviderType

# Search for all occurrences of SwaggerServiceDiscoveryProvider.ServiceProviderType
echo "Occurrences of SwaggerServiceDiscoveryProvider.ServiceProviderType:"
rg --type csharp "SwaggerServiceDiscoveryProvider\.ServiceProviderType"

# Search for other conditions related to Consul or PollConsul
echo "\nOther conditions related to Consul or PollConsul:"
rg --type csharp "(Consul|PollConsul)"

Length of output: 980

Copy link

@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: 1

🧹 Outside diff range and nitpick comments (2)
src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs (2)

142-142: Approve new property and suggest improvements

The addition of the ServiceProviderType property is good and aligns with the PR objectives. However, there are a couple of improvements that can be made:

  1. Add XML documentation for the property to improve code documentation.
  2. Consider adding a #nullable enable directive at the file level to properly enable nullable reference types.

Here's a suggested improvement:

/// <summary>
/// Gets or sets the type of the service provider.
/// </summary>
public static string? ServiceProviderType { get; set; }

Also, consider adding #nullable enable at the top of the file if it's not already present.

🧰 Tools
🪛 GitHub Check: build-and-test

[warning] 142-142:
The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.


[warning] 142-142:
Missing XML comment for publicly visible type or member 'SwaggerServiceDiscoveryProvider.ServiceProviderType'


144-145: Approve formatting change and fix typo

The reformatting of the GetErrorMessage method to use an expression-bodied member improves code conciseness. This is a good change.

However, there's a small typo in the error message that should be corrected.

Please correct the typo in the error message:

- $"Service with swagger documentation '{endPoint.Service.Name}' cann't be discovered";
+ $"Service with swagger documentation '{endPoint.Service.Name}' can't be discovered";
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 04778d0 and c676fe9.

📒 Files selected for processing (1)
  • src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs (2 hunks)
🧰 Additional context used
🪛 GitHub Check: build-and-test
src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs

[warning] 142-142:
The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.


[warning] 142-142:
Missing XML comment for publicly visible type or member 'SwaggerServiceDiscoveryProvider.ServiceProviderType'

🔇 Additional comments (2)
src/MMLib.SwaggerForOcelot/ServiceDiscovery/SwaggerServiceDiscoveryProvider.cs (2)

131-140: Approve formatting changes

The reformatting of the GetScheme method using a switch expression improves code readability without changing the underlying logic. This is a good improvement.


Line range hint 1-145: Summary of changes

The changes in this file successfully address the PR objective of fixing the missing URI scheme issue in Swagger when using PollConsul service discovery. The code quality has been improved with better formatting and readability.

Key improvements:

  1. Added a check for null or empty scheme in the UriBuilder.
  2. Reformatted the GetScheme method for better readability.
  3. Added a new ServiceProviderType property to handle different service discovery provider types.
  4. Reformatted the GetErrorMessage method for conciseness.

Minor suggestions for further improvement have been provided in the individual comments. Overall, these changes look good and should resolve the reported issue.

🧰 Tools
🪛 GitHub Check: build-and-test

[warning] 142-142:
The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.


[warning] 142-142:
Missing XML comment for publicly visible type or member 'SwaggerServiceDiscoveryProvider.ServiceProviderType'

Comment on lines +110 to +115
if (builder.Scheme.IsNullOrEmpty())
{
builder.Scheme = conf?.Scheme ?? "http";
}
builder.Scheme = conf?.Scheme ?? "http";

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Approve changes and remove duplicate line

The added check for a null or empty scheme addresses the PR objective of fixing the missing URI scheme issue in Swagger. This change looks good and should resolve the reported problem.

However, there's a duplicate line that should be removed:

Please remove the duplicate line:

            if (builder.Scheme.IsNullOrEmpty())
            {
                builder.Scheme = conf?.Scheme ?? "http";
            }
-           builder.Scheme = conf?.Scheme ?? "http";
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (builder.Scheme.IsNullOrEmpty())
{
builder.Scheme = conf?.Scheme ?? "http";
}
builder.Scheme = conf?.Scheme ?? "http";
if (builder.Scheme.IsNullOrEmpty())
{
builder.Scheme = conf?.Scheme ?? "http";
}

@Burgyn Burgyn merged commit c676fe9 into Burgyn:master Oct 4, 2024
2 of 3 checks passed
@Burgyn
Copy link
Owner

Burgyn commented Oct 4, 2024

close #307

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.

2 participants