简体   繁体   中英

Returning a simple Guid with RestSharp

RestSharp is unable to deserialize a response of a plain Guid.

[Route("User/BeginSession")]
[HttpGet]
public Guid BeginSession()
{
    return Guid.NewGuid();
}

This test fails:

[Fact]
public void BeginSession_MadeAsARestRequest_Succeeds()
{
    var client = new RestClient("http://localhost/xyz");
    var request = new RestRequest("User/BeginSession");

    var a = client.Execute(request);

    var result = client.Execute<Guid>(request).Data;

    Assert.NotEqual(
        Guid.Parse("{00000000-0000-0000-0000-000000000000}"),
        result);
}

I have to wrap the Guid with a class to get it to work:

public class GuidResponse
{
    public Guid Guid { get; set; }
}

[Route("User/BeginSession2")]
[HttpGet]
public GuidResponse BeginSession2()
{
    return new GuidResponse
    {
        Guid = Guid.NewGuid()
    };
}

This test passes:

[Fact]
public void BeginSession2_MadeAsARestRequest_Succeeds()
{
    var client = new RestClient("http://localhost/xyz");
    var request = new RestRequest("User/BeginSession2");

    var result = client.Execute<GuidResponse>(request).Data;

    Assert.NotEqual(
        Guid.Parse("{00000000-0000-0000-0000-000000000000}"),
        result.Guid);
}

is this expected behaviour?

I guess it's because returning just a GUID isn't valid JSON. It would be returned as a string, and not as a JavaScript object, eg { "id": "4bae9421-4fe0-4294-a659-9bc37388344b" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM