Files
octokit.net/Octokit.Tests/Models/DeploymentTests.cs
Phil Haack e40c792e27 Change Payload to a Dictionary<string, string> (#2303)
When serializing the `NewDeployment` type, the `Payload` is serialized as an escaped string because JSON.NET doesn't know it's meant to be JSON.

This causes a problem when you call the API because the Payload is supposed to be a JSON dictionary that's just part of the overall payload. It's not supposed to be an escaped string.

That's why the JSON deserializer fails on it. Not only that, any deployments created using the current Octokit.net will create an invalid payload.

This PR fixes it by changing the type of `Payload` to a dictionary. THIS IS A BREAKING CHANGE, but the old behavior was broken so it forces a new correct behavior.

Fixes #2250
2021-02-21 17:11:51 -04:00

69 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Octokit.Internal;
using Xunit;
namespace Octokit.Tests.Models
{
public class DeploymentTests
{
[Fact]
public void CanSerialize()
{
var deployment = new NewDeployment("ref")
{
Payload = new Dictionary<string, string> {{"environment", "production"}}
};
var deserialized = new SimpleJsonSerializer().Serialize(deployment);
Assert.Equal(@"{""ref"":""ref"",""task"":""deploy"",""payload"":{""environment"":""production""}}", deserialized);
}
[Fact]
public void CanDeserialize()
{
const string json = @"{
""id"": 1,
""sha"": ""topic-branch"",
""url"": ""https://api.github.com/repos/octocat/example/deployments/1"",
""creator"": {
""login"": ""octocat"",
""id"": 1,
""avatar_url"": ""https://github.com/images/error/octocat_happy.gif"",
""gravatar_id"": ""somehexcode"",
""url"": ""https://api.github.com/users/octocat"",
""html_url"": ""https://github.com/octocat"",
""followers_url"": ""https://api.github.com/users/octocat/followers"",
""following_url"": ""https://api.github.com/users/octocat/following{/other_user}"",
""gists_url"": ""https://api.github.com/users/octocat/gists{/gist_id}"",
""starred_url"": ""https://api.github.com/users/octocat/starred{/owner}{/repo}"",
""subscriptions_url"": ""https://api.github.com/users/octocat/subscriptions"",
""organizations_url"": ""https://api.github.com/users/octocat/orgs"",
""repos_url"": ""https://api.github.com/users/octocat/repos"",
""events_url"": ""https://api.github.com/users/octocat/events{/privacy}"",
""received_events_url"": ""https://api.github.com/users/octocat/received_events"",
""type"": ""User"",
""site_admin"": false
},
""payload"": { ""environment"":""production""},
""created_at"": ""2012-07-20T01:19:13Z"",
""updated_at"": ""2012-07-20T01:19:13Z"",
""description"": ""Deploy request from hubot"",
""statuses_url"": ""https://api.github.com/repos/octocat/example/deployments/1/statuses""
}";
var actual = new SimpleJsonSerializer().Deserialize<Deployment>(json);
Assert.Equal(1, actual.Id);
Assert.Equal("topic-branch", actual.Sha);
Assert.Equal("https://api.github.com/repos/octocat/example/deployments/1", actual.Url);
Assert.Equal(new ReadOnlyDictionary<string, string>(new Dictionary<string, string> { { "environment", "production" } }), actual.Payload);
Assert.Equal(DateTimeOffset.Parse("2012-07-20T01:19:13Z"), actual.CreatedAt);
Assert.Equal(DateTimeOffset.Parse("2012-07-20T01:19:13Z"), actual.UpdatedAt);
Assert.Equal("Deploy request from hubot", actual.Description);
Assert.Equal("https://api.github.com/repos/octocat/example/deployments/1/statuses", actual.StatusesUrl);
}
}
}