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

Match multiple ocelot routes for given downstream endpoint #284

Merged
merged 4 commits into from
Dec 4, 2023

Conversation

satano
Copy link
Contributor

@satano satano commented Dec 3, 2023

Downstream paths are matched against all relevant Ocelot routes and used all routes that fit. Consider following downstream API:

  • GET /api/test
  • POST /api/test

Which is represented in OpenApi JSON as:

"paths": {
    "/api/test": {
        "get": {

        },
        "post": {

        }
    }
}

And this ocelot.json:

"Routes": [
    {
        "UpstreamPathTemplate": "/post/ocelot",
        "UpstreamHttpMethod": [ "POST" ],
        "DownstreamPathTemplate": "/api/test"
        "SwaggerKey": "test"
    },
    {
        "UpstreamPathTemplate": "/all/ocelot",
        "DownstreamPathTemplate": "/api/test"
        "SwaggerKey": "test"
    }
]

So both API endpoints are mapped to /all/ocelot route, but POST endpoint is also mapped to /post/ocelot.

Former implementation of generating Swagger JSON file worked this way

  • Walked through all paths in downstream.
  • For each path (in this case just /api/test) found first suitable Ocelot route, which was /post/ocelot.
  • Then for processing path walked through all its methods (GET, POST in this case) and checked if matched route can handle these methods.
    • If method was not hanlded by Ocelot route, it was completely removed. In this example, GET method is not in the first route, so it was removed from downstream.
  • If all methods was removed from processing path (meaning this path remained empty), the whole path was removed from downstrem.

New implementation in this PR

Key changes in implementation are:

  • Not just paths, but every method in path is checked against Ocelot routes separately.
  • All suitable Ocelot routes are used, not just first one.
  • All original paths in downstream are removed and new ones are built based on matched Ocelot routes.

So the previous example works this way:

  • All suitable Ocelot routes for GET /api/test are found.
    • Only second route /all/ocelot matches, because the first one is only for POST.
    • New downstream path /all/ocelot is created and GET method is added to it.
  • All suitable Ocelot routes for POST /api/test are found.
    • Now both Ocelot routes match. First one is explicitly for POST, second one is for all methods.
    • Downstream path /all/ocelot/ is already created, so POST method is added to it.
    • New downstream path /post/ocelot is created and POST method is added to it.
  • When all paths and their methods are processed:
    • All original paths are removed from downstream.
    • All new generated downstream paths with methods are added.

Copy link

update-docs bot commented Dec 3, 2023

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

Copy link

request-info bot commented Dec 3, 2023

We would appreciate it if you could provide us with more info about this issue/pr!

@request-info request-info bot added the needs-more-info Autor needs more info label Dec 3, 2023
@Burgyn Burgyn marked this pull request as ready for review December 3, 2023 14:18
@satano satano force-pushed the matchMultipleOcelotRoutes branch from 80805cd to dc908e6 Compare December 3, 2023 14:48
@Burgyn Burgyn merged commit c1abe11 into Burgyn:master Dec 4, 2023
3 checks passed
@satano satano deleted the matchMultipleOcelotRoutes branch December 4, 2023 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-more-info Autor needs more info
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants