Skip to content

Commit 239ea28

Browse files
authored
Improvements to reflection version info (#62)
* Add playground project * Avoid creating vendor API version compatibility default mime type if version unknown * Attempt to get version from the AssemblyFileVersionAttribute * Update test naming * Ensure we check for null attribute * Refactor ReflectionVersionInfo for robustness * FIx license header
1 parent 27cec74 commit 239ea28

File tree

7 files changed

+71
-17
lines changed

7 files changed

+71
-17
lines changed

Playground/Playground.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.0.4" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\src\Elastic.Transport\Elastic.Transport.csproj" />
16+
</ItemGroup>
17+
18+
</Project>

Playground/Program.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 Elastic.Transport.Products.Elasticsearch;
6+
7+
var registration = new ElasticsearchProductRegistration(typeof(Elastic.Clients.Elasticsearch.ElasticsearchClient));
8+
9+
Console.WriteLine(registration.DefaultMimeType ?? "NOT SPECIFIED");

elastic-transport-net.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29806.167
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33213.308
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "scripts", "build\scripts\scripts.fsproj", "{4779DD6F-AE49-4A80-9F67-D9F2DB05E557}"
77
EndProject
@@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.Transport.Integrati
4040
EndProject
4141
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.Transport.Profiling", "benchmarks\Elastic.Transport.Profiling\Elastic.Transport.Profiling.csproj", "{ED4E89BE-FBE9-4876-979C-63A0E3BC5419}"
4242
EndProject
43+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Playground", "Playground\Playground.csproj", "{5EE4DC72-B337-448B-802A-6158F4D90667}"
44+
EndProject
4345
Global
4446
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4547
Debug|Any CPU = Debug|Any CPU
@@ -74,6 +76,10 @@ Global
7476
{ED4E89BE-FBE9-4876-979C-63A0E3BC5419}.Debug|Any CPU.Build.0 = Debug|Any CPU
7577
{ED4E89BE-FBE9-4876-979C-63A0E3BC5419}.Release|Any CPU.ActiveCfg = Release|Any CPU
7678
{ED4E89BE-FBE9-4876-979C-63A0E3BC5419}.Release|Any CPU.Build.0 = Release|Any CPU
79+
{5EE4DC72-B337-448B-802A-6158F4D90667}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
80+
{5EE4DC72-B337-448B-802A-6158F4D90667}.Debug|Any CPU.Build.0 = Debug|Any CPU
81+
{5EE4DC72-B337-448B-802A-6158F4D90667}.Release|Any CPU.ActiveCfg = Release|Any CPU
82+
{5EE4DC72-B337-448B-802A-6158F4D90667}.Release|Any CPU.Build.0 = Release|Any CPU
7783
EndGlobalSection
7884
GlobalSection(SolutionProperties) = preSolution
7985
HideSolutionNode = FALSE
@@ -86,6 +92,7 @@ Global
8692
{DABC7F0B-6174-4B2D-8F17-5E36C1CE43EF} = {BBB0AC81-F09D-4895-84E2-7E933D608E78}
8793
{3B27DE76-1188-4078-8828-67AFD39BAC10} = {3582B07D-C2B0-49CC-B676-EAF806EB010E}
8894
{ED4E89BE-FBE9-4876-979C-63A0E3BC5419} = {BBB0AC81-F09D-4895-84E2-7E933D608E78}
95+
{5EE4DC72-B337-448B-802A-6158F4D90667} = {7610B796-BB3E-4CB2-8296-79BBFF6D23FC}
8996
EndGlobalSection
9097
GlobalSection(ExtensibilityGlobals) = postSolution
9198
SolutionGuid = {7F60C4BB-6216-4E50-B1E4-9C38EB484843}

src/Elastic.Transport/Products/Elasticsearch/ElasticsearchProductRegistration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public ElasticsearchProductRegistration(Type markerType) : this()
3535
{
3636
var clientVersionInfo = ReflectionVersionInfo.Create(markerType);
3737
_metaHeaderProvider = new DefaultMetaHeaderProvider(clientVersionInfo, "es");
38-
_clientMajorVersion = clientVersionInfo.Version.Major;
38+
39+
// Only set this if we have a version.
40+
// If we don't have a version we won't apply the vendor-based REST API compatibilty Accept header.
41+
if (clientVersionInfo.Version.Major > 0)
42+
_clientMajorVersion = clientVersionInfo.Version.Major;
3943
}
4044

4145
/// <summary> A static instance of <see cref="ElasticsearchProductRegistration"/> to promote reuse </summary>

src/Elastic.Transport/Requests/MetaData/ReflectionVersionInfo.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,42 @@ public static ReflectionVersionInfo Create(Type type)
3535

3636
private static string DetermineVersionFromType(Type type)
3737
{
38+
var productVersion = EmptyVersion;
39+
3840
try
3941
{
40-
var productVersion = FileVersionInfo.GetVersionInfo(type.GetTypeInfo().Assembly.Location)?.ProductVersion ?? EmptyVersion;
42+
productVersion = type.Assembly?.GetCustomAttribute<AssemblyVersionAttribute>()?.Version ?? EmptyVersion;
43+
}
44+
catch
45+
{
46+
// ignore failures and fall through
47+
}
4148

49+
try
50+
{
4251
if (productVersion == EmptyVersion)
43-
productVersion = Assembly.GetAssembly(type).GetName().Version.ToString();
44-
45-
var match = VersionRegex.Match(productVersion);
52+
productVersion = FileVersionInfo.GetVersionInfo(type.Assembly.Location)?.ProductVersion ?? EmptyVersion;
53+
}
54+
catch
55+
{
56+
// ignore failures and fall through
57+
}
4658

47-
return match.Success ? match.Value : EmptyVersion;
59+
try
60+
{
61+
// This fallback may not include the minor version numbers
62+
if (productVersion == EmptyVersion)
63+
productVersion = type.Assembly.GetName()?.Version?.ToString() ?? EmptyVersion;
4864
}
4965
catch
5066
{
5167
// ignore failures and fall through
5268
}
5369

54-
return EmptyVersion;
70+
if (productVersion == EmptyVersion) return EmptyVersion;
71+
72+
var match = VersionRegex.Match(productVersion);
73+
74+
return match.Success ? match.Value : EmptyVersion;
5575
}
5676
}

tests/Elastic.Transport.IntegrationTests/Elastic.Transport.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.0.2" />
11+
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.0.4" />
1212
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.1" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
1414
<PackageReference Include="xunit" Version="2.4.1" />

tests/Elastic.Transport.IntegrationTests/Http/MetaHeaderTests.cs renamed to tests/Elastic.Transport.IntegrationTests/Http/ApiCompatibilityHeaderTests.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313

1414
namespace Elastic.Transport.IntegrationTests.Http;
1515

16-
/// <summary>
17-
/// Tests that the test framework loads a controller and the exposed transport can talk to its endpoints.
18-
/// Tests runs against a server that started up once and its server instance shared among many test classes
19-
/// </summary>
20-
public class MetaHeaderTests : AssemblyServerTestsBase
16+
public class ApiCompatibilityHeaderTests : AssemblyServerTestsBase
2117
{
22-
public MetaHeaderTests(TransportTestServer instance) : base(instance) { }
18+
public ApiCompatibilityHeaderTests(TransportTestServer instance) : base(instance) { }
2319

2420
[Fact]
25-
public async Task AddsExpectedMetaHeader()
21+
public async Task AddsExpectedVendorInformationForRestApiCompaitbility()
2622
{
2723
var connection = new TestableHttpConnection(responseMessage =>
2824
{

0 commit comments

Comments
 (0)