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.

Field Inference should not take into account DataMember attributes

See original GitHub issue

NEST/Elasticsearch.Net version: 7.5.1

Elasticsearch version: 7.5.1

Description of the problem including expected versus actual behavior:

The current PropertyMappingProvider takes into account DataMember attributes and it should not. I feel like it should only look at only nest defined attributes and leave the property naming via DataMember attributes to my defined serializer. It should only look at the PropertyName attributes and fallback to the serializer to handle naming.

We have DTO models that we put DataContact and DataMember attributes just to control the serialization for only the API surface and configured our own NEST JSON.NET serializer to ignore the attributes (via strategy.OverrideSpecifiedNames) and use our naming strategy. It was a big WTF moment when this wasn’t working, even though debugging it we saw our own naming strategy being called.

         private JsonSerializerSettings JsonSerializerSettingsFactory()
         {
            //.....
         }

         private void ModifyContractResolver(ConnectionSettingsAwareContractResolver resolver)
         {
             var strategy = ((DefaultContractResolver)_serializerSettings.ContractResolver).NamingStrategy;
             resolver.NamingStrategy = new CamelCaseNamingStrategy(strategy.ProcessDictionaryKeys, strategy.OverrideSpecifiedNames, strategy.ProcessExtensionDataNames);
         }

The fix we had to do was basically define a new custom PropertyMappingProvider and just return null because we don’t use any Nest attributes and didn’t want DataMembers to be used.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
russcamcommented, Jan 28, 2020

Relates: https://github.com/elastic/elasticsearch-net/issues/3107 and https://github.com/elastic/elasticsearch-net/pull/3304

I think this one’s a little tricky. Looking at the related issue and PR above, it was introduced because indexing a document with DataMemberAttribute applied to properties, then searching on that document index with .Field(...) was incongruent; former indexing call would pick up the DataMemberAttribute when serializing the document but the latter search call on the document field using a lambda expression would not.

0reactions
niemyjskicommented, Aug 26, 2020

You should only use your own nest attributes. This is causing really hard to debug issues when round trip serialization gets messed up. For example, you may define a special casing and tell json.net serializer to ignore data member attributes. But that doesn’t matter what you specify because you are overriding the behavior and then you go down a rabbit hole and can only find what’s going on after debugging source code in this repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Field inference | Elasticsearch.Net and NEST: the .NET clients [5.x]
This property has both a NEST attribute and a JsonProperty , NEST should win. We should take the json property into account by...
Read more >
Why does the C# DataMember attribute allow serialization ...
Be aware that the data returned by the member (even if it's private) will be serialized and deserialized, and thus can be viewed...
Read more >
DataMemberAttribute Class (System.Runtime.Serialization)
Properties to which the DataMemberAttribute attribute has been applied must have both get and set fields. They cannot be get -only or set...
Read more >
Protobuf order or serialization
I am currently using Protobuf serializer in my C# project. Currently I am serializing my Objects using "[ProtoContract(ImplicitFields ...
Read more >
WCF Crib Sheet – Data Contract and Data Member – Datatell
The data member attribute denotes which members of a type are serializable by the data contract serializer. Can be applied on fields and...
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