Open DoCode opened 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:
string uriString = UriTemplate.For("https://localhost:5000/api/resource{?key,value,id,name}")
.WithParameter("id", 10)
.WithPartialExpand()
.ExpandToString();
uriString.ShouldBeEquivalentTo("https://localhost:5000/api/resource?id=10{&key,value,name}");
string uriString = UriTemplate.For("https://localhost:5000/api/resource?key=mykey&value=myValue{&id,name}")
.WithParameter("id", 10)
.WithPartialExpand()
.ExpandToString();
uriString.ShouldBeEquivalentTo("https://localhost:5000/api/resource?key=mykey&value=myValue&id=10{&name}");
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.
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 ;-)
@DoCode thanks for this explanation. So if I understand correctly the UrlHelper
creates url like "https://localhost:5000/api/resource{?key,value}{?id,name}"
?
@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.
Ok so you generate the part {?key,value}{?id,name}
? Why don't you generate {?key,value,id,name}
instead ?
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
);
...
}
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.
Steps to reproduce
Expected behavior
or
Actual behavior