[CSHARP-1984] String array is not serialized when casted to object Created: 15/May/17 Updated: 20/Jan/23 Resolved: 07/Apr/21 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Serialization |
| Affects Version/s: | 2.4.3 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Andrius Zalimas | Assignee: | Robert Stam |
| Resolution: | Duplicate | Votes: | 5 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
.NET 4.5.1. We are upgrading the driver from v1.8.x. Project infrastructure does not allow generics yet so cast to object is used as a workaround. |
||
| Issue Links: |
|
||||||||||||||||
| Epic Link: | Improve Serialization | ||||||||||||||||
| Case: | (copied to CRM) | ||||||||||||||||
| Description |
|
Trying to build update command with $set operator using UpdateDefinitionBuilder. However it does not serialize string arrays when they are casted to object. Instead it gives "(Collection)". Here is simple console app to show to issue:
Output with v2.4.2:
Output with v2.4.3:
|
| Comments |
| Comment by Robert Stam [ 07/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Fixed by | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 07/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
It was fixed in | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 07/Apr/21 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Output using 2.7.3:
Output using 2.8.0:
So the issue appears to be fixed in 2.8.0. Will try and find what ticket fixed it.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Ashwin Lingannagari [ 10/Dec/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Same issue here. This is still open for so long. We replaced Update with ReplaceOne. Unfortunately, replace replaces one document at a time but we have the abstracted API to update fields of multiple documents at the same time. So, the number of transactions to update multiple documents significantly increased. I wish this issue gets fixed soon. Mongo DB driver - 2.7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Roman Zhyliov [ 20/Sep/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Surprised to see this still open for a year. Having exactly same issue with 2.7 which blocks our production completely from having and abstracted updates for dynamic schemas functionality. The whole .Update.Set() logic in my mind is broken. Developer should be able to set a Merge like updates. I.e. -> Update/Merge only the fields my code/schema knows about and don't replace the rest And using reflections like that (GetType().GetProperty) makes our code less performant. Currently due to this bug we are forced to use ReplaceOneAsync and cannot afford to have different versions of schemas in remote without some wacky hacks and workarounds. Most likely will be switching to some alternative drivers with better native js driver support (cuz you can clearly do any merge updates with the native syntax) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by John Mills [ 10/Aug/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I'm having a similar issue as well with string fields! Whenever the field is null, the string content "BsonNull" is being stored forcing the field type to become string instead of null. This is causing all sorts of issues with lookup and retrieval since we are treating that as a normal string value and cannot tell tha tit is supposed to be null! Please fix ASAP!
MongoDB Driver v2.7 / mongoDB v3.6 & 4.0
Output: Notice that the rendered updater and retrieved object both have the text "BsonNull" for the string field named 'B'. Robo3T also shows the field type as 'String' instead of 'Null'.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Corvino Fabio [ 19/Jun/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Same issue. MongoDB version 3.6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Maksim Kislyakov [ 28/Feb/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Hello everyone. I got the same bug too. I use MongoDB version 2.5.0. I have the following extension method and when i try to update array of strings driver writes to db just string "String[]", but not actual array. Is there any news about fixing this bug? Or is it not a bug, but a normal situation?
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by John Bencina [ 28/Jan/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I'm running into this issue as well. In the C# driver, I use reflection to only update non-null fields. However, my object has List<string> property that's being saved as "(Collection)" in MongoDB. My hacky workaround is to recreate the list from scratch.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Andrius Zalimas [ 18/May/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I agree that behavior in v2.4.2 is not a bug. Problem is only in v2.4.3. But it would be nice feature if driver recognized actual derived type and ignored generic. Reason for using <object> is because we have an old project and want to upgrade mongo server. To do this we need to upgrade driver first. But our data access layer has features that are not compatible with generic types. We only have object values and some complex conversion to old Bson document (of v1.8.1). Another issue (should I create another ticket?) : Then it is no longer possible to use $push operations to this array. Here is small console app to reproduce this. (MongoDB v3.4.4, MongoDriver v2.4.x):
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 16/May/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I can reproduce this. But I would argue that the v2.4.2 behavior is also a bug. Seems like the desired behavior is that the value would be serialized the same in both cases. Why are you using Set<object>? The confusion is arising because the Set statement is stating that the type of the field is object when the actual type of the field is List<int> or List<string>. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Andrius Zalimas [ 15/May/17 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
It would also be nice if driver used actual type (not generic cast) when making a decision to add or not the discriminator. |