Skip to content
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

Add Unknown type to TaskInfoType #542

Merged
merged 14 commits into from
Aug 5, 2024
31 changes: 31 additions & 0 deletions src/Meilisearch/Converters/TaskInfoTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Meilisearch.Converters
{
public class TaskInfoTypeConverter : JsonConverter<TaskInfoType>
{
public override TaskInfoType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var enumValue = reader.GetString();
if (Enum.TryParse<TaskInfoType>(enumValue, true, out var taskInfoType))
{
return taskInfoType;
}
}

// If we reach here, it means we encountered an unknown value
return TaskInfoType.Unknown;
}

public override void Write(Utf8JsonWriter writer, TaskInfoType value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}
1 change: 1 addition & 0 deletions src/Meilisearch/MeilisearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,6 @@ private TaskEndpoint TaskEndpoint()

return _taskEndpoint;
}

}
}
7 changes: 5 additions & 2 deletions src/Meilisearch/TaskInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

using Meilisearch.Converters;

namespace Meilisearch
{
/// <summary>
Expand Down Expand Up @@ -102,7 +104,7 @@ public enum TaskInfoStatus
Canceled
}

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(TaskInfoTypeConverter))]
public enum TaskInfoType
{
IndexCreation,
Expand All @@ -115,6 +117,7 @@ public enum TaskInfoType
TaskCancelation,
SnapshotCreation,
TaskDeletion,
IndexSwap
IndexSwap,
Unknown
}
}
31 changes: 31 additions & 0 deletions tests/Meilisearch.Tests/MeilisearchClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using FluentAssertions;

using Meilisearch.Converters;
using Meilisearch.Extensions;
using Meilisearch.QueryParameters;

Expand Down Expand Up @@ -140,6 +141,7 @@ public async Task SwapIndexes()
Assert.Equal(task.Details["swaps"].ToString(), JsonSerializer.Serialize(swaps).ToString());
}


[Fact]
public async Task Health()
{
Expand Down Expand Up @@ -188,5 +190,34 @@ public async Task DeleteIndex()
var finishedTask = await _defaultClient.Index(indexUid).WaitForTaskAsync(task.TaskUid);
Assert.Equal(TaskInfoStatus.Succeeded, finishedTask.Status);
}

[Fact]
public void Deserialize_UnknownTaskType_ReturnsUnknown()
{
var json = "\"NonExistentTaskType\"";
var options = new JsonSerializerOptions
{
Converters = { new TaskInfoTypeConverter() }
};

var result = JsonSerializer.Deserialize<TaskInfoType>(json, options);

Assert.Equal(TaskInfoType.Unknown, result);
}

[Fact]
public void Deserialize_KnownTaskType_ReturnsEnumValue()
{
var json = "\"IndexCreation\"";
var options = new JsonSerializerOptions
{
Converters = { new TaskInfoTypeConverter() }
};

var result = JsonSerializer.Deserialize<TaskInfoType>(json, options);

Assert.Equal(TaskInfoType.IndexCreation, result);
}

}
}
Loading