Skip to content

Use TypeHelper in IQueryableExtensions.Filter #82

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

Merged
merged 2 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
{
// convert the incoming value to the target value type
// "1" -> 1
var convertedValue = Convert.ChangeType(filterQuery.PropertyValue, property.PropertyType);
var convertedValue = TypeHelper.ConvertType(filterQuery.PropertyValue, property.PropertyType);
// {model}
var parameter = Expression.Parameter(concreteType, "model");
// {model.Id}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;

namespace JsonApiDotNetCoreExample.Migrations
{
public partial class AddGuidProperty : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "GuidProperty",
table: "TodoItems",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "GuidProperty",
table: "TodoItems");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)

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

b.Property<Guid>("GuidProperty");

b.Property<long>("Ordinal");

b.Property<int?>("OwnerId");
Expand Down
8 changes: 8 additions & 0 deletions src/JsonApiDotNetCoreExample/Models/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ namespace JsonApiDotNetCoreExample.Models
{
public class TodoItem : Identifiable
{
public TodoItem()
{
GuidProperty = Guid.NewGuid();
}

[Attr("description")]
public string Description { get; set; }

[Attr("ordinal")]
public long Ordinal { get; set; }

[Attr("guid-property")]
public Guid GuidProperty { get; set; }

public int? OwnerId { get; set; }
public int? AssigneeId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetCoreDocs;
using DotNetCoreDocs.Models;
using DotNetCoreDocs.Writers;
using JsonApiDotNetCoreExample;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Xunit;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCoreExample.Data;
using Bogus;
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCore.Serialization;
using System.Linq;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
[Collection("WebHostCollection")]
public class AttributeFilterTests
{
private DocsFixture<Startup, JsonDocWriter> _fixture;
private Faker<TodoItem> _todoItemFaker;

public AttributeFilterTests(DocsFixture<Startup, JsonDocWriter> fixture)
{
_fixture = fixture;
_todoItemFaker = new Faker<TodoItem>()
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
.RuleFor(t => t.Ordinal, f => f.Random.Number());
}

[Fact]
public async Task Can_Filter_On_Guid_Properties()
{
// arrange
var context = _fixture.GetService<AppDbContext>();
var todoItem = _todoItemFaker.Generate();
context.TodoItems.Add(todoItem);
await context.SaveChangesAsync();

var builder = new WebHostBuilder()
.UseStartup<Startup>();
var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items?filter[guid-property]={todoItem.GuidProperty}";
var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var deserializedBody = _fixture
.GetService<IJsonApiDeSerializer>()
.DeserializeList<TodoItem>(body);

var todoItemResponse = deserializedBody.Single();

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(todoItem.Id, todoItemResponse.Id);
Assert.Equal(todoItem.GuidProperty, todoItemResponse.GuidProperty);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Bogus;
using DotNetCoreDocs;
using DotNetCoreDocs.Writers;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Xunit;
using Person = JsonApiDotNetCoreExample.Models.Person;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
[Collection("WebHostCollection")]
Expand Down