ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.28k stars 1.63k forks source link

Proposal to add ExtraProps to the configuration #1990

Closed Burgyn closed 3 months ago

Burgyn commented 5 months ago

If the need arises on the Ocelot usage side for an extension, we usually have to work with some custom settings in the Ocelot downstream route configuration as well.

The current options are that we put the custom settings into a custom setup and use the downstream key as a reference to it, or we can add them to the original JSON setup and read them ourselves (duplicate read).

From an extensibility point of view, it would be nice if we could add additional properties that the programmer could then read.

Proposal

 "ExtraProps" : {
    "CachePolicies": ["policy1", "policy2"],
    "SwaggerKey": "todo"
  }

Whole usage

{
    "Routes": [
        {
          "DownstreamPathTemplate": "/todos/{id}",
          "DownstreamScheme": "https",
          "DownstreamHostAndPorts": [
            {
               "Host": "jsonplaceholder.typicode.com",
               "Port": 443
            }
          ],
         "UpstreamPathTemplate": "/todos/{id}",
         "UpstreamHttpMethod": [ "Get" ],
         // 👇 additional properties for better extensibility
         "ExtraProps" : {
            "CachePolicies": ["policy1", "policy2"],
            "SwaggerKey": "todo",
            "AnotherProp": "value"
          }
        }
    ]
}

Usage in middleware:

PreQueryStringBuilderMiddleware = async (context, next) =>
{
  DownstreamRoute? route = context.Items.DownstreamRoute();
  var cachePolicies = route.GetExtraProps<string[]>("CachePolicies");
  ...
}

I can prepare PR for this change. I just want to check if this is a change that fits the direction of the project.

Thank you in advance for your reply.


{
    "Routes": [
        {
          "DownstreamPathTemplate": "/todos/{id}",
          "DownstreamScheme": "https",
          "DownstreamHostAndPorts": [
            {
               "Host": "jsonplaceholder.typicode.com",
               "Port": 443
            }
          ],
         "UpstreamPathTemplate": "/todos/{id}",
         "UpstreamHttpMethod": [ "Get" ],
         // 👇 additional properties for better extensibility
         "CachePolicies": ["policy1", "policy2"],
         "SwaggerKey": "todo",
         "AnotherProp": "value"
        }
    ]
}

Theoretically, we can also do that extra properties will be on the main level. This is nicer from a usage point of view, but more complicated to implement. I don't know what you prefer.

raman-m commented 5 months ago

Duplicate of #738

raman-m commented 5 months ago

Hi Miňo! Thanks for PR creation! But unfortunately your issue is duplicate of #738 which can be closed by #1843 (ready on 80-90%)


I can prepare PR for this change. I just want to check if this is a change that fits the direction of the project.

No, thanks! See #1843❗ I invite you to review #1843 only... Your issue will be linked to #1843 too.

Burgyn commented 5 months ago

Hi @raman-m,

thanks for linking.

M.

raman-m commented 3 months ago

Reopen for official closing

raman-m commented 3 months ago

Closed by #1843