Skip to content

Commit

Permalink
Merge #542
Browse files Browse the repository at this point in the history
542: Add `Unknown` type to `TaskInfoType` r=curquiza a=postmeback

# Pull Request

## Related issue
Fixes #385 

## What does this PR do?
- Currently, it adds the "Unknown" type to the enum. It requires more tasks to do

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: postmeback <[email protected]>
Co-authored-by: Clémentine <[email protected]>
Co-authored-by: Ahmed Fwela <[email protected]>
Co-authored-by: Arka Poddar <[email protected]>
  • Loading branch information
4 people authored Aug 5, 2024
2 parents 63d5daf + 88019e8 commit 70a7026
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
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);
}

}
}

0 comments on commit 70a7026

Please sign in to comment.