Skip to content

Commit 7714afc

Browse files
Merge #652
652: Support structs as search result r=curquiza a=nikcio # Pull Request ## What does this PR do? This PR removes the class constraint on the `ISearchableJsonConverter` to allow using structs as the search result model (`T`). The class constraint `where T : class` isn't present on anywhere else and therefore only breaks at runtime and as the JSON serializer can handle structs just fine this restriction isn't needed. I've added some basic tests showing that the JSON converter works with structs. ## Current problem To explain the current problem. When using a struct as the response model you'll get the following error when running a search query: ```stacktrace System.ArgumentException : GenericArguments[0], 'Meilisearch.Tests.MovieStruct', on 'Meilisearch.ISearchableJsonConverter`1[T]' violates the constraint of type 'T'. ---- System.TypeLoadException : GenericArguments[0], 'Meilisearch.Tests.MovieStruct', on 'Meilisearch.ISearchableJsonConverter`1[T]' violates the constraint of type parameter 'T'. ``` Just removing the constraint on the converter removes the issue. ## 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! <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added support for searching and handling value types (structs) in search functionality. - **Tests** - Introduced new tests to verify search operations using struct-based movie data, ensuring correct results and pagination with value types. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Nikolaj Brask-Nielsen <[email protected]>
2 parents 93636f5 + 7a4ae2a commit 7714afc

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/Meilisearch/ISearchableJsonConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
3333
/// The json converter for <see cref="ISearchable{T}"/>
3434
/// </summary>
3535
/// <typeparam name="T"></typeparam>
36-
public class ISearchableJsonConverter<T> : JsonConverter<ISearchable<T>> where T : class
36+
public class ISearchableJsonConverter<T> : JsonConverter<ISearchable<T>>
3737
{
3838
/// <inheritdoc/>
3939
public override ISearchable<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

tests/Meilisearch.Tests/Movie.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ public class Movie
1212
public string Genre { get; set; }
1313
}
1414

15+
public struct MovieStruct
16+
{
17+
public string Id { get; set; }
18+
19+
public string Name { get; set; }
20+
21+
public string Genre { get; set; }
22+
}
1523
public class MovieInfo
1624
{
1725
public string Comment { get; set; }

tests/Meilisearch.Tests/SearchTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ public async Task BasicSearch()
4545
movies.Hits.ElementAt(1).Name.Should().NotBeEmpty();
4646
}
4747

48+
[Fact]
49+
public async Task BasicSearchWithStruct()
50+
{
51+
var movies = await _basicIndex.SearchAsync<MovieStruct>("man");
52+
movies.Hits.Should().NotBeEmpty();
53+
movies.Hits.First().Name.Should().NotBeEmpty();
54+
movies.Hits.ElementAt(1).Name.Should().NotBeEmpty();
55+
}
56+
57+
[Fact]
58+
public async Task PaginatedSearchWithStruct()
59+
{
60+
var movies = await _basicIndex.SearchAsync<MovieStruct>("man", new SearchQuery()
61+
{
62+
Page = 1,
63+
HitsPerPage = 2,
64+
});
65+
movies.Hits.Should().NotBeEmpty();
66+
movies.Hits.First().Name.Should().NotBeEmpty();
67+
movies.Hits.ElementAt(1).Name.Should().NotBeEmpty();
68+
}
69+
4870
[Fact]
4971
public async Task BasicSearchWithNoQuery()
5072
{

0 commit comments

Comments
 (0)