<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 09:00:09 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>[JAVA-3665] Polymorphic POJO mapping does not work with package registration only</title>
                <link>https://jira.mongodb.org/browse/JAVA-3665</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;When polymorphic classes are used with @BsonDiscriminator, attempts to read/deserialize POJOs that return polymorhpic results (any of the derived classes may be returned) will fail, unless you do one of two things:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Register every class explicitly (not just the package)&lt;/li&gt;
	&lt;li&gt;Use a concrete instead of every derived class before attempting to read it.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;The suggestion initially given by mongodb support (reading from Collection&amp;lt;DerivedClass&amp;gt; instead of Collection&amp;lt;BaseClass&amp;gt; is not workable, because you cannot read a list of mixed types e.g.&lt;br/&gt;
MongoCursor&amp;lt;MessageBase&amp;gt; cursor = msgs.find().iterator();&lt;br/&gt;
while (cursor.hasNext()) { ... }&lt;br/&gt;
{{}}&lt;/p&gt;

&lt;p&gt;The issue seems to be that simply registering the package as prescribed&lt;br/&gt;
CodecProvider pojoCodecProvider = PojoCodecProvider.builder().register(myPackage).build();&lt;br/&gt;
CodecRegistry pojoCodecRegistry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromProviders(pojoCodecProvider));&lt;br/&gt;
mongoClient = MongoClients.create(MongoClientSettings.builder()&lt;br/&gt;
	.applyConnectionString(new ConnectionString(mongodbURI))&lt;br/&gt;
	.codecRegistry(pojoCodecRegistry).build());&lt;br/&gt;
collection = mongoClient.getDatabase(mongodbDatabase).getCollection(mongodbCollection, entityClass);&lt;br/&gt;
{{}}&lt;/p&gt;

&lt;p&gt;is not enough. Attempting to read from the database results in the following error:&lt;br/&gt;
ERROR: An exception occurred when decoding using the AutomaticPojoCodec.&lt;br/&gt;
Decoding into a &apos;Derived1&apos; failed with the following exception:&lt;br/&gt;
Failed to decode &apos;Derived1&apos;. Decoding errored with: A class could not be found for the discriminator: &apos;derived1&apos;.&lt;br/&gt;
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.&#160;&lt;br/&gt;
Polymorphism&#160;&lt;em&gt;does&lt;/em&gt;&#160;work if concrete classes are first utilized before an attempt is made to read polymorphically. Please see the attached &quot;scratch.zip&quot; example which is built using maven. It is a CLI with three commands:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;clear: clear the table&lt;/li&gt;
	&lt;li&gt;write: write two objects to table, then read them back polymorphically&lt;/li&gt;
	&lt;li&gt;read: read objects polymorphically&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;When you run this, you&apos;ll see that the write command works as expected, returning a polymorphic list, but the read command fails if that is the first command run in a process. The only difference is that the write command first inserts&#160;&#160;&lt;tt&gt;Derived1&lt;/tt&gt;&#160;and&#160;&lt;tt&gt;Dervied2&lt;/tt&gt;&#160;before reading them back. Clearly, it must somehow convince the POJO codec to believe that the derived classes all exist before it can read them.&#160;&lt;/p&gt;

&lt;p&gt;Changing the codec registration to&#160;&lt;/p&gt;

&lt;p&gt;&lt;tt&gt;CodecProvider pojoCodecProvider = PojoCodecProvider.builder().register(Base.class, Derived1.class, Derived2.class).build(); CodecProvider pojoCodecProvider = PojoCodecProvider.builder().register(Base.class, Derived1.class, Derived2.class).build();&lt;/tt&gt;&lt;/p&gt;

&lt;p&gt;Works as expected.&lt;/p&gt;</description>
                <environment>Java 8, Mongodb server 3.6, mongodb-driver-sync 3.12.2</environment>
        <key id="1282191">JAVA-3665</key>
            <summary>Polymorphic POJO mapping does not work with package registration only</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="13202">Works as Designed</resolution>
                                        <assignee username="ross@mongodb.com">Ross Lawley</assignee>
                                    <reporter username="john.lilley@redpointglobal.com">John Lilley</reporter>
                        <labels>
                    </labels>
                <created>Thu, 19 Mar 2020 14:56:34 +0000</created>
                <updated>Fri, 27 Oct 2023 13:21:00 +0000</updated>
                            <resolved>Mon, 20 Apr 2020 13:21:50 +0000</resolved>
                                    <version>3.12.2</version>
                                                    <component>POJO</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="3048703" author="ross@10gen.com" created="Tue, 21 Apr 2020 10:34:31 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=john.lilley%40redpointglobal.com&quot; class=&quot;user-hover&quot; rel=&quot;john.lilley@redpointglobal.com&quot;&gt;john.lilley@redpointglobal.com&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;I&apos;ve added &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-3707&quot; title=&quot;Support package scanning for Discriminators&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-3707&quot;&gt;JAVA-3707&lt;/a&gt; to look at scanning the class path for BsonDiscriminators.&lt;/p&gt;

&lt;p&gt;All the best,&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="3046987" author="john.lilley@redpointglobal.com" created="Mon, 20 Apr 2020 14:14:56 +0000"  >&lt;p&gt;Even if this works &quot;as designed&quot;, I have described a valid and useful case which IMHO should be supported.&#160; Can you reclassify this as a Story and re-open it?&#160; A more appropriate title would be: &quot;Support polymorphic POJO mapping with package registration and custom BsonDiscriminator&quot;.&#160; In my workplace, we would never allow the default classname discriminator, as doing so prohibits refactoring and makes the schema fragile.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

&lt;p&gt;john&lt;/p&gt;</comment>
                            <comment id="3046895" author="ross@10gen.com" created="Mon, 20 Apr 2020 13:21:36 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=john.lilley%40redpointglobal.com&quot; class=&quot;user-hover&quot; rel=&quot;john.lilley@redpointglobal.com&quot;&gt;john.lilley@redpointglobal.com&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Thanks for the ticket, I was wondering why I couldn&apos;t reproduce the issue and thought the library already did package based lookups for classes.  Having looked at your code I noticed that the discriminators are all lowercase.  If the discriminator is the name of the class, then the registered &lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/blob/master/bson/src/main/org/bson/codecs/pojo/DiscriminatorLookup.java#L74-L82&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;package based lookups&lt;/a&gt; will work.  However, if it is different to the class name, then it would have to be explicitly declarated.&lt;/p&gt;

&lt;p&gt;All the best,&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="1324090">JAVA-3707</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                            <attachment id="252698" name="scratch.zip" size="5959" author="john.lilley@redpointglobal.com" created="Thu, 19 Mar 2020 14:54:43 +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|hwxm7j:</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>