8000 Add support for url form encoding collections by MariusVolkhart · Pull Request #584 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for url form encoding collections #584

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

Merged
merged 4 commits into from
Dec 3, 2018
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
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Xunit;

namespace Refit.Tests
{
public class FormValueDictionaryTests
public class FormValueMultimapTests
{
readonly RefitSettings settings = new RefitSettings();

[Fact]
public void EmptyIfNullPassedIn()
{
var target = new FormValueDictionary(null, settings);
var target = new FormValueMultimap(null, settings);
Assert.Empty(target);
}

Expand All @@ -25,7 +26,7 @@ public void LoadsFromDictionary()
{ "xyz", "123" }
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.Equal(source, target);
}
Expand All @@ -44,7 +45,34 @@ public void LoadsFromObject()
{ "B", "2" },
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}

[Fact]
public void LoadFromObjectWithCollections()
{
var source = new ObjectWithRepeatedFieldsTestClass
{
A = new List<int> { 1, 2 },
B = new HashSet<string> { "set1", "set2" },
C = new HashSet<int> { 1, 2 },
D = new List<double> { 0.1, 1.0 },
E = new List<bool> { true, false }
};
var expected = new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("A", "01"),
new KeyValuePair<string, string>("A", "02"),
new KeyValuePair<string, string>("B", "set1,set2"),
new KeyValuePair<string, string>("C", "01 02"),
new KeyValuePair<string, string>("D", "0.10\t1.00"),

// The default behavior is to capitalize booleans. This is not a requirement.
new KeyValuePair<string, string>("E", "True|False")
};

var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand All @@ -56,6 +84,20 @@ public class ObjectTestClass
public string C { get; set; }
}

public class ObjectWithRepeatedFieldsTestClass
{
[Query(CollectionFormat.Multi, Format = "00")]
public IList<int> A { get; set; }
[Query(CollectionFormat.Csv)]
public ISet<string> B { get; set; }
[Query(CollectionFormat.Ssv, Format = "00")]
public HashSet<int> C { get; set; }
[Query(CollectionFormat.Tsv, Format = "0.00")]
public IList<double> D { get; set; }
[Query(CollectionFormat.Pipes)]
public IList<bool> E { get; set; }
}

[Fact]
public void ExcludesPropertiesWithInaccessibleGetters()
{
Expand All @@ -69,7 +111,7 @@ public void ExcludesPropertiesWithInaccessibleGetters()
{ "C", "FooBar" }
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand All @@ -96,7 +138,7 @@ public void LoadsFromAnonymousType()
{ "xyz", "123" }
};

var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);


Assert.Equal(expected, actual);
Expand All @@ -110,11 +152,11 @@ public void UsesAliasAsAttribute()
Foo = "abc"
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Foo", target.Keys);
Assert.Contains("f", target.Keys);
Assert.Equal("abc", target["f"]);
Assert.Equal("abc", target.FirstOrDefault(entry => entry.Key == "f").Value);
}

[Fact]
Expand All @@ -125,11 +167,11 @@ public void UsesJsonPropertyAttribute()
Bar = "xyz"
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Bar", target.Keys);
Assert.Contains("b", target.Keys);
Assert.Equal("xyz", target["b"]);
Assert.Equal("xyz", target.FirstOrDefault(entry => entry.Key == "b").Value);
}

[Fact]
Expand All @@ -140,11 +182,11 @@ public void UsesQueryPropertyAttribute()
Frob = 4
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Bar", target.Keys);
Assert.Contains("prefix-fr", target.Keys);
Assert.Equal("4.0", target["prefix-fr"]);
Assert.Equal("4.0", target.FirstOrDefault(entry => entry.Key == "prefix-fr").Value);
}


Expand All @@ -156,12 +198,12 @@ public void GivesPrecedenceToAliasAs()
Baz = "123"
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.DoesNotContain("Bar", target.Keys);
Assert.DoesNotContain("z", target.Keys);
Assert.Contains("a", target.Keys);
Assert.Equal("123", target["a"]);
Assert.Equal("123", target.FirstOrDefault(entry => entry.Key == "a").Value);
}


Expand All @@ -173,7 +215,7 @@ public void SkipsNullValuesFromDictionary()
{ "xyz", null }
};

var target = new FormValueDictionary(source, settings);
var target = new FormValueMultimap(source, settings);

Assert.Single(target);
Assert.Contains("foo", target.Keys);
Expand All @@ -196,7 +238,7 @@ public void SerializesEnumWithEnumMemberAttribute()
};


var actual = new FormValueDictionary(source, settings);
var actual = new FormValueMultimap(source, settings);

Assert.Equal(expected, actual);
}
Expand Down
18 changes: 17 additions & 1 deletion Refit/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -260,8 +260,24 @@ public QueryAttribute(CollectionFormat collectionFormat)
public string Delimiter { get; protected set; } = ".";
public string Prefix { get; protected set; }

/// <summary>
/// Used to customize the formatting of the encoded value.
/// </summary>
/// <example>
/// <code>
/// interface IServerApi
/// {
/// [Get("/expenses")]
/// Task addExpense([Query(Format="0.00")] double expense);
/// }
/// </code>
/// Calling <c>serverApi.addExpense(5)</c> will result in a URI of <c>{baseUri}/expenses?expense=5.00</c>.
/// </example>
public string Format { get; set; }

/// <summary>
/// Specifies how the collection should be encoded. The default behavior is <c>RefitParameterFormatter</c>.
/// </summary>
public CollectionFormat CollectionFormat { get; set; } = CollectionFormat.RefitParameterFormatter;
}
}
81 changes: 0 additions & 81 deletions Refit/FormValueDictionary.cs

This file was deleted.

Loading
0