Enum cannot be serialized as string in CosmosDb
See original GitHub issueI have an object that I wan’t to save as a document in a CosmosDb container. This object contains some Enum properties. I did not find a way to serialize these properties as string in CosmosDb. They are serialized as their int value. I looked at CosmosSerializerOptions but I did not see a way to configure that.
Moreover, on my object the id of my document is also an ennum property so when I try to create the document I have this error:
Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: 400 Substatus: 0 Reason: (Message: {"Errors":["The input name '0' is invalid. Ensure to provide a unique non-empty string less than '1024' characters."]}
Am I missing something or is handling enum not implemented yet in the sdk ? Is there any workaround known ? Maybe using NewtonSoft serializer as the serializer ?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:15 (5 by maintainers)
Top Results From Across the Web
Serialize and Deserialize Cosmos DB document property ...
When I update the document in cosmos db, it is not getting updated to string value. It is always defaulting to the default...
Read more >CosmosPropertyNamingPolicy Enum (Azure.Cosmos. ...
Determines the naming policy used to convert a string-based name to another format, such as a camel-casing where the first letter is lower...
Read more >Azure Cosmos DB trigger for Functions 2.x and higher
The Azure Cosmos DB Trigger uses the Azure Cosmos DB change feed to listen for inserts and updates across partitions.
Read more >ASP.NET Core–Serializing enums
NET Core, it will be serialized as a number value. ... But what if we want to return the enum value as a...
Read more >Custom JSON serialization with Azure Cosmos DB SDK
This post explains how you can use custom JSON serializer settings with Cosmos DB using Newtonsoft JSON and System.Text.Json.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I succeeded to make it work. Thank you so much for your help @j82w ! What is weird is that I did the same thing that I was doing before but from another laptop. I wonder what was causing the issue … but nevermind it works now, I will check my work laptop tomorrow. I close the issue but I think it would make sense to add a setting for enum in the
CosmosSerializerOptions
in a future version of the sdk. Thanks again 👍.By any chance do you have
using system.text.json
in your project? Is it a .net core 3.0 app? My only thought is that the[JsonConverter(typeof(StringEnumConverter))]
is from the wrong package and that’s why it’s getting ignored.