Bug #82033 DbDoc Fails to produce valid JSON
Submitted: 28 Jun 2016 12:31 Modified: 5 Jul 2017 15:55
Reporter: Richard Pope Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:7.0.3 OS:Windows (7)
Assigned to: Roberto Ezequiel Garcia Ballesteros CPU Architecture:Any
Tags: C#, json, XDevAPI

[28 Jun 2016 12:31] Richard Pope
Description:
Provide valid JSON including an array of objects to the DbDoc constructor (or the Collection.Add method directly)

The Add method will fail owing to an invalid reserialization of the JSON

How to repeat:
Execute the following C# code utilising the MySQL Connector/NET:

XSession session = MySQLX.GetSession("server={server};port={port};uid={username};password={password}");
Schema schema = session.GetSchema("{schema_name}");
Collection col = schema.GetCollection("{collection_name}");
DbDoc dbdoc = new DbDoc(@"{""values"":[{""a"":""b""}]}");
col.Add(dbdoc).Execute();

Suggested fix:
Accept given JSON rather than deserialising as DbDoc and calling .ToString()
-- or --
When parsing the JSON, create a new DbDoc object for each non-primitive object encountered such that the .ToString() call doesn't break
[29 Jun 2016 5:56] Chiranjeevi Battula
Hello Richard Pope,

Thank you for the bug report.
I could not repeat the issue at our end using with Visual Studio 2013, Connector/NET 7.0.3.
Could you please provide repeatable test case (create table statements, data, sample code etc. - please make it as private if you prefer) to confirm this 

issue at our end?

X DevAPI functionality:
https://dev.mysql.com/doc/dev/connector-net/html/connector-net-x-devapi-getting-started.ht...

Thanks,
Chiranjeevi.
[29 Jun 2016 7:47] Richard Pope
I set up a DBA user with the name "user" and password "pass" and ran the following program. No other database setup was performed.
This program was written and executed in VS2015. Target framework 4.5.2; Output type: Console Application. Using MySQL version 5.7.13-log Community Server x86_64
There were no entries in the server error log near the time of execution

---- C# Program ----
using MySqlX.XDevAPI;

namespace XDocInterfacing
{
    class Program
    {

        static void Main(string[] args)
        {
            var server = "localhost";
            var port = 33060;
            var username = "user";
            var password = "pass";
            var schema_name = "blank_db";
            var collection_name = "blank_col";

            XSession session = MySQLX.GetSession($"server={server};port={port};uid={username};password={password}");
            session.DropSchema(schema_name);
            Schema schema = session.CreateSchema(schema_name);
            Collection col = schema.CreateCollection(collection_name);
            DbDoc dbdoc = new DbDoc(@"{""values"":[{""a"":""b""}]}");
            col.Add(dbdoc).Execute();
        }
    }
}
---- End C# Program ----

The Execute() function fires an exception (see below). It is notable that the .ToString() representation of the DbDoc is as follows (clearly showing the invalid serialisation)

---- DbDoc value ----
{{"values":System.Collections.Generic.Dictionary`2[System.String,System.Object][], "_id":"2a209016a4f64fab85142964f9adfac2"}}
---- End DbDoc Value ----

---- Exception Trace ----
MySql.Data.MySqlClient.MySqlException was unhandled
  Code=3140
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Invalid JSON text: "Invalid value." at position 10 in value for column 'blank_col.doc'.
  Number=0
  Source=MySql.Data
  SqlState=22032
  StackTrace:
       at MySqlX.Protocol.XProtocol.DecodeAndThrowError(CommunicationPacket p)
       at MySqlX.Protocol.XProtocol.CloseResult(BaseResult rs)
       at MySqlX.Session.XInternalSession.Insert(Collection collection, DbDoc[] json, List`1 newIds)
       at MySqlX.XDevAPI.CRUD.AddStatement.Execute()
       at XDocInterfacing.Program.Main(String[] args) in c:\Projects\XDocInterfacing\XDocInterfacing\Program.cs:line 22
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
---- End Exception Trace ----
[29 Jun 2016 8:53] Chiranjeevi Battula
Hello Richard Pope,

Thank you for your feedback and test case.
Verified this behavior on Visual Studio 2013 (C#.Net) and Connector/NET 7.0.3 version.

Thanks,
Chiranjeevi.
[29 Jun 2016 8:53] Chiranjeevi Battula
Screenshot

Attachment: 82033.JPG (image/jpeg, text), 225.43 KiB.

[29 Jun 2016 8:54] Chiranjeevi Battula
StackTrace:
   at MySqlX.Protocol.XProtocol.DecodeAndThrowError(CommunicationPacket p)
   at MySqlX.Protocol.XProtocol.CloseResult(BaseResult rs)
   at MySqlX.XDevAPI.Common.Result..ctor(InternalSession session)
   at MySqlX.Session.XInternalSession.Insert(Collection collection, DbDoc[] json, List`1 newIds)
   at MySqlX.XDevAPI.CRUD.AddStatement.Execute()
   at _82033.Form1.button2_Click(Object sender, EventArgs e) in d:\Projects\82033\82033\Form1.cs:line 80
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at _82033.Program.Main() in d:\Projects\82033\82033\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
[5 Jul 2017 15:55] Jose Ramirez
Thank you for your bug report. This issue was fixed in 7.0.4 version of the product.