<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:48:37 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-4567] Support projections where the lambda body returns a type different from but assignable to the lambda return type</title>
                <link>https://jira.mongodb.org/browse/CSHARP-4567</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;h4&gt;&lt;a name=&quot;Summary&quot;&gt;&lt;/a&gt;Summary&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Projection LINQ expressions dynamically created throw InvalidCastException when the projection is applied to IFluentFind and executed in &lt;b&gt;C# driver version 2.19.0&lt;/b&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Exception Information:&lt;/p&gt;

&lt;p&gt;Unable to cast object of type &apos;MongoDB.Bson.Serialization.BsonClassMapSerializer`1&lt;span class=&quot;error&quot;&gt;&amp;#91;DynamicType0`2&amp;#93;&lt;/span&gt;&apos; to type &apos;MongoDB.Bson.Serialization.IBsonSerializer`1&lt;span class=&quot;error&quot;&gt;&amp;#91;System.Object&amp;#93;&lt;/span&gt;&apos;.&lt;/p&gt;

&lt;p&gt;&#160; &#160;at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToProjection&lt;span class=&quot;error&quot;&gt;&amp;#91;TInput,TOutput&amp;#93;&lt;/span&gt;(Expression`1 expression, IBsonSerializer`1 inputSerializer, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)&lt;br/&gt;
&#160; &#160;at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToFindProjection&lt;span class=&quot;error&quot;&gt;&amp;#91;TSource,TProjection&amp;#93;&lt;/span&gt;(Expression`1 expression, IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)&lt;br/&gt;
&#160; &#160;at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation&lt;span class=&quot;error&quot;&gt;&amp;#91;TProjection&amp;#93;&lt;/span&gt;(FilterDefinition`1 filter, FindOptions`2 options)&lt;br/&gt;
&#160; &#160;at MongoDB.Driver.MongoCollectionImpl`1.FindAsync&lt;span class=&quot;error&quot;&gt;&amp;#91;TProjection&amp;#93;&lt;/span&gt;(IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)&lt;br/&gt;
&#160; &#160;at MongoDB.Driver.MongoCollectionImpl`1.&amp;lt;&amp;gt;c_&lt;em&gt;DisplayClass48_0`1.&amp;lt;FindAsync&amp;gt;b&lt;/em&gt;_0(IClientSessionHandle session)&lt;br/&gt;
&#160; &#160;at MongoDB.Driver.MongoCollectionImpl`1.&amp;lt;UsingImplicitSessionAsync&amp;gt;d__107`1.MoveNext()&lt;br/&gt;
&#160; &#160;at MongoDB.Driver.IAsyncCursorSourceExtensions.&amp;lt;ToListAsync&amp;gt;d__16`1.MoveNext()&lt;br/&gt;
&#160; &#160;at Program.&amp;lt;&amp;lt;Main&amp;gt;$&amp;gt;d__0.MoveNext() in C:\...\repos\MongoDB.Projection.Test\MongoDB.Projection.Test\Program.cs:line 52&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;HowtoReproduce&quot;&gt;&lt;/a&gt;How to Reproduce&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Download the attached VS solution and run the project. The code contains comments with information regarding the issue.&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;AdditionalBackground&quot;&gt;&lt;/a&gt;Additional Background&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;The projection is created using a dynamically generated System.Type via IL (see attached solution). The type is similar to an anonymous type containing a subset of the actual properties defined on a specific type. The properties can be any combination of properties from the source type and are verified against the source type preventing invalid properties from being&lt;/em&gt; &lt;em&gt;added to the projection.&lt;/em&gt;&#160;&lt;em&gt;Once the projection Type is generated it is then used in the creation of a dynamic LINQ expression that represents the actual projection applied to the IFluentFind.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As mentioned, the projection is similar to a projection created using an anonymous type. The anonymous type projection does not throw the exception though.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The implementation described here has been in use for quite some time in our systems and has worked quite well in dozens of microservices deployed in the US and Europe. The issue was only realized recently when we updated to driver version &lt;b&gt;v2.19.0&lt;/b&gt;. This implementation is part of a larger framework that is also used outside of MongoDB so this is a bit of a critical issue.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The issue is related to the changes made to address &lt;a href=&quot;https://github.com/advisories/GHSA-7j9m-j397-g4wx&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;MongoDB .NET/C# Driver vulnerable to Deserialization of Untrusted Data &#183; CVE-2022-48282 &#183; GitHub Advisory Database &#183; GitHub&lt;/a&gt; I assume but it isn&apos;t clear if this change was intended to prevent the scenario described in our implementation. Based on the comments made by James_Kovacs here &lt;a href=&quot;https://www.mongodb.com/community/forums/t/vulnerability-cve-2022-48282/216273&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;Vulnerability CVE-2022-48282 - Working with Data / Drivers &amp;amp; ODMs - MongoDB Developer Community Forums&lt;/a&gt; it seems like it should not.&lt;/p&gt;</description>
                <environment></environment>
        <key id="2286361">CSHARP-4567</key>
            <summary>Support projections where the lambda body returns a type different from but assignable to the lambda return type</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="13201">Fixed</resolution>
                                        <assignee username="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="thomas.murphy@swarco.com">Tom Murphy</reporter>
                        <labels>
                            <label>Bug</label>
                            <label>triage</label>
                    </labels>
                <created>Mon, 13 Mar 2023 03:05:44 +0000</created>
                <updated>Sat, 28 Oct 2023 11:46:56 +0000</updated>
                            <resolved>Wed, 17 May 2023 23:52:15 +0000</resolved>
                                    <version>2.19.0</version>
                                    <fixVersion>2.19.2</fixVersion>
                                    <component>LINQ3</component>
                    <component>Serialization</component>
                                        <votes>1</votes>
                                    <watches>4</watches>
                                                                                                                <comments>
                            <comment id="5433634" author="xgen-internal-githook" created="Thu, 18 May 2023 00:39:35 +0000"  >&lt;p&gt;Author: &lt;/p&gt;
{&apos;name&apos;: &apos;rstam&apos;, &apos;email&apos;: &apos;robert@robertstam.org&apos;, &apos;username&apos;: &apos;rstam&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-4567&quot; title=&quot;Support projections where the lambda body returns a type different from but assignable to the lambda return type&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-4567&quot;&gt;&lt;del&gt;CSHARP-4567&lt;/del&gt;&lt;/a&gt;: Support implicit conversion when lambda body returns a type assignable to the lambda return type.&lt;br/&gt;
Branch: v2.19.x&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-csharp-driver/commit/3c2d4ff52aa576fe2ccf1a3033ac65700327e8b8&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-csharp-driver/commit/3c2d4ff52aa576fe2ccf1a3033ac65700327e8b8&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="5433565" author="rstam" created="Wed, 17 May 2023 23:51:17 +0000"  >&lt;p&gt;While the repro provided is rather complex (it generates expressions dynamically), the root cause was found to be a projection like this:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;Expression&amp;lt;Func&amp;lt;C, object&amp;gt;&amp;gt; projection = x =&amp;gt; new R { X = x.Id };&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&amp;nbsp;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var find = collection.Find(&quot;{}&quot;).Project(projection);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;The lambda is declared to return `object` but the body returns a subclass of `object`.&lt;/p&gt;

&lt;p&gt;We were expecting the body to return a value whose type was the same as the return type of the lambda. We now support the body returning a value that is different from the lambda return type, as long as it is assignable to the lambda return type (which it must be, or the code would not have compiled in the first place).&lt;/p&gt;</comment>
                            <comment id="5433555" author="xgen-internal-githook" created="Wed, 17 May 2023 23:46:42 +0000"  >&lt;p&gt;Author: &lt;/p&gt;
{&apos;name&apos;: &apos;rstam&apos;, &apos;email&apos;: &apos;robert@robertstam.org&apos;, &apos;username&apos;: &apos;rstam&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-4567&quot; title=&quot;Support projections where the lambda body returns a type different from but assignable to the lambda return type&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-4567&quot;&gt;&lt;del&gt;CSHARP-4567&lt;/del&gt;&lt;/a&gt;: Support implicit conversion when lambda body returns a type assignable to the lambda return type.&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-csharp-driver/commit/b6190b5063f07c8639bc62e935d606da97a5d152&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-csharp-driver/commit/b6190b5063f07c8639bc62e935d606da97a5d152&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="5424038" author="JIRAUSER1273751" created="Mon, 15 May 2023 08:00:26 +0000"  >&lt;p&gt;I can confirm that the problem appeared also for us, after upgrading to 2.19.1 version.&#160;&lt;br/&gt;
Here is the repro code:&lt;br/&gt;
&lt;span class=&quot;image-wrap&quot; style=&quot;&quot;&gt;&lt;img src=&quot;https://jira.mongodb.org/secure/attachment/449710/449710_image-2023-05-15-10-00-22-523.png&quot; style=&quot;border: 0px solid black&quot; /&gt;&lt;/span&gt;&lt;/p&gt;</comment>
                            <comment id="5421914" author="rstam" created="Fri, 12 May 2023 20:02:25 +0000"  >&lt;p&gt;Sorry for the delay in investigating this. It shouldn&apos;t matter whether the projection expression was generated by the compiler or by your own code. But it does make it a bit harder to figure out what is going on. Thanks for the solution you provided that reproduces the issue. It helped me figure out the root cause and how to reproduce it without using dynamic expression generation.&lt;/p&gt;

&lt;p&gt;I&apos;m working on a fix now.&lt;/p&gt;</comment>
                            <comment id="5411882" author="JIRAUSER1273109" created="Tue, 9 May 2023 15:53:18 +0000"  >&lt;p&gt;Is there any update? The fact that this issue is now unassigned is not encouraging. I have colleagues in Europe that have identified additional issues with projections when including them in aggregate pipelines in driver version 2.19.0 which is totally outside of my issue. I saw in your backlog that there are other people that have had projections broken because driver 2.19.0 is not backward compatible. Any information would be helpful for me to relay to my organization.&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                            <attachment id="436605" name="MongoDB.Projection.Test.7z" size="5265" author="thomas.murphy@swarco.com" created="Mon, 13 Mar 2023 02:08:47 +0000"/>
                            <attachment id="449710" name="image-2023-05-15-10-00-22-523.png" size="36885" author="tkaras93@gmail.com" created="Mon, 15 May 2023 08:00:23 +0000"/>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                        <customfield id="customfield_10011" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Backwards Compatibility</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10038"><![CDATA[Fully Compatible]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10257" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Documentation Changes</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="11861"><![CDATA[Not Needed]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_14266" key="com.atlassian.jira.plugin.system.customfieldtypes:textarea">
                        <customfieldname>Documentation Changes Summary</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>&lt;p&gt;1.  What would you like to communicate to the user about this feature?&lt;br/&gt;
2.  Would you like the user to see examples of the syntax and/or executable code and its output?&lt;br/&gt;
3.  Which versions of the driver/connector does this apply to?&lt;/p&gt;</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|i1i7ow:</customfieldvalue>

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