question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum cannot be serialized as string in CosmosDb

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
TechWatchingcommented, Nov 28, 2019

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 👍.

2reactions
j82wcommented, Nov 28, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found