Skip to content

Commit fd9ce4d

Browse files
Deserialize enums in fallback Enum.Parse as case insensitive (#4833) (#4842)
This commit updates the EnumFormatter to parse case-insensitive when falling back to the Enum.Parse method, which happens when a value is not found in a mapping built from EnumMember/DataMember value or field name. Fixes #4817 Co-authored-by: Russ Cam <russ.cam@elastic.co>
1 parent f50ab74 commit fd9ce4d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Elasticsearch.Net/Utf8Json/Formatters/EnumFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterReso
185185
if (!nameValueMapping.TryGetValue(key, out value))
186186
{
187187
var str = StringEncoding.UTF8.GetString(key.Array, key.Offset, key.Count);
188-
value = (T)Enum.Parse(typeof(T), str); // Enum.Parse is slow
188+
value = (T)Enum.Parse(typeof(T), str, true); // Enum.Parse is slow
189189
}
190190
return value;
191191
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.IO;
7+
using System.Text;
8+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9+
using FluentAssertions;
10+
using Nest;
11+
using Tests.Core.Client;
12+
13+
namespace Tests.Reproduce
14+
{
15+
public class GitHubIssue4817
16+
{
17+
[U]
18+
public void DeserializeCaseInsensitiveEnum()
19+
{
20+
var json = "{\"default_operator\":\"AND\",\"query\":\"Hans Mueller\"}";
21+
var bytes = Encoding.UTF8.GetBytes(json);
22+
var elasticClient = new ElasticClient();
23+
24+
Func<QueryStringQuery> func = () => elasticClient.RequestResponseSerializer.Deserialize<QueryStringQuery>(new MemoryStream(bytes));
25+
26+
var query = func.Should().NotThrow().Subject;
27+
query.DefaultOperator.Should().Be(Operator.And);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)