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

Fix Json deserializion of string containing hyphens to List<string> property #1094

Merged
merged 1 commit into from
Feb 11, 2016
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
29 changes: 29 additions & 0 deletions Octokit.Tests/SimpleJsonSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Octokit.Helpers;
using Octokit.Internal;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
Expand Down Expand Up @@ -119,6 +120,34 @@ public void DeserializesEventInfosWithUnderscoresInName()
new SimpleJsonSerializer().Deserialize<EventInfo>(json);
}

public class MessageSingle
{
public string Message { get; private set; }
}

[Fact]
public void DeserializesStringsWithHyphensAndUnderscoresIntoString()
{
const string json = @"{""message"":""-my-test-string_with_underscores_""}";

var response = new SimpleJsonSerializer().Deserialize<MessageSingle>(json);
Assert.Equal("-my-test-string_with_underscores_", response.Message);
}

public class MessageList
{
public IReadOnlyList<string> Message { get; private set; }
}

[Fact]
public void DeserializesStringsWithHyphensAndUnderscoresIntoStringList()
{
const string json = @"{""message"":""-my-test-string_with_underscores_""}";

var response = new SimpleJsonSerializer().Deserialize<MessageList>(json);
Assert.Equal("-my-test-string_with_underscores_", response.Message[0]);
}

[Fact]
public void UnderstandsRubyCasing()
{
Expand Down
1 change: 0 additions & 1 deletion Octokit/Http/SimpleJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public override object DeserializeObject(object value, Type type)
var jsonValue = value as JsonObject;
if (stringValue != null)
{
stringValue = stringValue.Replace("-", "");
if (ReflectionUtils.GetTypeInfo(type).IsEnum)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reservation I had was that this value was being used elsewhere and some enum values depended on this, but then I saw those places were calling RemoveHyphenAndUnderscore and saw it had us covered:

static string RemoveHyphenAndUnderscore(string stringValue)
{
    // remove '-' from values coming in to be able to enum utf-8
    stringValue = stringValue.Replace("-", "");
    // remove '-' from values coming in to be able to enum EventInfoState names with underscores in them. Like "head_ref_deleted" 
    stringValue = stringValue.Replace("_", "");
    return stringValue;
}

{
// remove '-' from values coming in to be able to enum utf-8
Expand Down