letsar / DoLess.UriTemplates

.Net Standard implementation of the URI Template Spec https://tools.ietf.org/html/rfc6570
MIT License
14 stars 1 forks source link

Multiple queries not merged on expand to string #2

Open DoCode opened 6 years ago

DoCode commented 6 years ago

Steps to reproduce

Expected behavior

Actual behavior

letsar commented 6 years ago

Hi @DoCode. I didn't designed the partial expand to merge such templates. Why do you need such things?

To me, you should have these templates:

You can see here that the ? operator can be transformed to the & operator automatically if we partially expand a {?a,b,c} query with any of the parameter provided.

DoCode commented 6 years ago

Hi @letsar,
thanks for your response. I need such things because I used this in an ASP.NET Core API for HAL links. The UrlHelper creates the links as a preprocessor for specified routes and after that, your lib expands this strings with RFC6570 templates. The best would be when the library worked out of the box with the UrlHelper ;-)

letsar commented 6 years ago

@DoCode thanks for this explanation. So if I understand correctly the UrlHelper creates url like "https://localhost:5000/api/resource{?key,value}{?id,name}"?

DoCode commented 6 years ago

@letsar sorry for the delay. No, I generate the links first, with the UrlHelper, based on the current request/response and then append the RFC6570 template parts with your libs.

letsar commented 6 years ago

Ok so you generate the part {?key,value}{?id,name}? Why don't you generate {?key,value,id,name} instead ?

DoCode commented 6 years ago

The problem is, that I don't know if the existing (pre-rendered via UrlHelper) already contains a query. Like this code snippet:

protected override Task EnrichModel(INameBaseModel content, IUrlHelper urlHelper)
{
    RouteValueDictionary routeData = urlHelper.GetRouteDataWithQuery();

    ...

    new Link(
             HttpActionVerb.PUT,
             UriTemplate.For(urlHelper.Link(urlHelper.ActionContext.ActionDescriptor.AttributeRouteInfo.Name, routeData) + "{&value}")
                        .WithPartialExpand()
                        .ExpandToString(),
             RelationType.Self
            );

    ...
}
letsar commented 6 years ago

Ok I understand now. UriTemplate must be a valid uri in the first place, so you will have to handle it yourself. If you want you can do that with an extension method and submit a pull request, so that it will be available for other people.