Skip to content

Commit 5c016f5

Browse files
committed
Serialize IProperty types based on derived interface type (#4262)
This commit updates PropertyFormatter to use the interface type rather than the type string, to determine how to serialize the IProperty. Fixes #4248 (cherry picked from commit 84e8e7c)
1 parent 8b99472 commit 5c016f5

File tree

1 file changed

+66
-75
lines changed

1 file changed

+66
-75
lines changed

src/Nest/Mapping/Types/PropertyFormatter.cs

Lines changed: 66 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -109,119 +109,110 @@ public void Serialize(ref JsonWriter writer, IProperty value, IJsonFormatterReso
109109
return;
110110
}
111111

112-
switch (value.Type)
112+
switch (value)
113113
{
114-
case "text":
115-
Serialize<ITextProperty>(ref writer, value, formatterResolver);
114+
case ITextProperty textProperty:
115+
Serialize(ref writer, textProperty, formatterResolver);
116116
break;
117-
case "keyword":
118-
Serialize<IKeywordProperty>(ref writer, value, formatterResolver);
117+
case IKeywordProperty keywordProperty:
118+
Serialize(ref writer, keywordProperty, formatterResolver);
119119
break;
120-
case "search_as_you_type":
121-
Serialize<ISearchAsYouTypeProperty>(ref writer, value, formatterResolver);
120+
case INumberProperty numberProperty:
121+
Serialize(ref writer, numberProperty, formatterResolver);
122122
break;
123-
case "float":
124-
case "double":
125-
case "byte":
126-
case "short":
127-
case "integer":
128-
case "long":
129-
case "scaled_float":
130-
case "half_float":
131-
Serialize<INumberProperty>(ref writer, value, formatterResolver);
123+
case IDateProperty dateProperty:
124+
Serialize(ref writer, dateProperty, formatterResolver);
132125
break;
133-
case "date":
134-
Serialize<IDateProperty>(ref writer, value, formatterResolver);
126+
case IBooleanProperty booleanProperty:
127+
Serialize(ref writer, booleanProperty, formatterResolver);
135128
break;
136-
case "date_nanos":
137-
Serialize<IDateNanosProperty>(ref writer, value, formatterResolver);
129+
case INestedProperty nestedProperty:
130+
Serialize(ref writer, nestedProperty, formatterResolver);
138131
break;
139-
case "boolean":
140-
Serialize<IBooleanProperty>(ref writer, value, formatterResolver);
132+
case IObjectProperty objectProperty:
133+
Serialize(ref writer, objectProperty, formatterResolver);
141134
break;
142-
case "binary":
143-
Serialize<IBinaryProperty>(ref writer, value, formatterResolver);
135+
case ISearchAsYouTypeProperty searchAsYouTypeProperty:
136+
Serialize(ref writer, searchAsYouTypeProperty, formatterResolver);
144137
break;
145-
case "object":
146-
Serialize<IObjectProperty>(ref writer, value, formatterResolver);
138+
case IDateNanosProperty dateNanosProperty:
139+
Serialize(ref writer, dateNanosProperty, formatterResolver);
147140
break;
148-
case "nested":
149-
Serialize<INestedProperty>(ref writer, value, formatterResolver);
141+
case IBinaryProperty binaryProperty:
142+
Serialize(ref writer, binaryProperty, formatterResolver);
150143
break;
151-
case "ip":
152-
Serialize<IIpProperty>(ref writer, value, formatterResolver);
144+
case IIpProperty ipProperty:
145+
Serialize(ref writer, ipProperty, formatterResolver);
153146
break;
154-
case "geo_point":
155-
Serialize<IGeoPointProperty>(ref writer, value, formatterResolver);
147+
case IGeoPointProperty geoPointProperty:
148+
Serialize(ref writer, geoPointProperty, formatterResolver);
156149
break;
157-
case "geo_shape":
158-
Serialize<IGeoShapeProperty>(ref writer, value, formatterResolver);
150+
case IGeoShapeProperty geoShapeProperty:
151+
Serialize(ref writer, geoShapeProperty, formatterResolver);
159152
break;
160-
case "shape":
161-
Serialize<IShapeProperty>(ref writer, value, formatterResolver);
153+
case IShapeProperty shapeProperty:
154+
Serialize(ref writer, shapeProperty, formatterResolver);
162155
break;
163-
case "completion":
164-
Serialize<ICompletionProperty>(ref writer, value, formatterResolver);
156+
case ICompletionProperty completionProperty:
157+
Serialize(ref writer, completionProperty, formatterResolver);
165158
break;
166-
case "token_count":
167-
Serialize<ITokenCountProperty>(ref writer, value, formatterResolver);
159+
case ITokenCountProperty tokenCountProperty:
160+
Serialize(ref writer, tokenCountProperty, formatterResolver);
168161
break;
169-
case "murmur3":
170-
Serialize<IMurmur3HashProperty>(ref writer, value, formatterResolver);
162+
case IMurmur3HashProperty murmur3HashProperty:
163+
Serialize(ref writer, murmur3HashProperty, formatterResolver);
171164
break;
172-
case "percolator":
173-
Serialize<IPercolatorProperty>(ref writer, value, formatterResolver);
165+
case IPercolatorProperty percolatorProperty:
166+
Serialize(ref writer, percolatorProperty, formatterResolver);
174167
break;
175-
case "date_range":
176-
Serialize<IDateRangeProperty>(ref writer, value, formatterResolver);
168+
case IDateRangeProperty dateRangeProperty:
169+
Serialize(ref writer, dateRangeProperty, formatterResolver);
177170
break;
178-
case "double_range":
179-
Serialize<IDoubleRangeProperty>(ref writer, value, formatterResolver);
171+
case IDoubleRangeProperty doubleRangeProperty:
172+
Serialize(ref writer, doubleRangeProperty, formatterResolver);
180173
break;
181-
case "float_range":
182-
Serialize<IFloatRangeProperty>(ref writer, value, formatterResolver);
174+
case IFloatRangeProperty floatRangeProperty:
175+
Serialize(ref writer, floatRangeProperty, formatterResolver);
183176
break;
184-
case "integer_range":
185-
Serialize<IIntegerRangeProperty>(ref writer, value, formatterResolver);
177+
case IIntegerRangeProperty integerRangeProperty:
178+
Serialize(ref writer, integerRangeProperty, formatterResolver);
186179
break;
187-
case "long_range":
188-
Serialize<ILongRangeProperty>(ref writer, value, formatterResolver);
180+
case ILongRangeProperty longRangeProperty:
181+
Serialize(ref writer, longRangeProperty, formatterResolver);
189182
break;
190-
case "ip_range":
191-
Serialize<IIpRangeProperty>(ref writer, value, formatterResolver);
183+
case IIpRangeProperty ipRangeProperty:
184+
Serialize(ref writer, ipRangeProperty, formatterResolver);
192185
break;
193-
case "join":
194-
Serialize<IJoinProperty>(ref writer, value, formatterResolver);
186+
case IJoinProperty joinProperty:
187+
Serialize(ref writer, joinProperty, formatterResolver);
195188
break;
196-
case "alias":
197-
Serialize<IFieldAliasProperty>(ref writer, value, formatterResolver);
189+
case IFieldAliasProperty fieldAliasProperty:
190+
Serialize(ref writer, fieldAliasProperty, formatterResolver);
198191
break;
199-
case "rank_feature":
200-
Serialize<IRankFeatureProperty>(ref writer, value, formatterResolver);
192+
case IRankFeatureProperty rankFeatureProperty:
193+
Serialize(ref writer, rankFeatureProperty, formatterResolver);
201194
break;
202-
case "rank_features":
203-
Serialize<IRankFeaturesProperty>(ref writer, value, formatterResolver);
195+
case IRankFeaturesProperty rankFeaturesProperty:
196+
Serialize(ref writer, rankFeaturesProperty, formatterResolver);
204197
break;
205-
case "flattened":
206-
Serialize<IFlattenedProperty>(ref writer, value, formatterResolver);
198+
case IFlattenedProperty flattenedProperty:
199+
Serialize(ref writer, flattenedProperty, formatterResolver);
200+
break;
201+
case IGenericProperty genericProperty:
202+
Serialize(ref writer, genericProperty, formatterResolver);
207203
break;
208204
default:
209-
if (value is IGenericProperty genericProperty)
210-
Serialize<IGenericProperty>(ref writer, genericProperty, formatterResolver);
211-
else
212-
{
213-
var formatter = formatterResolver.GetFormatter<object>();
214-
formatter.Serialize(ref writer, value, formatterResolver);
215-
}
205+
var formatter = formatterResolver.GetFormatter<object>();
206+
formatter.Serialize(ref writer, value, formatterResolver);
216207
break;
217208
}
218209
}
219210

220-
private static void Serialize<TProperty>(ref JsonWriter writer, IProperty value, IJsonFormatterResolver formatterResolver)
211+
private static void Serialize<TProperty>(ref JsonWriter writer, TProperty value, IJsonFormatterResolver formatterResolver)
221212
where TProperty : class, IProperty
222213
{
223214
var formatter = formatterResolver.GetFormatter<TProperty>();
224-
formatter.Serialize(ref writer, value as TProperty, formatterResolver);
215+
formatter.Serialize(ref writer, value, formatterResolver);
225216
}
226217

227218
private static TProperty Deserialize<TProperty>(ref JsonReader reader, IJsonFormatterResolver formatterResolver)

0 commit comments

Comments
 (0)