Skip to content

Commit 8e0016c

Browse files
authored
Merge pull request #17 from Research-Institute/v0.1.4-fixes
Dasherize name in RouteBuilder
2 parents 1db131c + 6441a78 commit 8e0016c

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

JsonApiDotNetCore/Configuration/JsonApiConfigurationBuilder.cs

-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
using JsonApiDotNetCore.Routing;
44
using Microsoft.EntityFrameworkCore;
55
using System.Linq;
6-
using System.Linq.Expressions;
76
using AutoMapper;
8-
using JsonApiDotNetCore.Abstractions;
97
using JsonApiDotNetCore.Attributes;
108

119
namespace JsonApiDotNetCore.Configuration

JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public async Task Invoke(HttpContext context)
3636
}
3737
else
3838
{
39+
_logger.LogWarning("Request not handled by JsonApiDotNetCore");
40+
3941
await _next.Invoke(context);
4042

4143
RespondUnsupportedMediaType(context);
@@ -51,14 +53,13 @@ private bool IsJsonApiRequest(HttpContext context)
5153
if(context.Request.ContentType == "application/vnd.api+json") {
5254
return true;
5355
}
54-
_logger.LogInformation("Content-Type invalid for JsonAPI");
56+
_logger.LogWarning("Content-Type invalid for JsonAPI, must be application/vnd.api+json");
5557
return false;
5658
}
5759
return true;
5860
}
5961

60-
_logger.LogInformation("Accept header invalid for JsonAPI");
61-
62+
_logger.LogWarning("Accept header invalid for JsonAPI, must be application/vnd.api+json");
6263
return false;
6364
}
6465

JsonApiDotNetCore/Routing/RouteBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private Type GetTypeOfRelatedResource(string relationshipName)
8383
// TODO: Why is this here?
8484
public static string BuildRoute(string nameSpace, string resourceCollectionName)
8585
{
86-
return $"/{nameSpace}/{resourceCollectionName}";
86+
return $"/{nameSpace}/{resourceCollectionName.Dasherize()}";
8787
}
8888
}
8989
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Xunit;
2+
using JsonApiDotNetCore.Routing;
3+
4+
namespace JsonApiDotNetCoreTests.Routing.UnitTests
5+
{
6+
public class RoutBuilderTests
7+
{
8+
[Theory]
9+
[InlineData("api/v1","People","/api/v1/people")]
10+
[InlineData("api/v1","TodoItems","/api/v1/todo-items")]
11+
[InlineData("api","todoItems","/api/todo-items")]
12+
[InlineData("api","MoreModelsHere","/api/more-models-here")]
13+
public void BuildRoute_Returns_CorrectRoute(string nameSpace, string collectionName, string expectOutput)
14+
{
15+
// arrange
16+
// act
17+
var result = RouteBuilder.BuildRoute(nameSpace, collectionName);
18+
19+
// assert
20+
Assert.Equal(expectOutput, result);
21+
}
22+
}
23+
}

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public class PersonResource : IJsonApiResource
6767
```
6868

6969
We use [AutoMapper](http://automapper.org/) to map from the model class to the resource class.
70-
The below snippet shows how you can specify a custom mapping expression in your `Startup` class that will append `_1` to the resource name.
70+
The below snippet is a trivial example of how you can specify a custom mapping expression in your `Startup` class.
71+
In this example, the first and last name are concatenated and used for the value of the resource's DisplayName property.
7172
Check out [AutoMapper's Wiki](https://github.com/AutoMapper/AutoMapper/wiki) for detailed mapping options.
7273

7374
```
@@ -76,7 +77,7 @@ services.AddJsonApi(config => {
7677
config.AddResourceMapping<Person, PersonResource>(map =>
7778
{
7879
// resource.Name = model.Name + "_1"
79-
map.ForMember("Name", opt => opt.MapFrom(src => $"{((Person)src).Name}_1"));
80+
map.ForMember("DisplayName", opt => opt.MapFrom(src => $"{((Person)src).FirstName} {((Person)src).LastName}"));
8081
});
8182
...
8283
});
@@ -94,9 +95,11 @@ services.AddJsonApi(config => {
9495
});
9596
```
9697

97-
The controller **MUST** implement `IJsonApiController`, and it **MAY** inherit from [JsonApiController](https://github.com/Research-Institute/json-api-dotnet-core/blob/master/JsonApiDotNetCore/Controllers/JsonApiController.cs).
98+
- The controller **MUST** implement `IJsonApiController`
99+
- Controllers **MAY** inherit from [JsonApiController](https://github.com/Research-Institute/json-api-dotnet-core/blob/master/JsonApiDotNetCore/Controllers/JsonApiController.cs).
100+
98101
Constructor dependency injection will work like normal.
99-
Any services added in your `Startup.ConfigureServices()` method will be injected into the constructor parameters.
102+
Any services in your `Startup.ConfigureServices()` method will be injected into the constructor parameters.
100103

101104
```
102105
public class TodoItemsController : JsonApiController, IJsonApiController

0 commit comments

Comments
 (0)