<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:36:13 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CSHARP-236] Can&apos;t map an user defined type (class or struct) to a bson value</title>
                <link>https://jira.mongodb.org/browse/CSHARP-236</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;In my project I&apos;m using a user defined struct named Identity as id&apos;s type. So I implemented a BsonSerializer and an IIDGenerator. Then I registered them using the lines below:&lt;/p&gt;

&lt;p&gt;     BsonSerializer.RegisterSerializer(typeof(Identity), new IdentitySerializer());&lt;br/&gt;
     BsonSerializer.RegisterIdGenerator(typeof(Identity?), new IdentityGenerator());&lt;/p&gt;

&lt;p&gt;Then I defined an Order class the uses the Identity type:&lt;/p&gt;

&lt;p&gt;    public class Order&lt;br/&gt;
    {&lt;br/&gt;
        public Identity? Id &lt;/p&gt;
{ get; set; }&lt;br/&gt;
        public decimal Amount { get; set; }
&lt;p&gt;    }&lt;/p&gt;

&lt;p&gt;If I save a new Order into a collection, everything run well.&lt;br/&gt;
But when I want to save an existing Order an ArgumentException is being thrown with this message: &quot;.NET type MongoDriverBug.Identity cannot be mapped to a BsonValue&quot;&lt;/p&gt;

&lt;p&gt;So I did some debug and found that the exception was thrown by the method below:&lt;/p&gt;

&lt;p&gt;        public static BsonValue MapToBsonValue(object value) {&lt;br/&gt;
            BsonValue bsonValue;&lt;br/&gt;
            if (TryMapToBsonValue(value, out bsonValue)) &lt;/p&gt;
{
                return bsonValue;
            }&lt;br/&gt;
&lt;br/&gt;
            var message = string.Format(&quot;.NET type {0} cannot be mapped to a BsonValue&quot;, value.GetType().FullName);&lt;br/&gt;
            throw new ArgumentException(message);&lt;br/&gt;
        }&lt;br/&gt;
&lt;br/&gt;
I added some lines into the method to workaround the problem. The workaround tries to find a serializer for the value&apos;s type and if a serializer does exist, then it uses the serializer to convert the value to a valid BsonValue.&lt;br/&gt;
&lt;br/&gt;
        public static BsonValue MapToBsonValue(object value) {&lt;br/&gt;
            BsonValue bsonValue;&lt;br/&gt;
            if (TryMapToBsonValue(value, out bsonValue)) {                return bsonValue;            }

&lt;p&gt;            // ---------------------------------------------------------&lt;br/&gt;
            // WORKAROUND START&lt;br/&gt;
            var type = value.GetType();&lt;br/&gt;
            var serializer = BsonSerializer.LookupSerializer(type);&lt;br/&gt;
            if (serializer != null)&lt;br/&gt;
            {&lt;br/&gt;
                using (var stream = new MemoryStream())&lt;br/&gt;
                {&lt;br/&gt;
                    using (var writer = BsonWriter.Create(stream))&lt;br/&gt;
                    {&lt;br/&gt;
                        writer.WriteStartDocument();&lt;br/&gt;
                        writer.WriteName(&quot;x&quot;);&lt;br/&gt;
                        serializer.Serialize(writer, type, value, null);&lt;br/&gt;
                        writer.WriteEndDocument();&lt;br/&gt;
                        writer.Flush();&lt;/p&gt;

&lt;p&gt;                        stream.Seek(0, SeekOrigin.Begin);&lt;/p&gt;

&lt;p&gt;                        using (var reader = BsonReader.Create(stream))&lt;/p&gt;
                        {
                            reader.ReadStartDocument();
                            reader.ReadName();
                            return BsonValue.ReadFrom(reader);
                        }
&lt;p&gt;                    }&lt;br/&gt;
                }&lt;br/&gt;
            }&lt;br/&gt;
            // WORKAROUND END&lt;br/&gt;
            // ---------------------------------------------------------&lt;/p&gt;

&lt;p&gt;            var message = string.Format(&quot;.NET type &lt;/p&gt;
{0}
&lt;p&gt; cannot be mapped to a BsonValue&quot;, value.GetType().FullName);&lt;br/&gt;
            throw new ArgumentException(message);&lt;br/&gt;
        }&lt;/p&gt;
</description>
                <environment>Windows 7</environment>
        <key id="17817">CSHARP-236</key>
            <summary>Can&apos;t map an user defined type (class or struct) to a bson value</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="3">Duplicate</resolution>
                                        <assignee username="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="silviom">Silvio Massari</reporter>
                        <labels>
                            <label>bson</label>
                            <label>serialization</label>
                            <label>userDefined</label>
                    </labels>
                <created>Thu, 2 Jun 2011 18:58:45 +0000</created>
                <updated>Thu, 2 Apr 2015 18:27:43 +0000</updated>
                            <resolved>Thu, 2 Jun 2011 19:25:47 +0000</resolved>
                                    <version>1.0</version>
                                    <fixVersion>1.1</fixVersion>
                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="35808" author="silviom" created="Thu, 2 Jun 2011 19:51:49 +0000"  >&lt;p&gt;Hi Robert.&lt;br/&gt;
Yes, it is fixez at master branch.&lt;br/&gt;
Sorry I didn&apos;t find that the issue was already reported.&lt;br/&gt;
Thanks for your time.&lt;br/&gt;
Silvio&lt;/p&gt;

</comment>
                            <comment id="35800" author="rstam" created="Thu, 2 Jun 2011 19:25:47 +0000"  >&lt;p&gt;I believe this is the same issue as:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-198&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;https://jira.mongodb.org/browse/CSHARP-198&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you test with the latest version of the master branch from github?&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                            <attachment id="11999" name="MongoDriverBug.7z" size="108480" author="silviom" created="Thu, 2 Jun 2011 18:58:45 +0000"/>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrh8w7:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14240</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>