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
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ public override bool IsExpectedResponseContentType(string accept, string? respon
return false;
}

/// <inheritdoc cref="ProductRegistration.IsJsonContentType"/>
/// <remarks>
/// In addition to the base <c>application/json</c> check, accepts
/// <c>application/vnd.elasticsearch+json</c> (and any <c>;compatible-with=N</c>
/// suffix variant), which Elasticsearch returns when the client sends
/// <c>Accept: application/vnd.elasticsearch+json</c>.
/// </remarks>
public override bool IsJsonContentType(string? contentType) =>
base.IsJsonContentType(contentType) ||
(contentType != null && contentType.StartsWith("application/vnd.elasticsearch+json", StringComparison.OrdinalIgnoreCase));

private static bool TryParseElasticsearchVendorMime(string mime, out string bareVendor, out string bareForm)
{
const string prefix = "application/vnd.elasticsearch+";
Expand Down Expand Up @@ -389,10 +400,7 @@ public override IReadOnlyCollection<string> DefaultHeadersToParse()
];

/// <inheritdoc />
public override bool IsErrorContentType(string? contentType) =>
contentType is not null && (
contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase) ||
contentType.StartsWith("application/vnd.elasticsearch+json", StringComparison.OrdinalIgnoreCase));
public override bool IsErrorContentType(string? contentType) => IsJsonContentType(contentType);

/// <inheritdoc />
public override ErrorResponse? TryExtractError(BoundConfiguration boundConfiguration, Stream responseStream)
Expand Down
11 changes: 11 additions & 0 deletions src/Elastic.Transport/Products/ProductRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public virtual bool IsExpectedResponseContentType(string accept, string? respons
/// </summary>
public virtual IReadOnlyCollection<IResponseBuilder> ResponseBuilders { get; } = [new DefaultResponseBuilder()];

/// <summary>
/// Determines whether the response <c>Content-Type</c> indicates that the body should be
/// parsed as a JSON document. The default accepts any <c>application/json</c> type
/// (including those with parameters such as <c>;charset=utf-8</c>). Products that use
/// vendor MIME types for JSON payloads (e.g. <c>application/vnd.elasticsearch+json</c>)
/// should override this to return <c>true</c> for those types too.
/// </summary>
/// <param name="contentType">The <c>Content-Type</c> header value from the response.</param>
public virtual bool IsJsonContentType(string? contentType) =>
contentType != null && contentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Determines whether the given response content-type indicates a product-specific error body
/// that can be deserialized by <see cref="TryExtractError"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static async ValueTask<T> BuildCoreAsync(bool isAsync, ApiCallDetails ap
string contentType, long contentLength, CancellationToken cancellationToken = default)
{
//if not json store the result under "body"
if (contentType == null || !contentType.StartsWith(BoundConfiguration.DefaultContentType, StringComparison.Ordinal))
if (!boundConfiguration.ConnectionSettings.ProductRegistration.IsJsonContentType(contentType))
{
DynamicDictionary dictionary;
string stringValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static async ValueTask<T> BuildCoreAsync(bool isAsync, ApiCallDetails ap
string contentType, long contentLength, CancellationToken cancellationToken = default)
{
// If not JSON, store the result under "body"
if (contentType == null || !contentType.StartsWith(BoundConfiguration.DefaultContentType, StringComparison.Ordinal))
if (!boundConfiguration.ConnectionSettings.ProductRegistration.IsJsonContentType(contentType))
{
string stringValue;

Expand Down
41 changes: 41 additions & 0 deletions tests/Elastic.Transport.Tests/Responses/Json/JsonResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Elastic.Transport.Products.Elasticsearch;
using FluentAssertions;
using Xunit;

Expand Down Expand Up @@ -57,6 +58,46 @@ public async Task BuilderNonJsonContent()
result.Get<string>("body").Should().Be("This is not JSON");
}

[Theory]
[InlineData("application/vnd.elasticsearch+json")]
[InlineData("application/vnd.elasticsearch+json;compatible-with=8")]
[InlineData("application/vnd.elasticsearch+json; compatible-with=8")]
public async Task BuilderElasticsearchVendorJsonContentType(string vendorContentType)
{
IResponseBuilder sut = new JsonResponseBuilder();
var config = new TransportConfiguration(productRegistration: ElasticsearchProductRegistration.Default);
var apiCallDetails = new ApiCallDetails();
var boundConfiguration = new BoundConfiguration(config);

var data = Encoding.UTF8.GetBytes("""{"task":"abc123:1"}""");
var stream = new MemoryStream(data);

var result = await sut.BuildAsync<JsonResponse>(apiCallDetails, boundConfiguration, stream, vendorContentType, data.Length);
result.Get<string>("task").Should().Be("abc123:1");

stream.Position = 0;

result = sut.Build<JsonResponse>(apiCallDetails, boundConfiguration, stream, vendorContentType, data.Length);
result.Get<string>("task").Should().Be("abc123:1");
}

[Theory]
[InlineData("application/vnd.elasticsearch+x-ndjson")]
[InlineData("application/vnd.elasticsearch+vnd.mapbox-vector-tile")]
public async Task BuilderElasticsearchVendorNonJsonContentType(string vendorContentType)
{
IResponseBuilder sut = new JsonResponseBuilder();
var config = new TransportConfiguration(productRegistration: ElasticsearchProductRegistration.Default);
var apiCallDetails = new ApiCallDetails();
var boundConfiguration = new BoundConfiguration(config);

var data = Encoding.UTF8.GetBytes("raw body");
var stream = new MemoryStream(data);

var result = await sut.BuildAsync<JsonResponse>(apiCallDetails, boundConfiguration, stream, vendorContentType, data.Length);
result.Get<string>("body").Should().Be("raw body");
}

// --- Direct DOM access ---

[Fact]
Expand Down
Loading