From 8eda127d4354730894bab992906d47046327dbc7 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Thu, 14 Nov 2024 07:02:43 +0000 Subject: [PATCH] Fix tests and BOM --- .../Components/Pipeline/RequestData.cs | 33 ++++++++++++++++--- .../Configuration/IRequestConfiguration.cs | 2 +- .../Configuration/RequestConfiguration.cs | 1 + .../RequestConfigurationDescriptor.cs | 4 ++- .../RequestConfigurationTests.cs | 2 +- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Elastic.Transport/Components/Pipeline/RequestData.cs b/src/Elastic.Transport/Components/Pipeline/RequestData.cs index 87650a9..f8a7b2b 100644 --- a/src/Elastic.Transport/Components/Pipeline/RequestData.cs +++ b/src/Elastic.Transport/Components/Pipeline/RequestData.cs @@ -40,9 +40,6 @@ public RequestData(ITransportConfiguration global, IRequestConfiguration? local ProxyPassword = global.ProxyPassword; DisableAutomaticProxyDetection = global.DisableAutomaticProxyDetection; UserAgent = global.UserAgent; - ResponseBuilders = local?.ResponseBuilders ?? global.ResponseBuilders; - ProductResponseBuilders = global.ProductRegistration.ResponseBuilders; - KeepAliveInterval = (int)(global.KeepAliveInterval?.TotalMilliseconds ?? 2000); KeepAliveTime = (int)(global.KeepAliveTime?.TotalMilliseconds ?? 2000); RunAs = local?.RunAs ?? global.RunAs; @@ -89,8 +86,36 @@ public RequestData(ITransportConfiguration global, IRequestConfiguration? local Headers ??= []; Headers.Add(OpaqueIdHeader, local.OpaqueId); } - } + // If there are builders set at the transport level and on the request config, we combine them, + // prioritising the request config response builders as most specific. + if (local is not null && local.ResponseBuilders.Count > 0 && global.ResponseBuilders.Count > 0) + { + var builders = new IResponseBuilder[local.ResponseBuilders.Count + global.ResponseBuilders.Count]; + + var counter = 0; + foreach (var builder in local.ResponseBuilders) + { + builders[counter++] = builder; + } + foreach (var builder in global.ResponseBuilders) + { + builders[counter++] = builder; + } + + ResponseBuilders = builders; + } + else if (local is not null && local.ResponseBuilders.Count > 0) + { + ResponseBuilders = local.ResponseBuilders; + } + else + { + ResponseBuilders = global.ResponseBuilders; + } + + ProductResponseBuilders = global.ProductRegistration.ResponseBuilders; + } /// public MemoryStreamFactory MemoryStreamFactory { get; } diff --git a/src/Elastic.Transport/Configuration/IRequestConfiguration.cs b/src/Elastic.Transport/Configuration/IRequestConfiguration.cs index f594502..39934db 100644 --- a/src/Elastic.Transport/Configuration/IRequestConfiguration.cs +++ b/src/Elastic.Transport/Configuration/IRequestConfiguration.cs @@ -1,4 +1,4 @@ -// Licensed to Elasticsearch B.V under one or more agreements. +// Licensed to Elasticsearch B.V under one or more agreements. // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information diff --git a/src/Elastic.Transport/Configuration/RequestConfiguration.cs b/src/Elastic.Transport/Configuration/RequestConfiguration.cs index f175d74..940d69b 100644 --- a/src/Elastic.Transport/Configuration/RequestConfiguration.cs +++ b/src/Elastic.Transport/Configuration/RequestConfiguration.cs @@ -64,6 +64,7 @@ public RequestConfiguration(IRequestConfiguration config) ResponseHeadersToParse = (config.ResponseHeadersToParse is null) ? null : new HeadersList(config.ResponseHeadersToParse); ParseAllHeaders = config.ParseAllHeaders; RequestMetaData = config.RequestMetaData; + ResponseBuilders = config.ResponseBuilders; } /// diff --git a/src/Elastic.Transport/Configuration/RequestConfigurationDescriptor.cs b/src/Elastic.Transport/Configuration/RequestConfigurationDescriptor.cs index b8957bc..552fbdd 100644 --- a/src/Elastic.Transport/Configuration/RequestConfigurationDescriptor.cs +++ b/src/Elastic.Transport/Configuration/RequestConfigurationDescriptor.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Collections.Specialized; +using System.Linq; using System.Security.Cryptography.X509Certificates; using Elastic.Transport.Extensions; @@ -49,6 +50,7 @@ public RequestConfigurationDescriptor(IRequestConfiguration? config) _responseHeadersToParse = (config.ResponseHeadersToParse is null) ? null : new HeadersList(config.ResponseHeadersToParse); _parseAllHeaders = config.ParseAllHeaders; _requestMetaData = config.RequestMetaData; + _responseBuilders = [.. config.ResponseBuilders]; } private string? _accept; @@ -336,5 +338,5 @@ public RequestConfigurationDescriptor ResponseBuilder(IResponseBuilder responseB RequestMetaData? IRequestConfiguration.RequestMetaData => _requestMetaData; - IReadOnlyCollection IRequestConfiguration.ResponseBuilders => _responseBuilders; + IReadOnlyCollection IRequestConfiguration.ResponseBuilders => _responseBuilders ?? []; } diff --git a/tests/Elastic.Transport.Tests/Configuration/RequestConfigurationTests.cs b/tests/Elastic.Transport.Tests/Configuration/RequestConfigurationTests.cs index 4380c75..bd06dd5 100644 --- a/tests/Elastic.Transport.Tests/Configuration/RequestConfigurationTests.cs +++ b/tests/Elastic.Transport.Tests/Configuration/RequestConfigurationTests.cs @@ -38,7 +38,7 @@ public void SameDefaults() public void CopiesAllProperties() { var autoFaker = new AutoFaker(); - autoFaker.RuleFor(x => x.ClientCertificates, f => new X509CertificateCollection()); + autoFaker.RuleFor(x => x.ClientCertificates, f => []); var config = autoFaker.Generate(); config.Accept.Should().NotBeEmpty();