Skip to content

Commit

Permalink
Fix value body request serialization (requests)
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd committed May 28, 2024
1 parent b9c12fb commit 96264f5
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public sealed partial class CreateRequestParameters : RequestParameters
/// <summary>
/// <para>Adds a JSON document to the specified data stream or index and makes it searchable.<br/>If the target is an index and the document already exists, the request updates the document and increments its version.</para>
/// </summary>
public sealed partial class CreateRequest<TDocument> : PlainRequest<CreateRequestParameters>
public sealed partial class CreateRequest<TDocument> : PlainRequest<CreateRequestParameters>, ISelfSerializable
{
public CreateRequest(Elastic.Clients.Elasticsearch.Serverless.IndexName index, Elastic.Clients.Elasticsearch.Serverless.Id id) : base(r => r.Required("index", index).Required("id", id))
{
Expand Down Expand Up @@ -127,6 +127,11 @@ public CreateRequest(Elastic.Clients.Elasticsearch.Serverless.IndexName index, E
public Elastic.Clients.Elasticsearch.Serverless.WaitForActiveShards? WaitForActiveShards { get => Q<Elastic.Clients.Elasticsearch.Serverless.WaitForActiveShards?>("wait_for_active_shards"); set => Q("wait_for_active_shards", value); }
[JsonIgnore]
public TDocument Document { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
SourceSerialization.Serialize(Document, writer, settings.SourceSerializer);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public sealed partial class PutIndicesSettingsRequestParameters : RequestParamet
/// <summary>
/// <para>Changes a dynamic index setting in real time. For data streams, index setting<br/>changes are applied to all backing indices by default.</para>
/// </summary>
public sealed partial class PutIndicesSettingsRequest : PlainRequest<PutIndicesSettingsRequestParameters>
public sealed partial class PutIndicesSettingsRequest : PlainRequest<PutIndicesSettingsRequestParameters>, ISelfSerializable
{
public PutIndicesSettingsRequest()
{
Expand Down Expand Up @@ -131,6 +131,11 @@ public PutIndicesSettingsRequest(Elastic.Clients.Elasticsearch.Serverless.Indice
public Elastic.Clients.Elasticsearch.Serverless.Duration? Timeout { get => Q<Elastic.Clients.Elasticsearch.Serverless.Duration?>("timeout"); set => Q("timeout", value); }
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Serverless.IndexManagement.IndexSettings Settings { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Settings, options);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public sealed partial class IndexRequestParameters : RequestParameters
/// <summary>
/// <para>Adds a JSON document to the specified data stream or index and makes it searchable.<br/>If the target is an index and the document already exists, the request updates the document and increments its version.</para>
/// </summary>
public sealed partial class IndexRequest<TDocument> : PlainRequest<IndexRequestParameters>
public sealed partial class IndexRequest<TDocument> : PlainRequest<IndexRequestParameters>, ISelfSerializable
{
public IndexRequest(Elastic.Clients.Elasticsearch.Serverless.IndexName index, Elastic.Clients.Elasticsearch.Serverless.Id? id) : base(r => r.Required("index", index).Optional("id", id))
{
Expand Down Expand Up @@ -175,6 +175,11 @@ public IndexRequest(Elastic.Clients.Elasticsearch.Serverless.IndexName index) :
public Elastic.Clients.Elasticsearch.Serverless.WaitForActiveShards? WaitForActiveShards { get => Q<Elastic.Clients.Elasticsearch.Serverless.WaitForActiveShards?>("wait_for_active_shards"); set => Q("wait_for_active_shards", value); }
[JsonIgnore]
public TDocument Document { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
SourceSerialization.Serialize(Document, writer, settings.SourceSerializer);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed partial class ValidateDetectorRequestParameters : RequestParameter
/// <summary>
/// <para>Validates an anomaly detection detector.</para>
/// </summary>
public sealed partial class ValidateDetectorRequest : PlainRequest<ValidateDetectorRequestParameters>
public sealed partial class ValidateDetectorRequest : PlainRequest<ValidateDetectorRequestParameters>, ISelfSerializable
{
internal override ApiUrls ApiUrls => ApiUrlLookup.MachineLearningValidateDetector;

Expand All @@ -48,6 +48,11 @@ public sealed partial class ValidateDetectorRequest : PlainRequest<ValidateDetec

[JsonIgnore]
public Elastic.Clients.Elasticsearch.Serverless.MachineLearning.Detector Detector { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Detector, options);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public sealed partial class CreateRepositoryRequestParameters : RequestParameter
/// <summary>
/// <para>Creates a repository.</para>
/// </summary>
public sealed partial class CreateRepositoryRequest : PlainRequest<CreateRepositoryRequestParameters>
public sealed partial class CreateRepositoryRequest : PlainRequest<CreateRepositoryRequestParameters>, ISelfSerializable
{
public CreateRepositoryRequest(Elastic.Clients.Elasticsearch.Serverless.Name name) : base(r => r.Required("repository", name))
{
Expand Down Expand Up @@ -83,6 +83,11 @@ public CreateRepositoryRequest(Elastic.Clients.Elasticsearch.Serverless.Name nam
public bool? Verify { get => Q<bool?>("verify"); set => Q("verify", value); }
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Serverless.Snapshot.IRepository Repository { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Repository, options);
}
}

/// <summary>
Expand Down
12 changes: 1 addition & 11 deletions src/Elastic.Clients.Elasticsearch.Shared/Api/CreateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
// 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.

#if ELASTICSEARCH_SERVERLESS
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
#else
using Elastic.Clients.Elasticsearch.Serialization;
#endif
using Elastic.Transport;
using System.Text.Json;

#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless;
#else
namespace Elastic.Clients.Elasticsearch;
#endif

public sealed partial class CreateRequest<TDocument> : ICustomJsonWriter
public sealed partial class CreateRequest<TDocument>
{

public CreateRequest(Id id) : this(typeof(TDocument), id)
Expand All @@ -26,6 +18,4 @@ public CreateRequest(Id id) : this(typeof(TDocument), id)
public CreateRequest(TDocument documentWithId, IndexName index = null, Id id = null)
: this(index ?? typeof(TDocument), id ?? Id.From(documentWithId)) =>
Document = documentWithId;

public void WriteJson(Utf8JsonWriter writer, Serializer sourceSerializer) => SourceSerialization.Serialize(Document, writer, sourceSerializer);
}
14 changes: 2 additions & 12 deletions src/Elastic.Clients.Elasticsearch.Shared/Api/IndexRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
// 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.

using System.Text.Json;
using System.Text.Json.Serialization;
#if ELASTICSEARCH_SERVERLESS
using Elastic.Clients.Elasticsearch.Serverless.Requests;
#else
using Elastic.Clients.Elasticsearch.Requests;
#endif
#if ELASTICSEARCH_SERVERLESS
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
#else
using Elastic.Clients.Elasticsearch.Serialization;
#endif
using Elastic.Transport;

#if ELASTICSEARCH_SERVERLESS
Expand All @@ -22,7 +16,7 @@ namespace Elastic.Clients.Elasticsearch.Serverless;
namespace Elastic.Clients.Elasticsearch;
#endif

public partial class IndexRequest<TDocument> : ICustomJsonWriter
public partial class IndexRequest<TDocument>
{
public IndexRequest() : this(typeof(TDocument)) { }

Expand All @@ -36,17 +30,13 @@ public partial class IndexRequest<TDocument> : ICustomJsonWriter

[JsonIgnore] private Id? Id => RouteValues.Get<Id>("id");

void ICustomJsonWriter.WriteJson(Utf8JsonWriter writer, Serializer sourceSerializer) => SourceSerialization.Serialize(Document, writer, sourceSerializer);

internal static HttpMethod GetHttpMethod(IndexRequest<TDocument> request) =>
request.Id?.StringOrLongValue != null || request.RouteValues.ContainsId ? HttpMethod.PUT : HttpMethod.POST;
}

public sealed partial class IndexRequestDescriptor<TDocument> : ICustomJsonWriter
public sealed partial class IndexRequestDescriptor<TDocument>
{
internal Id _id;

public void WriteJson(Utf8JsonWriter writer, Serializer sourceSerializer) => SourceSerialization.Serialize(DocumentValue, writer, sourceSerializer);

protected override HttpMethod? DynamicHttpMethod => _id is not null || RouteValues.ContainsId ? HttpMethod.PUT : HttpMethod.POST;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings) :
new KeyValuePairConverterFactory(settings),
new ObjectToInferredTypesConverter(),
new SourceConverterFactory(settings),
new CustomJsonWriterConverterFactory(settings),
new SelfSerializableConverterFactory(settings),
new SelfDeserializableConverterFactory(settings),
new SelfTwoWaySerializableConverterFactory(settings),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public sealed partial class CreateRequestParameters : RequestParameters
/// <summary>
/// <para>Adds a JSON document to the specified data stream or index and makes it searchable.<br/>If the target is an index and the document already exists, the request updates the document and increments its version.</para>
/// </summary>
public sealed partial class CreateRequest<TDocument> : PlainRequest<CreateRequestParameters>
public sealed partial class CreateRequest<TDocument> : PlainRequest<CreateRequestParameters>, ISelfSerializable
{
public CreateRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clients.Elasticsearch.Id id) : base(r => r.Required("index", index).Required("id", id))
{
Expand Down Expand Up @@ -127,6 +127,11 @@ public CreateRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clie
public Elastic.Clients.Elasticsearch.WaitForActiveShards? WaitForActiveShards { get => Q<Elastic.Clients.Elasticsearch.WaitForActiveShards?>("wait_for_active_shards"); set => Q("wait_for_active_shards", value); }
[JsonIgnore]
public TDocument Document { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
SourceSerialization.Serialize(Document, writer, settings.SourceSerializer);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed partial class DownsampleRequestParameters : RequestParameters
/// <summary>
/// <para>Aggregates a time series (TSDS) index and stores pre-computed statistical summaries (`min`, `max`, `sum`, `value_count` and `avg`) for each metric field grouped by a configured time interval.</para>
/// </summary>
public sealed partial class DownsampleRequest : PlainRequest<DownsampleRequestParameters>
public sealed partial class DownsampleRequest : PlainRequest<DownsampleRequestParameters>, ISelfSerializable
{
public DownsampleRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clients.Elasticsearch.IndexName targetIndex) : base(r => r.Required("index", index).Required("target_index", targetIndex))
{
Expand All @@ -52,6 +52,11 @@ public DownsampleRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.

[JsonIgnore]
public Elastic.Clients.Elasticsearch.IndexManagement.DownsampleConfig Config { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Config, options);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public sealed partial class PutIndicesSettingsRequestParameters : RequestParamet
/// <summary>
/// <para>Changes a dynamic index setting in real time. For data streams, index setting<br/>changes are applied to all backing indices by default.</para>
/// </summary>
public sealed partial class PutIndicesSettingsRequest : PlainRequest<PutIndicesSettingsRequestParameters>
public sealed partial class PutIndicesSettingsRequest : PlainRequest<PutIndicesSettingsRequestParameters>, ISelfSerializable
{
public PutIndicesSettingsRequest()
{
Expand Down Expand Up @@ -131,6 +131,11 @@ public PutIndicesSettingsRequest(Elastic.Clients.Elasticsearch.Indices? indices)
public Elastic.Clients.Elasticsearch.Duration? Timeout { get => Q<Elastic.Clients.Elasticsearch.Duration?>("timeout"); set => Q("timeout", value); }
[JsonIgnore]
public Elastic.Clients.Elasticsearch.IndexManagement.IndexSettings Settings { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Settings, options);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public sealed partial class IndexRequestParameters : RequestParameters
/// <summary>
/// <para>Adds a JSON document to the specified data stream or index and makes it searchable.<br/>If the target is an index and the document already exists, the request updates the document and increments its version.</para>
/// </summary>
public sealed partial class IndexRequest<TDocument> : PlainRequest<IndexRequestParameters>
public sealed partial class IndexRequest<TDocument> : PlainRequest<IndexRequestParameters>, ISelfSerializable
{
public IndexRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clients.Elasticsearch.Id? id) : base(r => r.Required("index", index).Optional("id", id))
{
Expand Down Expand Up @@ -175,6 +175,11 @@ public IndexRequest(Elastic.Clients.Elasticsearch.IndexName index) : base(r => r
public Elastic.Clients.Elasticsearch.WaitForActiveShards? WaitForActiveShards { get => Q<Elastic.Clients.Elasticsearch.WaitForActiveShards?>("wait_for_active_shards"); set => Q("wait_for_active_shards", value); }
[JsonIgnore]
public TDocument Document { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
SourceSerialization.Serialize(Document, writer, settings.SourceSerializer);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed partial class PutModelRequestParameters : RequestParameters
/// <summary>
/// <para>Create an inference service model</para>
/// </summary>
public sealed partial class PutModelRequest : PlainRequest<PutModelRequestParameters>
public sealed partial class PutModelRequest : PlainRequest<PutModelRequestParameters>, ISelfSerializable
{
public PutModelRequest(Elastic.Clients.Elasticsearch.Id inferenceId) : base(r => r.Required("inference_id", inferenceId))
{
Expand All @@ -56,6 +56,11 @@ public PutModelRequest(Elastic.Clients.Elasticsearch.Inference.TaskType? taskTyp

[JsonIgnore]
public Elastic.Clients.Elasticsearch.Inference.ModelConfig ModelConfig { get; set; }

void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, ModelConfig, options);
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 96264f5

Please sign in to comment.