From 83101bcbe9e3935fafffadcaad31f1b849c85064 Mon Sep 17 00:00:00 2001 From: Todd Date: Wed, 26 Jan 2022 19:57:05 -0600 Subject: [PATCH] #669 bug setting query params w/ nullable props --- Test/Flurl.Test/UrlBuilder/UrlBuildingTests.cs | 10 ++++++++++ src/Flurl/Util/CommonExtensions.cs | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/Test/Flurl.Test/UrlBuilder/UrlBuildingTests.cs b/Test/Flurl.Test/UrlBuilder/UrlBuildingTests.cs index 88e9ea66..92caaa56 100644 --- a/Test/Flurl.Test/UrlBuilder/UrlBuildingTests.cs +++ b/Test/Flurl.Test/UrlBuilder/UrlBuildingTests.cs @@ -71,6 +71,16 @@ public void ignores_null_or_empty_query_params(string original) { Assert.AreEqual(original, modified2); } + [Test] // #669 + public void can_set_query_params_using_objects_with_nullable_types() { + int? x = 1; + int? y = null; + var query = new { x, y }; + var url = new Url("https://api.com"); + url.SetQueryParams(query); + Assert.AreEqual("https://api.com?x=1", url.ToString()); + } + [Test] // #632 public void can_set_query_params_to_enums_cast_to_ints() { var enumValues = new[] { FileMode.Append, FileMode.Create }; diff --git a/src/Flurl/Util/CommonExtensions.cs b/src/Flurl/Util/CommonExtensions.cs index ce889fb0..15da5285 100644 --- a/src/Flurl/Util/CommonExtensions.cs +++ b/src/Flurl/Util/CommonExtensions.cs @@ -101,6 +101,10 @@ internal static object GetDeclaredTypeValue(object value, Type declaredType) { if (value == null || value.GetType() == declaredType) return value; + // without this we had https://github.com/tmenier/Flurl/issues/669 + // related: https://stackoverflow.com/q/3531318/62600 + declaredType = Nullable.GetUnderlyingType(declaredType) ?? declaredType; + // added to deal with https://github.com/tmenier/Flurl/issues/632 // thx @j2jensen! if (value is IEnumerable col