Skip to content

[Backport 8.3] Fix to ensure dynamic HTTP methods are used when available #7062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
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
3 changes: 0 additions & 3 deletions src/Elastic.Clients.Elasticsearch/Api/IndexRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public partial class IndexRequest<TDocument> : ICustomJsonWriter
{
public IndexRequest() : this(typeof(TDocument)) { }

//public IndexRequest(TDocument document) : this(typeof(TDocument)) => Document = document;

public IndexRequest(TDocument document, Id id) : this(typeof(TDocument), id) => Document = document;

protected override HttpMethod? DynamicHttpMethod => GetHttpMethod(this);
Expand All @@ -34,7 +32,6 @@ internal static HttpMethod GetHttpMethod(IndexRequest<TDocument> request) =>

public sealed partial class IndexRequestDescriptor<TDocument> : ICustomJsonWriter
{
// TODO: Codegen
public IndexRequestDescriptor<TDocument> Document(TDocument document)
{
DocumentValue = document;
Expand Down
6 changes: 3 additions & 3 deletions src/Elastic.Clients.Elasticsearch/Core/Fluent/Descriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Elastic.Clients.Elasticsearch.Fluent;

public abstract class Descriptor
{
// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal Descriptor() { }

Expand Down Expand Up @@ -49,7 +49,7 @@ public abstract class Descriptor<TDescriptor> : Descriptor
{
private readonly TDescriptor _self;

// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal Descriptor() : base() => _self = (TDescriptor)this;

Expand All @@ -64,7 +64,7 @@ public abstract class Descriptor<TDescriptor> : Descriptor
public abstract class SerializableDescriptor<TDescriptor> : Descriptor<TDescriptor>, ISelfSerializable
where TDescriptor : SerializableDescriptor<TDescriptor>
{
// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal SerializableDescriptor(): base() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class IsADictionaryDescriptor<TDescriptor, TPromised, TKey, TVal
where TDescriptor : IsADictionaryDescriptor<TDescriptor, TPromised, TKey, TValue>
where TPromised : class, IIsADictionary<TKey, TValue>
{
// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal IsADictionaryDescriptor(TPromised instance) : base(instance) { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class PromiseDescriptor<TDescriptor, TValue> : Descriptor, IProm
{
internal readonly TValue PromisedValue;

// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal PromiseDescriptor(TValue instance) : base()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Elastic.Clients.Elasticsearch.Requests;
public abstract class PlainRequest<TParameters> : Request<TParameters>
where TParameters : RequestParameters, new()
{
// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal PlainRequest() { }

Expand Down
36 changes: 29 additions & 7 deletions src/Elastic.Clients.Elasticsearch/Core/Request/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,43 @@

namespace Elastic.Clients.Elasticsearch.Requests;

/// <summary>
/// Base type for requests sent by the client.
/// </summary>
public abstract class Request
{
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal Request() { }

internal virtual string? Accept { get; } = null;

internal virtual string? ContentType { get; } = null;

[JsonIgnore] internal abstract HttpMethod HttpMethod { get; }

[JsonIgnore] internal abstract bool SupportsBody { get; }

[JsonIgnore] protected RouteValues RouteValues { get; } = new();

[JsonIgnore] protected virtual HttpMethod? DynamicHttpMethod => null;
/// <summary>
/// The default HTTP method for the request which is based on the Elasticsearch Specification endpoint definition.
/// </summary>
[JsonIgnore]
protected abstract HttpMethod StaticHttpMethod { get; }

[JsonIgnore]
internal abstract bool SupportsBody { get; }

[JsonIgnore]
protected RouteValues RouteValues { get; } = new();

/// <summary>
/// Allows for per request replacement of the specified HTTP method, including scenarios such as indexing which
/// require access to the document to determine the correct URL path and method combination to choose.
/// </summary>
[JsonIgnore]
protected virtual HttpMethod? DynamicHttpMethod => null;

/// <summary>
/// The final HTTP method used to send the request to the Elasticsearch server.
/// </summary>
[JsonIgnore]
internal HttpMethod HttpMethod => DynamicHttpMethod ?? StaticHttpMethod;

internal abstract ApiUrls ApiUrls { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract partial class RequestDescriptor<TDescriptor, TParameters> : Requ

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings) => Serialize(writer, options, settings);

// This internal ctor ensures that only types defined within the client assembly can derive from this base class.
// This internal ctor ensures that only types defined within the Elastic.Clients.Elasticsearch assembly can derive from this base class.
// We don't expect consumers to derive from this public base class.
internal RequestDescriptor() => _descriptor = (TDescriptor)this;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AsyncSearchStatusRequest(Elastic.Clients.Elasticsearch.Id id) : base(r =>
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchStatus;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
}

Expand All @@ -54,7 +54,7 @@ internal AsyncSearchStatusRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchStatus;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
public AsyncSearchStatusRequestDescriptor<TDocument> Id(Elastic.Clients.Elasticsearch.Id id)
{
Expand All @@ -79,7 +79,7 @@ internal AsyncSearchStatusRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchStatus;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
public AsyncSearchStatusRequestDescriptor Id(Elastic.Clients.Elasticsearch.Id id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public DeleteAsyncSearchRequest(Elastic.Clients.Elasticsearch.Id id) : base(r =>
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchDelete;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => false;
}

Expand All @@ -54,7 +54,7 @@ internal DeleteAsyncSearchRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchDelete;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => false;
public DeleteAsyncSearchRequestDescriptor<TDocument> Id(Elastic.Clients.Elasticsearch.Id id)
{
Expand All @@ -79,7 +79,7 @@ internal DeleteAsyncSearchRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchDelete;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => false;
public DeleteAsyncSearchRequestDescriptor Id(Elastic.Clients.Elasticsearch.Id id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public GetAsyncSearchRequest(Elastic.Clients.Elasticsearch.Id id) : base(r => r.
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchGet;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Duration? KeepAlive { get => Q<Elastic.Clients.Elasticsearch.Duration?>("keep_alive"); set => Q("keep_alive", value); }
Expand All @@ -70,7 +70,7 @@ internal GetAsyncSearchRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchGet;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
public GetAsyncSearchRequestDescriptor<TDocument> KeepAlive(Elastic.Clients.Elasticsearch.Duration? keepAlive) => Qs("keep_alive", keepAlive);
public GetAsyncSearchRequestDescriptor<TDocument> TypedKeys(bool? typedKeys = true) => Qs("typed_keys", typedKeys);
Expand Down Expand Up @@ -98,7 +98,7 @@ internal GetAsyncSearchRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchGet;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
public GetAsyncSearchRequestDescriptor KeepAlive(Elastic.Clients.Elasticsearch.Duration? keepAlive) => Qs("keep_alive", keepAlive);
public GetAsyncSearchRequestDescriptor TypedKeys(bool? typedKeys = true) => Qs("typed_keys", typedKeys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public SubmitAsyncSearchRequest(Elastic.Clients.Elasticsearch.Indices? indices)
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchSubmit;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Duration? WaitForCompletionTimeout { get => Q<Elastic.Clients.Elasticsearch.Duration?>("wait_for_completion_timeout"); set => Q("wait_for_completion_timeout", value); }
Expand Down Expand Up @@ -782,7 +782,7 @@ public SubmitAsyncSearchRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchSubmit;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
public SubmitAsyncSearchRequestDescriptor<TDocument> SourceExcludes(Elastic.Clients.Elasticsearch.Fields? sourceExcludes) => Qs("_source_excludes", sourceExcludes);
public SubmitAsyncSearchRequestDescriptor<TDocument> SourceIncludes(Elastic.Clients.Elasticsearch.Fields? sourceIncludes) => Qs("_source_includes", sourceIncludes);
Expand Down Expand Up @@ -1815,7 +1815,7 @@ public SubmitAsyncSearchRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.AsyncSearchSubmit;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
public SubmitAsyncSearchRequestDescriptor SourceExcludes(Elastic.Clients.Elasticsearch.Fields? sourceExcludes) => Qs("_source_excludes", sourceExcludes);
public SubmitAsyncSearchRequestDescriptor SourceIncludes(Elastic.Clients.Elasticsearch.Fields? sourceIncludes) => Qs("_source_includes", sourceIncludes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public BulkRequest(Elastic.Clients.Elasticsearch.IndexName? index) : base(r => r
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceBulk;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
[JsonIgnore]
public string? Pipeline { get => Q<string?>("pipeline"); set => Q("pipeline", value); }
Expand Down Expand Up @@ -106,7 +106,7 @@ public BulkRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceBulk;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
public BulkRequestDescriptor<TDocument> Source(Elastic.Clients.Elasticsearch.Core.Search.SourceConfigParam? source) => Qs("_source", source);
public BulkRequestDescriptor<TDocument> SourceExcludes(Elastic.Clients.Elasticsearch.Fields? sourceExcludes) => Qs("_source_excludes", sourceExcludes);
Expand Down Expand Up @@ -136,7 +136,7 @@ public BulkRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceBulk;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
public BulkRequestDescriptor Source(Elastic.Clients.Elasticsearch.Core.Search.SourceConfigParam? source) => Qs("_source", source);
public BulkRequestDescriptor SourceExcludes(Elastic.Clients.Elasticsearch.Fields? sourceExcludes) => Qs("_source_excludes", sourceExcludes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class ClearScrollRequestParameters : RequestParameters
public sealed partial class ClearScrollRequest : PlainRequest<ClearScrollRequestParameters>
{
internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceClearScroll;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => true;
[JsonInclude]
[JsonPropertyName("scroll_id")]
Expand All @@ -49,7 +49,7 @@ public ClearScrollRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceClearScroll;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => true;
private Elastic.Clients.Elasticsearch.ScrollIds? ScrollIdValue { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class ClosePointInTimeRequestParameters : RequestParameters
public sealed partial class ClosePointInTimeRequest : PlainRequest<ClosePointInTimeRequestParameters>
{
internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceClosePointInTime;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => true;
[JsonInclude]
[JsonPropertyName("id")]
Expand All @@ -49,7 +49,7 @@ public ClosePointInTimeRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceClosePointInTime;
internal override HttpMethod HttpMethod => HttpMethod.DELETE;
protected override HttpMethod StaticHttpMethod => HttpMethod.DELETE;
internal override bool SupportsBody => true;
private Elastic.Clients.Elasticsearch.Id IdValue { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public HealthRequest(Elastic.Clients.Elasticsearch.Indices? indices) : base(r =>
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.ClusterHealth;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
[JsonIgnore]
public ICollection<Elastic.Clients.Elasticsearch.ExpandWildcard>? ExpandWildcards { get => Q<ICollection<Elastic.Clients.Elasticsearch.ExpandWildcard>?>("expand_wildcards"); set => Q("expand_wildcards", value); }
Expand Down Expand Up @@ -118,7 +118,7 @@ public HealthRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.ClusterHealth;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
public HealthRequestDescriptor<TDocument> ExpandWildcards(ICollection<Elastic.Clients.Elasticsearch.ExpandWildcard>? expandWildcards) => Qs("expand_wildcards", expandWildcards);
public HealthRequestDescriptor<TDocument> Level(Elastic.Clients.Elasticsearch.Level? level) => Qs("level", level);
Expand Down Expand Up @@ -150,7 +150,7 @@ public HealthRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.ClusterHealth;
internal override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod StaticHttpMethod => HttpMethod.GET;
internal override bool SupportsBody => false;
public HealthRequestDescriptor ExpandWildcards(ICollection<Elastic.Clients.Elasticsearch.ExpandWildcard>? expandWildcards) => Qs("expand_wildcards", expandWildcards);
public HealthRequestDescriptor Level(Elastic.Clients.Elasticsearch.Level? level) => Qs("level", level);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public CountRequest(Elastic.Clients.Elasticsearch.Indices? indices) : base(r =>
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceCount;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
[JsonIgnore]
public bool? AllowNoIndices { get => Q<bool?>("allow_no_indices"); set => Q("allow_no_indices", value); }
Expand Down Expand Up @@ -144,7 +144,7 @@ public CountRequestDescriptor(Elasticsearch.Indices? indices) : base(r => r.Opti
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceCount;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
public CountRequestDescriptor<TDocument> AllowNoIndices(bool? allowNoIndices = true) => Qs("allow_no_indices", allowNoIndices);
public CountRequestDescriptor<TDocument> AnalyzeWildcard(bool? analyzeWildcard = true) => Qs("analyze_wildcard", analyzeWildcard);
Expand Down Expand Up @@ -231,7 +231,7 @@ public CountRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceCount;
internal override HttpMethod HttpMethod => HttpMethod.POST;
protected override HttpMethod StaticHttpMethod => HttpMethod.POST;
internal override bool SupportsBody => true;
public CountRequestDescriptor AllowNoIndices(bool? allowNoIndices = true) => Qs("allow_no_indices", allowNoIndices);
public CountRequestDescriptor AnalyzeWildcard(bool? analyzeWildcard = true) => Qs("analyze_wildcard", analyzeWildcard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CreateRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clie
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceCreate;
internal override HttpMethod HttpMethod => HttpMethod.PUT;
protected override HttpMethod StaticHttpMethod => HttpMethod.PUT;
internal override bool SupportsBody => true;
[JsonIgnore]
public TDocument Document { get; set; }
Expand Down Expand Up @@ -105,7 +105,7 @@ internal CreateRequestDescriptor()
}

internal override ApiUrls ApiUrls => ApiUrlsLookups.NoNamespaceCreate;
internal override HttpMethod HttpMethod => HttpMethod.PUT;
protected override HttpMethod StaticHttpMethod => HttpMethod.PUT;
internal override bool SupportsBody => true;
public CreateRequestDescriptor<TDocument> Pipeline(string? pipeline) => Qs("pipeline", pipeline);
public CreateRequestDescriptor<TDocument> Refresh(Elastic.Clients.Elasticsearch.Refresh? refresh) => Qs("refresh", refresh);
Expand Down
Loading