Code Monkey home page Code Monkey logo

Comments (4)

MrFoxPro avatar MrFoxPro commented on August 18, 2024

I think function should return res.Data[0].ToObject<T>();, not the (T)(object)res.Data[0];

from rethinkdb.driver.

MrFoxPro avatar MrFoxPro commented on August 18, 2024

So complicated. I tried to repeat this code with boxing/unboxing, converting to jobject and back. But ids equals anyway.

from rethinkdb.driver.

MrFoxPro avatar MrFoxPro commented on August 18, 2024

Oh. Seems like i understood wrong this function... Insert method wouldn't return object. But why i get default(T)?

from rethinkdb.driver.

bchavez avatar bchavez commented on August 18, 2024

Hi Dmitriy,

You need to use .RunWrite(conn) instead. Please be confident that you see a problem in the driver source code before posting a GitHub issue for help. Supposing a problem exists is not sufficient for posting issues. If you think you might need help, please use the public help Slack and Discord channels.

The code in the driver that you referenced above is correct.

The only time (T)(object)res.Data[0]; is called is if T is typeof(T).IsJToken(); and in this case, T is State not a JObject/JArray/JValue/JToken.

Because T is State the code res.Data[0].ToObject<T>(Converter.Serializer); is executed as expected.

The reason you are receiving an empty State object that looks like default(T) is that you're deserializing JSON response data with the wrong type of T.

For example, this is what you're doing:

public class ServerWriteResponse{
  public int Inserted {get;set;}
  public int Deleted {get;set;}
}

public class State{
  public string SomeProperty {get;set;}
  public string Id {get;set;}
}

The response from for the table.Insert(state) is:

{
  "inserted": 1,
  "deleted": 0
}

Effectively, when you specify .RunAtom<State>(conn) you are saying to the driver the following:

JsonConvert.Deseralize<State>("{'inserted':1,'deleted':0}").Id != state.Id

The code above does not make any sense because the JSON insert response does not match State type. This is why you get an object that looks like default(T). The JSON data does not match the type T specified.

The correct usage is as follows:

[TestFixture]
public class Issue147
{
   public class State
   {
      [JsonProperty("id", DefaultValueHandling = DefaultValueHandling.Ignore)]
      public string Id { get; set; }
      public string someProperty { get; set; }
      protected internal State()
      {
         this.Id = Guid.NewGuid().ToString();
      }
 
      [JsonConstructor]
      public State(string someProperty)
      {
      }
   }
 
   [Test]
   public void Test()
   {
      var conn = R.Connection().Connect();
      var s = new State();
      var table = R.Db("query").Table("test");
      var result = table.Insert(s).RunWrite(conn);
      result.Inserted.Should().Be(1);
   }
}

Please remember, if you don't have 100% confidence there is an issue with driver code, please ask for help in the chat channels. I will try to answer any questions you have when I have time.

Thanks,
Brian Chavez

from rethinkdb.driver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.