Burgyn / MMLib.SwaggerForOcelot

This repo contains swagger extension for ocelot.
MIT License
354 stars 95 forks source link

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

Closed rabdulatif closed 1 month ago

rabdulatif commented 1 month ago

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 commented 1 month ago

Hi @rabdulatif,

Please prepare a PR.

Thanks

rabdulatif commented 1 month ago

Hi @Burgyn ,

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

Thanks

Burgyn commented 1 month ago

Fixed by #308

soon it will be published in version 8.3.1.