Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for FromUri named parameters #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Drum.Tests/UriMakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public void GetPaged(PageInfo page, bool detailed) { }
public void GetById(int id) { }

[Route("{id}")]
public void GetById(int id, bool detailed) { }
public void GetById(int id, bool detailed) { }

[Route("")]
public void GetPagedWithExcplicitName([FromUri(Name = "pager")]PageInfo page) { }

[Route("")]
public void GetPagedWithoutName([FromUri]PageInfo page) { }
}

[Fact]
Expand Down Expand Up @@ -78,6 +84,20 @@ public void Can_make_uri_for_action_with_complex_uri_parameters()
{
var uri = _uriMaker.UriFor(c => c.GetPaged(new PageInfo{page = 2,count = 10}, true));
Assert.Equal("http://example.org/api/UriMakerTests/resources?page=2&count=10&detailed=True", uri.ToString());
}

[Fact]
public void Can_make_uri_for_action_with_complex_named_uri_parameters()
{
var uri = _uriMaker.UriFor(c => c.GetPagedWithExcplicitName(new PageInfo { page = 5, count = 15 }));
Assert.Equal("http://example.org/api/UriMakerTests/resources?pager.page=5&pager.count=15", uri.ToString());
}

[Fact]
public void Can_make_uri_for_action_with_complex_uri_parameters_with_FromUri()
{
var uri = _uriMaker.UriFor(c => c.GetPagedWithoutName(new PageInfo { page = 5, count = 15 }));
Assert.Equal("http://example.org/api/UriMakerTests/resources?page=5&count=15", uri.ToString());
}

public UriMakerTests()
Expand Down
10 changes: 7 additions & 3 deletions src/Drum/DecoratorRouteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ public ParameterHandler(ParameterInfo parameterInfo)
new RouteValueHandler(parameterInfo.Name, v => v)
});
}
else
{
else
{
var fromUriAttributes = parameterInfo.GetCustomAttributes(typeof(FromUriAttribute), false);
var fromUriAttribute = fromUriAttributes.FirstOrDefault() as FromUriAttribute;
var nameFormat = fromUriAttribute != null && !string.IsNullOrEmpty(fromUriAttribute.Name)? string.Format("{0}.{{0}}", fromUriAttribute.Name) : "{0}";

var type = parameterInfo.ParameterType;
var typeDesc = new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
var propDescs = typeDesc.GetProperties();
GetRouteValues = propDescs.OfType<PropertyDescriptor>()
.Select(desc => new RouteValueHandler(desc.Name, desc.GetValue)).ToList();
.Select(desc => new RouteValueHandler(string.Format(nameFormat, desc.Name), desc.GetValue)).ToList();
}

IsFromUri = true;
Expand Down