<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:35:29 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>[GODRIVER-1020] Primitive UnmarshalJSON method should return a zero&apos;d objectId when the field is empty instead of a decode error</title>
                <link>https://jira.mongodb.org/browse/GODRIVER-1020</link>
                <project id="14289" key="GODRIVER">Go Driver</project>
                    <description>&lt;p&gt;Currently, when ObjectIds are unmarshalled from a JSON payload&#160;&lt;a href=&quot;https://github.com/mongodb/mongo-go-driver/blob/master/bson/primitive/objectid.go#L100&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-go-driver/blob/master/bson/primitive/objectid.go#L100&lt;/a&gt;&#160;, 64 and 12 byte strings are properly converted to an objectId. All others return decoding errors. This is expected functionality.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;However an empty string or empty field also returns an error instead of a zero/null valued objectId. This makes using the primitive.ObjectID type within a struct definition very opinionated, and negates the need for IsZero methods. For example, if a service layer is responsible for decoding a struct and then setting ObjectID values based on claims information, the request will hit a decoding error since the payload will not contain the objectIds.&lt;/p&gt;

&lt;p&gt;It forces the use of struct composition to decode input values, by necessitating the extension to a more complete struct definition to apply the ObjectIds. It could very easily be argued that this is a best practice, but it should not be the responsibility of the mongo-driver to force such a high level design decision&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;Request: ObjectIds should be decoded for 64/12 bytes and for empty fields it should initialize a zero valued objectId that can then be validated, initialized or handled in the application logic&lt;/p&gt;</description>
                <environment></environment>
        <key id="758457">GODRIVER-1020</key>
            <summary>Primitive UnmarshalJSON method should return a zero&apos;d objectId when the field is empty instead of a decode error</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</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="divjot.arora@mongodb.com">Divjot Arora</assignee>
                                    <reporter username="qhenkart@gmail.com">Quest Henkart</reporter>
                        <labels>
                    </labels>
                <created>Sun, 5 May 2019 12:26:47 +0000</created>
                <updated>Sat, 28 Oct 2023 11:38:54 +0000</updated>
                            <resolved>Thu, 23 Apr 2020 21:19:04 +0000</resolved>
                                    <version>1.0.1</version>
                                    <fixVersion>1.4.0</fixVersion>
                                    <component>JSON &amp;amp; ExtJSON</component>
                                        <votes>0</votes>
                                    <watches>6</watches>
                                                                                                                <comments>
                            <comment id="3053541" author="xgen-internal-githook" created="Thu, 23 Apr 2020 21:18:59 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{&apos;name&apos;: &apos;Divjot Arora&apos;, &apos;email&apos;: &apos;divjot.arora@10gen.com&apos;, &apos;username&apos;: &apos;divjotarora&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/GODRIVER-1020&quot; title=&quot;Primitive UnmarshalJSON method should return a zero&amp;#39;d objectId when the field is empty instead of a decode error&quot; class=&quot;issue-link&quot; data-issue-key=&quot;GODRIVER-1020&quot;&gt;&lt;del&gt;GODRIVER-1020&lt;/del&gt;&lt;/a&gt; Allow empty JSON string to decode into ObjectID (#384)&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-go-driver/commit/5ae4e01d738a08817cbfaeac2ac829377c7d8f40&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-go-driver/commit/5ae4e01d738a08817cbfaeac2ac829377c7d8f40&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="3051671" author="divjot.arora" created="Wed, 22 Apr 2020 19:41:20 +0000"  >&lt;p&gt;PR:&#160;&lt;a href=&quot;https://github.com/mongodb/mongo-go-driver/pull/384&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-go-driver/pull/384&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="3047069" author="epelc@greatcloak.com" created="Mon, 20 Apr 2020 14:47:02 +0000"  >&lt;p&gt;Awesome! Hope it makes it through.&lt;/p&gt;</comment>
                            <comment id="3047062" author="divjot.arora" created="Mon, 20 Apr 2020 14:45:51 +0000"  >&lt;p&gt;Ok, I&apos;ve moved the ticket back to &quot;Needs Triage&quot; so the team can discuss it in our meeting later today.&lt;/p&gt;</comment>
                            <comment id="3047059" author="epelc@greatcloak.com" created="Mon, 20 Apr 2020 14:44:06 +0000"  >&lt;p&gt;Yes, that is correct.&lt;/p&gt;

&lt;p&gt;&#160;With json input `{&quot;userId&quot;:&quot;&quot;}` Foo.UserID should equal `primitive.NilObjectID` instead of erroring out.&lt;/p&gt;</comment>
                            <comment id="3047054" author="divjot.arora" created="Mon, 20 Apr 2020 14:42:38 +0000"  >&lt;p&gt;&lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=epelc%40greatcloak.com&quot; class=&quot;user-hover&quot; rel=&quot;epelc@greatcloak.com&quot;&gt;epelc@greatcloak.com&lt;/a&gt;&#160;From what I understand, the request is to allow a JSON string like {&quot;userId&quot;: &quot;&quot;} to unmarshal into a struct defined as&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;type Foo struct {&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;&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;    UserID primitive.ObjectID `json:&lt;/span&gt;&lt;span style=&quot;color: blue; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;&quot;userId&quot;&lt;/span&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;`&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;   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;}&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;Is this correct?&lt;/p&gt;</comment>
                            <comment id="3046175" author="epelc@greatcloak.com" created="Sun, 19 Apr 2020 17:37:49 +0000"  >&lt;p&gt;Hi Quest, Thanks for the library and info! I looked it over and I don&apos;t think it is the right way to go about this. Using a 12 byte array as an object id is more efficient and I agree with the official lib&apos;s reasons for switching to this. Also we&apos;ve converted to this already on the backend so the work is done.&lt;/p&gt;

&lt;p&gt;The real thing though is that if you convert to the official lib you end up with compilation errors surrounding the old mgo object ids. If you fix all compilation errors it is compatible besides for this small point of not handling empty strings with json. I think this should be added for this upgrade reason and also when dealing with third parties or frontend code with JSON an empty string is very common since it is often not explicit that you set this. Even if you update all frontend code you still have third parties or people using your api which will easily run into errors with minimal info since the std lib json library doesn&apos;t give you a field that it failed on.&lt;/p&gt;

&lt;p&gt;The current implementation already handles 64 and 12 byte representations. I think it&apos;s a fair extension to lax this to handle empty strings too.&lt;/p&gt;</comment>
                            <comment id="3046112" author="qhenkart@gmail.com" created="Sun, 19 Apr 2020 07:49:27 +0000"  >&lt;p&gt;Hey Ed,  when this ticket was rejected I made a small abstraction that completely resolved the issue and made migrating from mgo very easy. It also made dealing with the new library way less error prone. Feel free to use it &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/qhenkart/objectid-go&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/qhenkart/objectid-go&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="3046060" author="epelc@greatcloak.com" created="Sat, 18 Apr 2020 23:41:34 +0000"  >&lt;p&gt;Could we consider reopening this? Allowing an empty json string to unmarshal to `primitive.NilObjectID` would greatly help with upgrading from mgo. If you are just using mongodb I don&apos;t see why you&apos;d need or want an extra abstraction layer especially for such a common scenario.&lt;/p&gt;

&lt;p&gt;Forcing a client to send 12 0s, or delete a field entirely(no empty string, null, etc ie unset the key) due to such a minor change is a big pain and also hard to do in existing frontend apps.&lt;/p&gt;

&lt;p&gt;You also can&apos;t use a pointer to an object id to workaround this like you can with `omitempty` when marshaling. The only option for upgrading is large code modifications on the frontend/connected services or to rip out all uses of `primitive.ObjectID` with a replacement on the backend. I think this is common enough to include in the official lib though.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;</comment>
                            <comment id="2265054" author="kris.brandow" created="Thu, 30 May 2019 13:53:56 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=qhenkart&quot; class=&quot;user-hover&quot; rel=&quot;qhenkart&quot;&gt;qhenkart&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;We do handle JSON and while it&apos;s not recommended you can use types like &lt;tt&gt;primitive.ObjectID&lt;/tt&gt; in your APIs. You can use them, however you will be directly coupling your service to MongoDB. It is not supported to use types like &lt;tt&gt;primitive.ObjectID&lt;/tt&gt; while also wanting to be decoupled from MongoDB. We support extended JSON and for convenience we also support decoding ObjectIDs that are properly formatted hex strings. At this time, an empty string does not count as a properly formatted hex string.&lt;/p&gt;</comment>
                            <comment id="2251447" author="qhenkart@gmail.com" created="Sat, 18 May 2019 10:37:47 +0000"  >&lt;p&gt;thank you @krisbrandow I appreciate the response. I think this answer is adequate, but I still think documentation and migration instructions need to reflect this behavior.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;It is strange though, that given your response, it seems like the primitive.ObjectID unmarshalJSON method here&#160;&lt;a href=&quot;https://github.com/mongodb/mongo-go-driver/blob/master/bson/primitive/objectid.go#L100&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-go-driver/blob/master/bson/primitive/objectid.go#L100&lt;/a&gt;&#160;would know how to handle the exact behavior you described (converting string id payloads used in other services, along with empty strings to objectIDs or nil objectIDs in the mongo service). Why have a JSON unmarshal method that doesn&apos;t know how to handle JSON payloads properly? It seems to me like the opinionated logic should be confined to the BSON marshallers and validators instead as they are built to interact with BSON directly&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;Given the context of this answer, I look forward to your additions involving this ticket&#160;&lt;a href=&quot;https://jira.mongodb.org/browse/GODRIVER-1030&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;https://jira.mongodb.org/browse/GODRIVER-1030&lt;/a&gt;&#160;&lt;/p&gt;</comment>
                            <comment id="2250473" author="kris.brandow" created="Fri, 17 May 2019 15:43:49 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=qhenkart&quot; class=&quot;user-hover&quot; rel=&quot;qhenkart&quot;&gt;qhenkart&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;The types within the Go driver&apos;s BSON library are meant to be used directly with the &lt;tt&gt;mongo&lt;/tt&gt; package and aren&apos;t general types meant to be used in other APIs. While they may function for that, it is recommended that you create a database abstraction layer, using your own types, that you then convert into types that can be inserted into the database. For example, instead of using the &lt;tt&gt;primitive.ObjectID&lt;/tt&gt; type directly, use a &lt;tt&gt;string&lt;/tt&gt; in the type you decode the HTTP JSON payload into, then convert that &lt;tt&gt;string&lt;/tt&gt;&#160;into an objectID when inserting it into the database.&lt;/p&gt;</comment>
                            <comment id="2243684" author="qhenkart@gmail.com" created="Sun, 12 May 2019 08:18:24 +0000"  >&lt;p&gt;@isabella To be clear, I am not talking about representing a BSON document with JSON. I am talking about everyday use of the primitive package in a typical Go Application, in this case simply decoding a JSON payload from an http request, still far removed from interfacing with mongo or creating a bson document.&#160;I think this ticket is more related to an important Go-ism that the Go driver team should consider rather than the mongod spec. This behavior will require any go service that is in communication with a go-mongo enabled service to be refactored to fall in line with the mongod spec, due to Go&#8217;s zero-valued initialization of strings and other values, and cause services to bleed mongo specifications in their APIs&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;It creates a lot of coupling to the mongo driver with non-mongo-services. We don&#8217;t want our non-mongo-services to be concerned with the mongod bson/json spec, we don&#8217;t even want to expose that we are using mongo. We want our go clients to only be concerned with our API reqs. An entire http request will fail anonymously on receipt of a JSON payload with an un-initialized ID field from a Go client without giving us the ability to validate or create our own behaviors/error scenarios. The GO-driver should not be an opinionated framework but simply a way to interface with mongo and bson from a Go service.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;In our case, we can just fork the library, but this behavior needs to be clearly documented in the migration guide as it is at odds with all of the current mongo community drivers&lt;/p&gt;</comment>
                            <comment id="2243077" author="isabella.siu" created="Fri, 10 May 2019 20:07:02 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=qhenkart&quot; class=&quot;user-hover&quot; rel=&quot;qhenkart&quot;&gt;qhenkart&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;This request goes against the &lt;a href=&quot;https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#conversion-table&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;extended JSON spec&lt;/a&gt;, which does not consider an empty string a valid ObjectId. &lt;/p&gt;</comment>
                            <comment id="2236263" author="qhenkart@gmail.com" created="Mon, 6 May 2019 21:54:32 +0000"  >&lt;p&gt;@isabella Thank you for investigating this matter. Here is a gist that will create the undesirable results&#160;&lt;a href=&quot;https://gist.github.com/qhenkart/99ab370875130c6c82e12bdf6594d0ec&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://gist.github.com/qhenkart/99ab370875130c6c82e12bdf6594d0ec&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;The main issue is that if one Golang service is passing a payload to another. But the former doesn&apos;t use the mongo library (opting for string representations of ids instead), Golang will automatically initialize the id fields in the struct as zero valued strings `&quot;&quot;`. This makes any Go server unable to pass data easily unless they both share the mongo library. The same goes with requests from clientside javascript, although they are less likely to have uninitialized Id fields unless the server has given it to them&#160;&lt;/p&gt;</comment>
                            <comment id="2236155" author="isabella.siu" created="Mon, 6 May 2019 20:59:14 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=qhenkart&quot; class=&quot;user-hover&quot; rel=&quot;qhenkart&quot;&gt;qhenkart&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Can you provide either the json you&apos;re using or an example program illustrating this?&lt;/p&gt;</comment>
                            <comment id="2236146" author="qhenkart@gmail.com" created="Mon, 6 May 2019 20:54:07 +0000"  >&lt;p&gt;I&apos;ve made a gerrithub PR here&#160;&lt;a href=&quot;https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/453160&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/453160&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;and a normal github PR here&#160;&lt;a href=&quot;https://github.com/mongodb/mongo-go-driver/pull/155&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-go-driver/pull/155&lt;/a&gt;&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </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|hunazb:</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>