Field Inference should not take into account DataMember attributes
See original GitHub issueNEST/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:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
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 theDataMemberAttribute
when serializing the document but the latter search call on the document field using a lambda expression would not.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.