From 0188238cb584c7e06973f3eaf7ef1103326bba6a Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 7 Mar 2016 11:55:16 +0200 Subject: [PATCH 1/3] #1107 Do no set visibility and affiliation value to avoid mixing with type --- Octokit/Models/Request/RepositoryRequest.cs | 12 ++++++++++++ Octokit/Models/Request/RequestParameters.cs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Octokit/Models/Request/RepositoryRequest.cs b/Octokit/Models/Request/RepositoryRequest.cs index 1f5d2e8907..aae2b8972b 100644 --- a/Octokit/Models/Request/RepositoryRequest.cs +++ b/Octokit/Models/Request/RepositoryRequest.cs @@ -125,6 +125,12 @@ public enum RepositorySort /// public enum RepositoryVisibility { + /// + /// Default value (all) + /// + [Parameter(Value = "")] + Default, + /// /// Returns only public repositories /// @@ -146,6 +152,12 @@ public enum RepositoryVisibility /// public enum RepositoryAffiliation { + /// + /// Default value (all) + /// + [Parameter(Value = "")] + Default, + /// /// Repositories that are owned by the authenticated user /// diff --git a/Octokit/Models/Request/RequestParameters.cs b/Octokit/Models/Request/RequestParameters.cs index fa689f8c76..4fe403f2d0 100644 --- a/Octokit/Models/Request/RequestParameters.cs +++ b/Octokit/Models/Request/RequestParameters.cs @@ -33,7 +33,7 @@ public virtual IDictionary ToParametersDictionary() return (from property in map let value = property.GetValue(this) let key = property.Key - where value != null + where !String.IsNullOrEmpty(value) select new { key, value }).ToDictionary(kvp => kvp.key, kvp => kvp.value); } From f50ccf77b1fd1491a8179ee1ae86c2d7a7476625 Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 8 Mar 2016 15:57:55 +0200 Subject: [PATCH 2/3] #1107 Make RepositoryRequest properties nullable and add tests --- .../Models/RepositoryRequestTests.cs | 50 +++++++++++++++++++ Octokit.Tests/Octokit.Tests.csproj | 1 + Octokit/Models/Request/RepositoryRequest.cs | 22 ++------ Octokit/Models/Request/RequestParameters.cs | 5 +- 4 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 Octokit.Tests/Models/RepositoryRequestTests.cs diff --git a/Octokit.Tests/Models/RepositoryRequestTests.cs b/Octokit.Tests/Models/RepositoryRequestTests.cs new file mode 100644 index 0000000000..e5d89562fc --- /dev/null +++ b/Octokit.Tests/Models/RepositoryRequestTests.cs @@ -0,0 +1,50 @@ +using Xunit; + +namespace Octokit.Tests.Models +{ + public class RepositoryRequestTests + { + public class TheToParametersDictionaryMethod + { + [Fact] + public void ContainsSetValues() + { + var request = new RepositoryRequest + { + Type = RepositoryType.All, + Sort = RepositorySort.FullName, + Direction = SortDirection.Ascending + }; + + var parameters = request.ToParametersDictionary(); + + Assert.Equal(3, parameters.Count); + Assert.Equal("all", parameters["type"]); + Assert.Equal("full_name", parameters["sort"]); + Assert.Equal("asc", parameters["direction"]); + + request = new RepositoryRequest + { + Affiliation = RepositoryAffiliation.All, + Visibility = RepositoryVisibility.Public + }; + + parameters = request.ToParametersDictionary(); + + Assert.Equal(2, parameters.Count); + Assert.Equal("owner, collaborator, organization_member", parameters["affiliation"]); + Assert.Equal("public", parameters["visibility"]); + } + + [Fact] + public void DoesNotReturnValuesForDefaultRequest() + { + var request = new RepositoryRequest(); + + var parameters = request.ToParametersDictionary(); + + Assert.Empty(parameters); + } + } + } +} diff --git a/Octokit.Tests/Octokit.Tests.csproj b/Octokit.Tests/Octokit.Tests.csproj index 99537721eb..5c6484c309 100644 --- a/Octokit.Tests/Octokit.Tests.csproj +++ b/Octokit.Tests/Octokit.Tests.csproj @@ -182,6 +182,7 @@ + diff --git a/Octokit/Models/Request/RepositoryRequest.cs b/Octokit/Models/Request/RepositoryRequest.cs index aae2b8972b..2f751f62a0 100644 --- a/Octokit/Models/Request/RepositoryRequest.cs +++ b/Octokit/Models/Request/RepositoryRequest.cs @@ -19,7 +19,7 @@ public class RepositoryRequest : RequestParameters /// The type. /// [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")] - public RepositoryType Type { get; set; } + public RepositoryType? Type { get; set; } /// /// Gets or sets the sort property. @@ -27,7 +27,7 @@ public class RepositoryRequest : RequestParameters /// /// The sort. /// - public RepositorySort Sort { get; set; } + public RepositorySort? Sort { get; set; } /// /// Gets or sets the sort direction. @@ -35,7 +35,7 @@ public class RepositoryRequest : RequestParameters /// /// The direction. /// - public SortDirection Direction { get; set; } + public SortDirection? Direction { get; set; } /// /// Gets or sets the visibility property. @@ -43,7 +43,7 @@ public class RepositoryRequest : RequestParameters /// /// The visibility. /// - public RepositoryVisibility Visibility { get; set; } + public RepositoryVisibility? Visibility { get; set; } /// /// Gets or sets the affiliation property. @@ -51,7 +51,7 @@ public class RepositoryRequest : RequestParameters /// /// The affiliation. /// - public RepositoryAffiliation Affiliation { get; set; } + public RepositoryAffiliation? Affiliation { get; set; } internal string DebuggerDisplay { @@ -125,12 +125,6 @@ public enum RepositorySort /// public enum RepositoryVisibility { - /// - /// Default value (all) - /// - [Parameter(Value = "")] - Default, - /// /// Returns only public repositories /// @@ -152,12 +146,6 @@ public enum RepositoryVisibility /// public enum RepositoryAffiliation { - /// - /// Default value (all) - /// - [Parameter(Value = "")] - Default, - /// /// Repositories that are owned by the authenticated user /// diff --git a/Octokit/Models/Request/RequestParameters.cs b/Octokit/Models/Request/RequestParameters.cs index 4fe403f2d0..ccd7121ca9 100644 --- a/Octokit/Models/Request/RequestParameters.cs +++ b/Octokit/Models/Request/RequestParameters.cs @@ -33,7 +33,7 @@ public virtual IDictionary ToParametersDictionary() return (from property in map let value = property.GetValue(this) let key = property.Key - where !String.IsNullOrEmpty(value) + where value != null select new { key, value }).ToDictionary(kvp => kvp.key, kvp => kvp.value); } @@ -49,6 +49,9 @@ static List GetPropertyParametersForType(Type type) Justification = "GitHub API depends on lower case strings")] static Func GetValueFunc(Type propertyType) { + // get underlying type if nullable + propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType; + if (typeof(IEnumerable).IsAssignableFrom(propertyType)) { return (prop, value) => From 7866f3cbd191bba8013baff3ddf2999f52b40f80 Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 8 Mar 2016 20:04:24 +0200 Subject: [PATCH 3/3] Update DebuggerDisplay in RepositoryRequest --- Octokit/Models/Request/RepositoryRequest.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Octokit/Models/Request/RepositoryRequest.cs b/Octokit/Models/Request/RepositoryRequest.cs index 2f751f62a0..03753b432e 100644 --- a/Octokit/Models/Request/RepositoryRequest.cs +++ b/Octokit/Models/Request/RepositoryRequest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; @@ -57,7 +58,19 @@ internal string DebuggerDisplay { get { - return string.Format(CultureInfo.InvariantCulture, "Type: {0}, Sort: {1}, Direction: {2}", Type, Sort, Direction); + var propValues = new List(); + if (Type.HasValue) + propValues.Add(string.Format(CultureInfo.InvariantCulture, "Type: {0}", Type)); + if (Sort.HasValue) + propValues.Add(string.Format(CultureInfo.InvariantCulture, "Sort: {0}", Sort)); + if (Direction.HasValue) + propValues.Add(string.Format(CultureInfo.InvariantCulture, "Direction: {0}", Direction)); + if (Visibility.HasValue) + propValues.Add(string.Format(CultureInfo.InvariantCulture, "Visibility: {0}", Visibility)); + if (Affiliation.HasValue) + propValues.Add(string.Format(CultureInfo.InvariantCulture, "Affiliation: {0}", Affiliation)); + + return string.Join(", ", propValues); } } }