Skip to content

Commit c045151

Browse files
authored
Fallback to 0.0.0 when runtime version is invalid (#5541)
1 parent 25e2962 commit c045151

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

src/Elasticsearch.Net/Connection/MetaData/MetaDataHeader.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,38 @@ namespace Elasticsearch.Net
88
{
99
internal sealed class MetaDataHeader
1010
{
11-
private const char _separator = ',';
11+
private const char Separator = ',';
1212

1313
private readonly string _headerValue;
1414

15+
private static readonly string RuntimeVersionString = new RuntimeVersionInfo().ToString();
16+
1517
public MetaDataHeader(VersionInfo version, string serviceIdentifier, bool isAsync)
1618
{
1719
ClientVersion = version.ToString();
18-
RuntimeVersion = new RuntimeVersionInfo().ToString();
1920
ServiceIdentifier = serviceIdentifier;
21+
RuntimeVersion = RuntimeVersionString;
2022

2123
// This code is expected to be called infrequently so we're not concerns with over optimising this
2224

2325
_headerValue = new StringBuilder(64)
24-
.Append(serviceIdentifier).Append("=").Append(ClientVersion).Append(_separator)
25-
.Append("a=").Append(isAsync ? "1" : "0").Append(_separator)
26-
.Append("net=").Append(RuntimeVersion).Append(_separator)
27-
.Append(_httpClientIdentifier).Append("=").Append(RuntimeVersion)
26+
.Append(serviceIdentifier).Append("=").Append(ClientVersion).Append(Separator)
27+
.Append("a=").Append(isAsync ? "1" : "0").Append(Separator)
28+
.Append("net=").Append(RuntimeVersion).Append(Separator)
29+
.Append(HttpClientIdentifier).Append("=").Append(RuntimeVersion)
2830
.ToString();
2931
}
3032

31-
private static readonly string _httpClientIdentifier =
33+
private static readonly string HttpClientIdentifier =
3234
#if DOTNETCORE
3335
ConnectionInfo.UsingCurlHandler ? "cu" : "so";
3436
#else
3537
"wr";
3638
#endif
3739

38-
public string ServiceIdentifier { get; private set; }
39-
public string ClientVersion { get; private set; }
40-
public string RuntimeVersion { get; private set; }
40+
public string ServiceIdentifier { get; }
41+
public string ClientVersion { get; }
42+
public string RuntimeVersion { get; }
4143

4244
public override string ToString() => _headerValue;
4345
}

src/Elasticsearch.Net/Connection/MetaData/RuntimeVersionInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace Elasticsearch.Net
4141
/// </summary>
4242
internal sealed class RuntimeVersionInfo : VersionInfo
4343
{
44-
public static readonly RuntimeVersionInfo Default = new RuntimeVersionInfo { Version = new Version(0, 0, 0), IsPrerelease = false };
44+
public static readonly RuntimeVersionInfo Default = new() { Version = new Version(0, 0, 0), IsPrerelease = false };
4545

4646
public RuntimeVersionInfo() => StoreVersion(GetRuntimeVersion());
4747

@@ -79,7 +79,7 @@ private static string GetNetCoreVersion()
7979
return runtimeVersion;
8080
}
8181

82-
//At this point, we can't identify whether this is a prerelease, but a version is better than nothing!
82+
//At this point, we can't identify whether this is a pre-release, but a version is better than nothing!
8383

8484
var frameworkName = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
8585
if (TryGetVersionFromFrameworkName(frameworkName, out runtimeVersion))
@@ -182,7 +182,7 @@ private static string GetFullFrameworkRuntime()
182182
{
183183
var version = CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
184184

185-
if (!string.IsNullOrEmpty(version) )
185+
if (!string.IsNullOrEmpty(version))
186186
return version;
187187
}
188188
}

src/Elasticsearch.Net/Connection/MetaData/VersionInfo.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ protected void StoreVersion(string fullVersion)
2020
fullVersion = EmptyVersion;
2121

2222
var clientVersion = GetParsableVersionPart(fullVersion);
23-
23+
2424
if (!Version.TryParse(clientVersion, out var parsedVersion))
25-
throw new ArgumentException("Invalid version string", nameof(fullVersion));
25+
parsedVersion = new Version(EmptyVersion);
2626

2727
var finalVersion = parsedVersion;
2828

@@ -34,14 +34,16 @@ protected void StoreVersion(string fullVersion)
3434
: 0);
3535

3636
Version = finalVersion;
37-
IsPrerelease = ContainsPrerelease(fullVersion);
37+
IsPrerelease = !IsEmpty(parsedVersion) && ContainsPrerelease(fullVersion);
3838
}
3939

40+
private bool IsEmpty(Version version) => version.Major == 0 && version.Minor == 0 && version.Build == 0;
41+
4042
protected virtual bool ContainsPrerelease(string version) => version.Contains("-");
4143

4244
private static string GetParsableVersionPart(string fullVersionName) =>
43-
new string(fullVersionName.TakeWhile(c => char.IsDigit(c) || c == '.').ToArray());
45+
new(fullVersionName.TakeWhile(c => char.IsDigit(c) || c == '.').ToArray());
4446

45-
public override string ToString() => IsPrerelease ? Version.ToString() + "p" : Version.ToString();
47+
public override string ToString() => IsPrerelease ? $"{Version}p" : Version.ToString();
4648
}
4749
}

tests/Tests/Connection/MetaData/MetaHeaderProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using FluentAssertions;
99
using Nest;
1010

11-
namespace Tests.Core.Connection.MetaData
11+
namespace Tests.Connection.MetaData
1212
{
1313
public class MetaHeaderProviderTests
1414
{

tests/Tests/Connection/MetaData/VersionInfoTests.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,36 @@
77
using Elasticsearch.Net;
88
using FluentAssertions;
99

10-
namespace Tests.Core.Connection.MetaData
10+
namespace Tests.Connection.MetaData
1111
{
1212
public class VersionInfoTests
1313
{
1414
[U] public void ToString_ReturnsExpectedValue_ForNonPrerelease()
1515
{
16-
var sut = new TestVersionInfo("1.2.3", false);
16+
var sut = new TestableVersionInfo("1.2.3");
1717
sut.ToString().Should().Be("1.2.3");
1818
}
1919

2020
[U] public void ToString_ReturnsExpectedValue_ForPrerelease()
2121
{
22-
var sut = new TestVersionInfo("1.2.3", true);
22+
var sut = new TestableVersionInfo("1.2.3-beta-1");
2323
sut.ToString().Should().Be("1.2.3p");
2424
}
2525

26+
[U]
27+
public void ToString_ReturnsExpectedValue_ForNullVersion()
28+
{
29+
var sut = new TestableVersionInfo(null);
30+
sut.ToString().Should().Be("0.0.0");
31+
}
32+
33+
[U]
34+
public void ToString_ReturnsExpectedValue_ForInvalidVersion()
35+
{
36+
var sut = new TestableVersionInfo("NOT-A-VERSION-NUMBER");
37+
sut.ToString().Should().Be("0.0.0");
38+
}
39+
2640
private class TestVersionInfo : VersionInfo
2741
{
2842
public TestVersionInfo(string version, bool isPrerelease)
@@ -31,5 +45,10 @@ public TestVersionInfo(string version, bool isPrerelease)
3145
IsPrerelease = isPrerelease;
3246
}
3347
}
48+
49+
private class TestableVersionInfo : VersionInfo
50+
{
51+
public TestableVersionInfo(string version) => StoreVersion(version);
52+
}
3453
}
3554
}

0 commit comments

Comments
 (0)