CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swagger / OpenAPI Support combined with .NET Catch All Routes

Dreamwalker666 opened this issue · comments

Problem

I hit a problem that I have a catch all route "/key/{**path}"

Now this causes a problem with the Swagger UI it sends that exact string for the argument of path over the wire.

How UI displays
image

Over the wire it sends GET http://127.0.0.1:5001/key/{*path} instead of using the specified parameter.

But I also am assuming an auto generated client will get the same issue as the UI as wildcards are not part of the OpenAPI standard as of yet.

Ideas

This obviously stems from the fact that OpenAPI doesn't support catch all routes.

Now what I am thinking is we should have a way to override the route information sent out this could be as simple as RouteMetaData.EndpointRoute = "/key/{path}"
RouteMetaData.EndpointRoute = null // Default uses route from actual endpoint

I did consider it should translate maybe .NET Routing -> OpenAPI Route but I think that is overkill as generally it would just work and for anything that doesn't you can put the specification in your self to match the correct OpenAPI for you case.

I've manage to have a look into the code. While my idea would work your already doing template translation.

The (z.IsCatchAll ? "*" : string.Empty) part of the path generation is adding the * which breaks the UI. I am struggling to find in the OpenAPI docs what * means to open api it does not mention it?

Ok more digging :)

I think I located the issue. The path generated is "/key/{*path}" but the operations name does not match it's currently "path" if I change this in the online editor to "*path" it generates the correct curl request.

The error it reports is:

Semantic error at paths./key/{*path}.get.parameters.0.name
Path parameter "path" must have the corresponding {path} segment in the "/key/{*path}" path
Jump to line 24

I had another play and removed (z.IsCatchAll ? "*" : string.Empty) on a local build.

That fixes my issues and lets me continue work. But I'm not suggesting that's the solution was more to prove it was just the ref and parameter mismatch causing the issues in the OpenAPI schema that is currently generated.

I wonder if something changed somewhere, maybe the UI, it might be different again now as the OpenApi version Carter outputs is a bit behind what the current released version is.

We also have only one integration test for OpenAPI which doesn't have a catchall route in it so could probably do with adding that in there.

Good job for getting stuck in though and glad it works for you

Carter 6 will use ASP.NET Core OpenAPI implementation