refit icon indicating copy to clipboard operation
refit copied to clipboard

Optional url segment

Open nbsoftware opened this issue 7 years ago • 5 comments

Would there be a way to support route definitions with optional segments (as for instance in PHP Slim Framework)?

For instance, if I want to delete say one specific notification message from a user/device or delete all of them I would like to have the following RefitDefinition:

(omitting [Header] parameters for simplicity sake)

        [Delete("/push/notifMsg/{deviceId}[/{notifMsgId}]")]
        Task<StatusCountResponse> DeleteNotifMsgs(
            Guid? deviceId,
            Guid? notificationMsgId,
            CancellationToken ct);

Notice the brackets.

The issue right now is that with the only supported definition which is the full one (i.e. without brackets) as:

        [Delete("/push/notifMsg/{deviceId}/{notifMsgId}")]
        Task<StatusCountResponse> DeleteNotifMsgs(
            Guid? deviceId,
            Guid? notificationMsgId,
            CancellationToken ct);

If say notificationMsgId is null, it generates the url as /push/notifMsg/{deviceId}/ i.e. with a trailing slash which gets a 404 response code with php slim-based server code because it is not recognized as a registered valid route. But /push/notifMsg/{deviceId} is indeed valid on the server side.

Would there be an existing workaround for this use case?

Unless I'm missing the obvious, which is always possible!!

nbsoftware avatar Dec 08 '18 10:12 nbsoftware

Of course the syntax I was showing was taken from some PHP framework. With Microsoft's Web API 2 i.e. using ?, this translates to:

[Delete("/push/notifMsg/{deviceId}/{notifMsgId?}")]
Task<StatusCountResponse> DeleteNotifMsgs(
            Guid? deviceId,
            Guid? notificationMsgId = null,
            CancellationToken ct);

and will issue a call to /push/notifMsg/{deviceId}if notifMsgId isn't provided. But I'm sure you get the idea.

Anyone?

nbsoftware avatar Dec 19 '18 15:12 nbsoftware

I'll pick this up.

jamiehowarth0 avatar Jun 09 '19 22:06 jamiehowarth0

@benjaminhowarth1 Any chance?

nbsoftware avatar Dec 03 '19 07:12 nbsoftware

I could also use this. The workaround for this we use now is an overload:

interface MyApi {
  [Get("/foo/{bar}")]
  GetFoo(string bar);

  [Get("/foo/{bar}/{baz}")]
  GetFoo(string bar, string baz);
}

This can then be 'wrapped' in a class which does a string.IsNullOrEmpty(baz) and invokes the correct method. It's not too bad, but having a way to specify an optional path argument would be nice.

RobThree avatar Sep 14 '21 08:09 RobThree

Any news on that?

Hooch180 avatar Aug 02 '24 06:08 Hooch180