Skip to content

Commit e837dd0

Browse files
authored
Serialize own IProperty implementations (#4219)
This commit fixes the ability to serialize own IProperty types within PropertyFormatter, using utf8json. Initially, this feature was not originally available in utf8json due to how enums with the same underlying values but different field names were serialized, and how this interacts with internal enums of attribute runtime types. Since this is now fixed in #elastic/elasticsearch-net/4032, custom IProperty implementations will now serialize correctly.
1 parent 780f52f commit e837dd0

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

docs/client-concepts/connection/configuration-options.asciidoc

+4
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ Instead of following a c/go like error checking on response.IsValid do throw an
190190
+
191191
Reasons for such exceptions could be search parser errors, index missing exceptions, etc...
192192

193+
`TransferEncodingChunked`::
194+
195+
Whether the request should be sent with chunked Transfer-Encoding. Default is `false`
196+
193197
`UserAgent`::
194198

195199
The user agent string to send with requests. Useful for debugging purposes to understand client and framework versions that initiate requests to Elasticsearch

docs/client-concepts/high-level/serialization/extending-nest-types.asciidoc

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ please modify the original csharp file found at the link and submit the PR with
1515
[[extending-nest-types]]
1616
=== Extending NEST types
1717

18-
Sometimes you might want to provide a custom implementation of a type perhaps to work around an issue or because
18+
Sometimes you might want to provide a custom implementation of a type, perhaps to work around an issue or because
1919
you're using a third-party plugin that extends the features of Elasticsearch, and NEST does not provide support out of the box.
2020

21+
NEST allows extending its types in some scenarios, discussed here.
22+
23+
==== Creating your own property mapping
24+
25+
As an example, let's imagine we're using a third party plugin that provides support for additional data type
26+
for field mapping. We can implement a custom `IProperty` implementation so that we can use the field mapping
27+
type with NEST.
28+
2129
[source,csharp]
2230
----
2331
public class MyPluginProperty : IProperty
@@ -48,7 +56,7 @@ Now that we have our own `IProperty` implementation we can add it to our propert
4856

4957
[source,csharp]
5058
----
51-
var createIndexResponse = _client.Indices.Create("myindex", c => c
59+
var createIndexResponse = client.Indices.Create("myindex", c => c
5260
.Map<Project>(m => m
5361
.Properties(props => props
5462
.Custom(new MyPluginProperty("fieldName", "dutch"))
@@ -75,5 +83,5 @@ which will serialize to the following JSON request
7583
----
7684

7785
Whilst NEST can _serialize_ our `my_plugin_property`, it does not know how to _deserialize_ it;
78-
We plan to make this more pluggable in the future
86+
We plan to make this more pluggable in the future.
7987

docs/high-level.asciidoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ include::client-concepts/high-level/serialization/extending-nest-types.asciidoc[
105105
[[mapping]]
106106
== Mapping
107107

108-
To interact with Elasticsearch using NEST, we need to be able to contol how POCO types in our solution
108+
To interact with Elasticsearch using NEST, we need to be able to control how POCO types in our solution
109109
map to JSON documents and fields stored within the inverted index in Elasticsearch. This section
110110
describes all of the different functionality available within NEST that makes working with POCOs and Elasticsearch
111111
a breeze.

src/Nest/Mapping/Types/PropertyFormatter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public IProperty Deserialize(ref JsonReader reader, IJsonFormatterResolver forma
9494
case FieldType.RankFeatures: return Deserialize<RankFeaturesProperty>(ref segmentReader, formatterResolver);
9595
case FieldType.Flattened: return Deserialize<FlattenedProperty>(ref segmentReader, formatterResolver);
9696
case FieldType.None:
97-
// no "type" field in the property mapping
97+
// no "type" field in the property mapping, or FieldType enum could not be parsed from typeString
9898
return Deserialize<ObjectProperty>(ref segmentReader, formatterResolver);
9999
default:
100100
throw new ArgumentOutOfRangeException(nameof(type), type, "mapping property converter does not know this value");
@@ -210,7 +210,7 @@ public void Serialize(ref JsonWriter writer, IProperty value, IJsonFormatterReso
210210
Serialize<IGenericProperty>(ref writer, genericProperty, formatterResolver);
211211
else
212212
{
213-
var formatter = DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter<IProperty>();
213+
var formatter = formatterResolver.GetFormatter<object>();
214214
formatter.Serialize(ref writer, value, formatterResolver);
215215
}
216216
break;

src/Tests/Tests/ClientConcepts/HighLevel/Serialization/ExtendingNestTypes.doc.cs

+15-13
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ namespace Tests.ClientConcepts.HighLevel.Serialization
1616
/**[[extending-nest-types]]
1717
* === Extending NEST types
1818
*
19-
* Sometimes you might want to provide a custom implementation of a type perhaps to work around an issue or because
19+
* Sometimes you might want to provide a custom implementation of a type, perhaps to work around an issue or because
2020
* you're using a third-party plugin that extends the features of Elasticsearch, and NEST does not provide support out of the box.
21+
*
22+
* NEST allows extending its types in some scenarios, discussed here.
23+
*
24+
* ==== Creating your own property mapping
25+
*
26+
* As an example, let's imagine we're using a third party plugin that provides support for additional data type
27+
* for field mapping. We can implement a custom `IProperty` implementation so that we can use the field mapping
28+
* type with NEST.
2129
*/
2230
public class ExtendingNestTypes
2331
{
24-
private readonly IElasticClient _client = TestClient.DisabledStreaming;
32+
// keep field name as client, for documentation purposes
33+
private readonly IElasticClient client = TestClient.DisabledStreaming;
2534

26-
/* ==== Creating your own property mapping
27-
*
28-
* As an example, let's imagine we're using a third party plugin that provides support for additional data type
29-
* for field mapping. We can implement a custom `IProperty` implementation so that we can use the field mapping
30-
* type with NEST.
31-
*/
3235
public class MyPluginProperty : IProperty
3336
{
3437
IDictionary<string, object> IProperty.LocalMetadata { get; set; }
@@ -48,10 +51,9 @@ public MyPluginProperty(string name, string language)
4851
[PropertyName("numeric")]
4952
public bool Numeric { get; set; }
5053
}
51-
5254

53-
[U (Skip = "TODO: Does not work with utf8json")]
54-
// hide
55+
56+
[U]
5557
public void InjectACustomIPropertyImplementation()
5658
{
5759
/**
@@ -60,7 +62,7 @@ public void InjectACustomIPropertyImplementation()
6062
*
6163
* Now that we have our own `IProperty` implementation we can add it to our propertes mapping when creating an index
6264
*/
63-
var createIndexResponse = _client.Indices.Create("myindex", c => c
65+
var createIndexResponse = client.Indices.Create("myindex", c => c
6466
.Map<Project>(m => m
6567
.Properties(props => props
6668
.Custom(new MyPluginProperty("fieldName", "dutch"))
@@ -90,7 +92,7 @@ public void InjectACustomIPropertyImplementation()
9092

9193
/**
9294
* Whilst NEST can _serialize_ our `my_plugin_property`, it does not know how to _deserialize_ it;
93-
* We plan to make this more pluggable in the future
95+
* We plan to make this more pluggable in the future.
9496
*/
9597
// hide
9698
Expect(expected).FromRequest(createIndexResponse);

src/Tests/Tests/high-level.asciidoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ own serializer, or by modifying how NEST's serializer behaves.
7474
[[mapping]]
7575
== Mapping
7676

77-
To interact with Elasticsearch using NEST, we need to be able to contol how POCO types in our solution
77+
To interact with Elasticsearch using NEST, we need to be able to control how POCO types in our solution
7878
map to JSON documents and fields stored within the inverted index in Elasticsearch. This section
7979
describes all of the different functionality available within NEST that makes working with POCOs and Elasticsearch
8080
a breeze.

0 commit comments

Comments
 (0)