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

Missing Scheme in URI for Swagger when using PollConsul Service Discovery with Ocelot #307

Closed
rabdulatif opened this issue Oct 4, 2024 · 3 comments
Assignees

Comments

@rabdulatif
Copy link
Contributor

I encountered an issue when using Ocelot's PollConsul service discovery provider with dynamic service registration from code. The ServiceHostAndPort retrieved from the PollConsul provider does not include the URI scheme, causing an exception when attempting to access the Swagger URI. The exception is due to the missing scheme, making the URI invalid.

Problem:
When retrieving the ServiceHostAndPort using PollConsul, the scheme is missing.
As a result, when trying to get the Swagger URI, an exception is thrown: "URI is not valid" because the scheme is empty.
Routes are not defined in the ocelot.json file, as I am dynamically getting them.
My ocelot.json configuration:

  "Routes": [],
  "SwaggerEndPoints": [
    {
      "Key": "FileApi",
      "Config": [
        {
          "Name": "Files API",
          "Version": "v1",
          "Service": {
            "Name": "FileApi",
            "Path": "/swagger/v1/swagger.json"
          }
        }
      ]
    },
    {
      "Key": "ManagementApi",
      "Config": [
        {
          "Name": "Management API",
          "Version": "v1",
          "Service": {
            "Name": "ManagementApi",
            "Path": "/swagger/v1/swagger.json"
          }
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "PollConsul",
      "PollingInterval": 100
    }
  }
}

Fix:
To resolve the missing scheme issue, I modified the SwaggerServiceDiscoveryProvider class in the GetSwaggerUri(...) method (after line 109). I added the following code to ensure the scheme is set properly:

if (builder.Scheme.IsNullOrEmpty())
    builder.Scheme = conf?.Scheme ?? "http";

With this change, the Swagger URI is now constructed correctly.

Additional Issue:
After fixing the URI issue, I encountered another problem where the Swagger content was empty. Upon debugging, I noticed that the _transformer.Transform method clears the content when the ServiceProviderType is not Consul. However, my type is PollConsul.

Second Fix:
I added a condition to also check for PollConsul, and it resolved the issue:
if (SwaggerServiceDiscoveryProvider.ServiceProviderType != "Consul" && SwaggerServiceDiscoveryProvider.ServiceProviderType != "PollConsul") { // original logic }

Can I submit a PR?
I'd like to contribute this fix via a pull request as I need to deploy my project to the server soon

@Burgyn
Copy link
Owner

Burgyn commented Oct 4, 2024

Hi @rabdulatif,

Please prepare a PR.

Thanks

@rabdulatif
Copy link
Contributor Author

Hi @Burgyn ,

The PR is ready and has been submitted. Please let me know if you need any further changes or clarifications.

Thanks

@Burgyn
Copy link
Owner

Burgyn commented Oct 4, 2024

Fixed by #308

soon it will be published in version 8.3.1.

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

No branches or pull requests

2 participants