0

This is my ocelot configuration...

{
  "Routes": [
    {
      "UpstreamPathTemplate": "/api/test/{url}",
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "test-host",
          "Port": 8080
        }
      ],
      "SwaggerKey": "test"
    }
  ],
  "SwaggerEndPoints": [
    {
      "Key": "test",
      "Config": [
        {
          "Name": "Test API",
          "Version": "v1",
          "Url": "http://test-host:8080/swagger/v1/swagger.json",
        }
      ]
    }
  ]
}

Testing results are as follows...

The downstream endpoint:

  • running in a container, without an external port forward
  • is alive as an internal call to http://test-host:8080/api/weather-forecast will return an expected result
  • is running local swagger at http://test-host:8080/swagger and endpoints are shown and function correctly (json listed below)

The upstream endpoint (via Ocelot):

  • running on a different container (with access to downstream) and forwarded to localhost:80
  • is routing correctly (as per config) as a call to the external endpoint http://localhost/api/test/weather-forecast is remapping and will correctly return a result from the internal API
  • Is providing a central swagger at http://localhost/swagger/ that detects the down stream swagger (shown in the drop down as "Test API", however
  • Shows no endpoints! I get the "No operations defined in spec!" provided when the paths object in the spec is empty.

Things I have tried:

  • I believe it can see the downstream swagger spec ok because if I add RemoveUnusedComponentsFromScheme: false then it will find and render the models, but still no endpoints; further more intentionally breaking the URL returns an error; so I'm confident the downstream swagger spec is loaded correctly
  • If I add TransformByOcelotConfig: false it WILL show the endpoints (further proof the spec if loaded ok), but then the generated url's are wrong; the hostname is correct but the rest of the url uses the internal/downstream path not the re-written external/upstream path
  • Bumping package versions up/down to see if its a mismatch (currently Microsoft.AspNetCore.OpenApi 9.0.11, MMLib.SwaggerForOcelot 9.0.0, Swashbuckle.AspNetCore 9.0.6

Downstream swagger doc looks like...

{
  "openapi": "3.0.4",
  "info": {
    "title": "Test API",
    "version": "1.0"
  },
  "paths": {
    "/api/weather-forecast": {
      "get": {
        "tags": ["WeatherForecast"],
        "operationId": "GetWeatherForecast",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              },
              "text/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32",
            "readOnly": true
          },
          "summary": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    }
  }
}

Upstream swagger doc looks like...

{
  "openapi": "3.0.4",
  "info": {
    "title": "Test API",
    "version": "1.0"
  },
  "paths": {},
  "components": {
    "schemas": {}
  }
}

What am I missing here?

1 Answer 1

1

Frustratingly this is now working.

I think there was originally a mistake, now lost to history, but I have fallen victim to some kind of caching issue with Visual Studio debugging in docker that cached the old ocelot.json. Playing with it now; changes I make are not always reflected into the container that runs when you debug (regardless of the CopyToOutputDirectory=Always)

UPDATE

If you found yourself here with the same issue; I have had 100% success propagating changes via the following:

  • make changes to json config

  • 'clean' project before starting your debug

  • drop browser cache when the swagger comes up

Both VS/docker AND your browser can cache and stop your changes coming through

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.