Skip to content

Commit d96c0c7

Browse files
authored
Merge pull request #82 from Research-Institute/fix/#81
Use TypeHelper in IQueryableExtensions.Filter
2 parents 3ca6593 + 1513211 commit d96c0c7

File tree

7 files changed

+205
-8
lines changed

7 files changed

+205
-8
lines changed

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
7878
{
7979
// convert the incoming value to the target value type
8080
// "1" -> 1
81-
var convertedValue = Convert.ChangeType(filterQuery.PropertyValue, property.PropertyType);
81+
var convertedValue = TypeHelper.ConvertType(filterQuery.PropertyValue, property.PropertyType);
8282
// {model}
8383
var parameter = Expression.Parameter(concreteType, "model");
8484
// {model.Id}

src/JsonApiDotNetCoreExample/Migrations/20170330234539_AddGuidProperty.Designer.cs

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace JsonApiDotNetCoreExample.Migrations
6+
{
7+
public partial class AddGuidProperty : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.AddColumn<Guid>(
12+
name: "GuidProperty",
13+
table: "TodoItems",
14+
nullable: false,
15+
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
16+
}
17+
18+
protected override void Down(MigrationBuilder migrationBuilder)
19+
{
20+
migrationBuilder.DropColumn(
21+
name: "GuidProperty",
22+
table: "TodoItems");
23+
}
24+
}
25+
}

src/JsonApiDotNetCoreExample/Migrations/AppDbContextModelSnapshot.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
4141

4242
b.Property<string>("Description");
4343

44+
b.Property<Guid>("GuidProperty");
45+
4446
b.Property<long>("Ordinal");
4547

4648
b.Property<int?>("OwnerId");

src/JsonApiDotNetCoreExample/Models/TodoItem.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ namespace JsonApiDotNetCoreExample.Models
55
{
66
public class TodoItem : Identifiable
77
{
8+
public TodoItem()
9+
{
10+
GuidProperty = Guid.NewGuid();
11+
}
12+
813
[Attr("description")]
914
public string Description { get; set; }
1015

1116
[Attr("ordinal")]
1217
public long Ordinal { get; set; }
18+
19+
[Attr("guid-property")]
20+
public Guid GuidProperty { get; set; }
1321

1422
public int? OwnerId { get; set; }
1523
public int? AssigneeId { get; set; }
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using DotNetCoreDocs;
5+
using DotNetCoreDocs.Models;
6+
using DotNetCoreDocs.Writers;
7+
using JsonApiDotNetCoreExample;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.TestHost;
10+
using Newtonsoft.Json;
11+
using Xunit;
12+
using JsonApiDotNetCore.Internal;
13+
using JsonApiDotNetCoreExample.Data;
14+
using Bogus;
15+
using JsonApiDotNetCoreExample.Models;
16+
using JsonApiDotNetCore.Serialization;
17+
using System.Linq;
18+
19+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
20+
{
21+
[Collection("WebHostCollection")]
22+
public class AttributeFilterTests
23+
{
24+
private DocsFixture<Startup, JsonDocWriter> _fixture;
25+
private Faker<TodoItem> _todoItemFaker;
26+
27+
public AttributeFilterTests(DocsFixture<Startup, JsonDocWriter> fixture)
28+
{
29+
_fixture = fixture;
30+
_todoItemFaker = new Faker<TodoItem>()
31+
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
32+
.RuleFor(t => t.Ordinal, f => f.Random.Number());
33+
}
34+
35+
[Fact]
36+
public async Task Can_Filter_On_Guid_Properties()
37+
{
38+
// arrange
39+
var context = _fixture.GetService<AppDbContext>();
40+
var todoItem = _todoItemFaker.Generate();
41+
context.TodoItems.Add(todoItem);
42+
await context.SaveChangesAsync();
43+
44+
var builder = new WebHostBuilder()
45+
.UseStartup<Startup>();
46+
var httpMethod = new HttpMethod("GET");
47+
var route = $"/api/v1/todo-items?filter[guid-property]={todoItem.GuidProperty}";
48+
var server = new TestServer(builder);
49+
var client = server.CreateClient();
50+
var request = new HttpRequestMessage(httpMethod, route);
51+
52+
// act
53+
var response = await client.SendAsync(request);
54+
var body = await response.Content.ReadAsStringAsync();
55+
var deserializedBody = _fixture
56+
.GetService<IJsonApiDeSerializer>()
57+
.DeserializeList<TodoItem>(body);
58+
59+
var todoItemResponse = deserializedBody.Single();
60+
61+
// assert
62+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
63+
Assert.Equal(todoItem.Id, todoItemResponse.Id);
64+
Assert.Equal(todoItem.GuidProperty, todoItemResponse.GuidProperty);
65+
}
66+
}
67+
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/FetchingRelationshipsTests.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Linq;
42
using System.Net;
53
using System.Net.Http;
64
using System.Threading.Tasks;
75
using Bogus;
86
using DotNetCoreDocs;
97
using DotNetCoreDocs.Writers;
10-
using JsonApiDotNetCore.Models;
11-
using JsonApiDotNetCore.Serialization;
128
using JsonApiDotNetCore.Services;
139
using JsonApiDotNetCoreExample;
1410
using JsonApiDotNetCoreExample.Data;
1511
using JsonApiDotNetCoreExample.Models;
1612
using Microsoft.AspNetCore.Hosting;
1713
using Microsoft.AspNetCore.TestHost;
18-
using Newtonsoft.Json;
1914
using Xunit;
20-
using Person = JsonApiDotNetCoreExample.Models.Person;
21-
2215
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
2316
{
2417
[Collection("WebHostCollection")]

0 commit comments

Comments
 (0)