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

Form property aliasing #55

Merged
merged 2 commits into from
Oct 12, 2014
Merged
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
4 changes: 2 additions & 2 deletions Refit-Tests/Refit-Tests-Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="packages.Refit-Tests-Net45.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand All @@ -105,4 +105,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
29 changes: 28 additions & 1 deletion Refit-Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ public interface IDummyHttpApi
Task FetchSomeStuffWithVoid();

[Post("/foo/bar/{id}")]
Task<string> PostSomeUrlEncodedStuff(int id, [Body(BodySerializationMethod.UrlEncoded)] object content);
Task<string> PostSomeUrlEncodedStuff(int id, [Body(BodySerializationMethod.UrlEncoded)] object content);

[Post("/foo/bar/{id}")]
Task<string> PostSomeAliasedUrlEncodedStuff(int id,[Body(BodySerializationMethod.UrlEncoded)] SomeRequestData content);

string SomeOtherMethod();

Expand All @@ -272,6 +275,12 @@ public interface IDummyHttpApi
Task<string> PutSomeStuffWithDynamicContentType(int id, [Body] string content, [Header("Content-Type")] string contentType);
}

public class SomeRequestData
{
[AliasAs("rpn")]
public int ReadablePropertyName { get; set; }
}

public class TestHttpMessageHandler : HttpMessageHandler
{
public HttpRequestMessage RequestMessage { get; private set; }
Expand Down Expand Up @@ -519,5 +528,23 @@ public async Task BodyContentGetsUrlEncoded()

Assert.AreEqual("Foo=Something&Bar=100&Baz=", content);
}

[Test]
public async Task FormFieldGetsAliased()
{
var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi));
var factory = fixture.BuildRequestFactoryForMethod("PostSomeAliasedUrlEncodedStuff");
var output = factory(
new object[] {
6,
new SomeRequestData {
ReadablePropertyName = 99
}
});

string content = await output.Content.ReadAsStringAsync();

Assert.AreEqual("rpn=99", content);
}
}
}
5 changes: 5 additions & 0 deletions Refit-Tests/packages.Refit-Tests-Net45.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
<package id="Microsoft.Bcl.Build" version="1.0.8" />
<package id="Microsoft.Net.Http" version="2.2.13" />
<package id="NUnit" version="2.6.2" />
<package id="Rx-Core" version="2.2.5" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="net45" />
<package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
<package id="Rx-Main" version="2.2.5" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
</packages>
2 changes: 1 addition & 1 deletion Refit/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public BodyAttribute(BodySerializationMethod serializationMethod = BodySerializa
}
}

[AttributeUsage(AttributeTargets.Parameter)]
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
public class AliasAsAttribute : Attribute
{
public string Name { get; protected set; }
Expand Down
10 changes: 9 additions & 1 deletion Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ public FormValueDictionary(object source)
}

foreach (var property in propertyCache[type]) {
Add(property.Name, string.Format("{0}", property.GetValue(source, null)));
Add(getFieldNameForProperty(property), string.Format("{0}", property.GetValue(source, null)));
}
}
}
Expand All @@ -614,5 +614,13 @@ PropertyInfo[] getProperties(Type type)
.Where(p => p.CanRead)
.ToArray();
}

string getFieldNameForProperty(PropertyInfo propertyInfo)
{
var aliasAttr = propertyInfo.GetCustomAttributes(true)
.OfType<AliasAsAttribute>()
.FirstOrDefault();
return aliasAttr != null ? aliasAttr.Name : propertyInfo.Name;
}
}
}
5 changes: 5 additions & 0 deletions packages/repositories.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
<repository path="../Refit/packages.Refit-Monomac.config" />
<repository path="../Refit-Tests/packages.config" />
<repository path="../Refit-Tests/packages.Refit-Tests-Android.config" />
<repository path="..\Refit\packages.Refit-Net45.config" />
<repository path="..\Refit\packages.Refit-Portable.config" />
<repository path="..\Refit\packages.Refit-sl5.config" />
<repository path="..\Refit\packages.Refit-wp8.config" />
<repository path="..\Refit-Tests\packages.Refit-Tests-Net45.config" />
</repositories>