Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Elastic.Transport/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Elastic.Transport.Extensions;

internal static class EnumExtensions
{
public static string GetEnumName(this Enum e)
{
var attributes = e.GetType().GetField(e.ToString())?.GetCustomAttributes(typeof(EnumMemberAttribute), false);
if (attributes is null || attributes.Length == 0)
return e.ToString();

var enumMember = attributes[0] as EnumMemberAttribute;
return enumMember?.Value ?? e.ToString();
}
}

internal static class Extensions
{
[Obsolete("Please use the overload placing the enumerated array as out")]
Expand Down
2 changes: 1 addition & 1 deletion src/Elastic.Transport/Requests/UrlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static string CreateString(object? value, ITransportConfiguration setting
null => string.Empty,
string s => s,
string[] ss => string.Join(",", ss),
Enum e => e.ToString(),
Enum e => e.GetEnumName(),
bool b => b ? "true" : "false",
DateTimeOffset offset => offset.ToString("o"),
TimeSpan timeSpan => timeSpan.ToTimeUnit(),
Expand Down
27 changes: 27 additions & 0 deletions tests/Elastic.Transport.Tests/UsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Elastic.Transport.Products;
using Elastic.Transport.Products.Elasticsearch;
using FluentAssertions;
Expand All @@ -15,6 +17,31 @@ namespace Elastic.Transport.Tests;

public class UsageTests
{
private enum MyEnum
{
[EnumMember(Value = "different")]
Value
}

[Fact]
public void EnumsUseEnumMemberAttribute()
{
var pool = new StaticNodePool([new Node(new Uri("http://localhost:9200"))]);
var requestInvoker = new InMemoryRequestInvoker();
var serializer = LowLevelRequestResponseSerializer.Instance;
var product = ElasticsearchProductRegistration.Default;

var settings = new TransportConfiguration(pool, requestInvoker, serializer, product);
var transport = new DistributedTransport<TransportConfiguration>(settings);

var requestParameters = new DefaultRequestParameters { QueryString =
new Dictionary<string, object>{ { "enum", MyEnum.Value } } };
var path = requestParameters.CreatePathWithQueryStrings("/", settings);
var response = transport.Request<StringResponse>(new EndpointPath(HttpMethod.GET, path), null, null, null);

response.ApiCallDetails.Uri.Should().Be(new Uri("http://localhost:9200?enum=different"));
}

[Fact]
public void TransportVersionIsSet()
{
Expand Down
Loading