Skip to content

Fix/#93 #97

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 4 commits into from
Apr 27, 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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ JsonApiDotnetCore provides a framework for building [json:api](http://jsonapi.or
- [Non-Integer Type Keys](#non-integer-type-keys)
- [Routing](#routing)
- [Namespacing and Versioning URLs](#namespacing-and-versioning-urls)
- [Disable Convention](#disable-convention)
- [Defining Custom Data Access Methods](#defining-custom-data-access-methods)
- [Pagination](#pagination)
- [Filtering](#filtering)
Expand Down Expand Up @@ -244,6 +245,24 @@ services.AddJsonApi<AppDbContext>(
opt => opt.Namespace = "api/v1");
```

#### Disable Convention

You can disable the dasherized convention and specify your own template
by using the `DisableRoutingConvention` Attribute:

```csharp
[DisableRoutingConvention]
public class CamelCasedModelsController : JsonApiController<CamelCasedModel>
{
public CamelCasedModelsController(
IJsonApiContext jsonApiContext,
IResourceService<CamelCasedModel> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiContext, resourceService, loggerFactory)
{ }
}
```

### Defining Custom Data Access Methods

By default, data retrieval is distributed across 3 layers:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace JsonApiDotNetCore.Controllers
{
public class DisableRoutingConventionAttribute : Attribute
{ }
}
37 changes: 27 additions & 10 deletions src/JsonApiDotNetCore/Internal/DasherizedRoutingConvention.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// REF: https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.CustomRoutingConvention/NameSpaceRoutingConvention.cs
// REF: https://github.com/aspnet/Mvc/issues/5691
using System.Reflection;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;

namespace JsonApiDotNetCore.Internal
Expand All @@ -17,21 +19,36 @@ public DasherizedRoutingConvention(string nspace)
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
if (IsJsonApiController(controller))
{
var template = string.Empty;

if (IsDasherizedJsonApiController(controller))
template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
else
template = GetTemplate(controller);

controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
{
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
{
Template = template
};
}
Template = template
};
}
}

private bool IsJsonApiController(ControllerModel controller)
private bool IsDasherizedJsonApiController(ControllerModel controller)
{
return controller.ControllerType.IsSubclassOf(typeof(JsonApiControllerMixin));
var type = controller.ControllerType;
var notDisabled = type.GetCustomAttribute<DisableRoutingConventionAttribute>() == null;
return notDisabled && type.IsSubclassOf(typeof(JsonApiControllerMixin));
}

private string GetTemplate(ControllerModel controller)
{
var type = controller.ControllerType;
var routeAttr = type.GetCustomAttribute<RouteAttribute>();
if(routeAttr != null)
return ((RouteAttribute)routeAttr).Template;

return controller.ControllerName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExample.Controllers
{
[DisableRoutingConvention]
public class CamelCasedModelsController : JsonApiController<CamelCasedModel>
{
public CamelCasedModelsController(
IJsonApiContext jsonApiContext,
IResourceService<CamelCasedModel> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiContext, resourceService, loggerFactory)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Models;
Expand All @@ -8,6 +9,7 @@

namespace JsonApiDotNetCoreExample.Controllers
{
[DisableRoutingConvention]
[Route("custom/route/todo-items")]
public class TodoItemsCustomController : CustomJsonApiController<TodoItem>
{
Expand Down
5 changes: 3 additions & 2 deletions src/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using JsonApiDotNetCoreExample.Models;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCoreExample.Models;
using Microsoft.EntityFrameworkCore;
using System;

namespace JsonApiDotNetCoreExample.Data
{
Expand Down Expand Up @@ -33,5 +31,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

[Resource("todo-collections")]
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }

[Resource("camelCasedModels")]
public DbSet<CamelCasedModel> CamelCasedModels { get; set; }
}
}

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,32 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Metadata;

namespace JsonApiDotNetCoreExample.Migrations
{
public partial class AddCamelCasedModel : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "CamelCasedModels",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
CompoundAttr = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_CamelCasedModels", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CamelCasedModels");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "1.1.1");

modelBuilder.Entity("JsonApiDotNetCoreExample.Models.CamelCasedModel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

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

b.HasKey("Id");

b.ToTable("CamelCasedModels");
});

modelBuilder.Entity("JsonApiDotNetCoreExample.Models.Person", b =>
{
b.Property<int>("Id")
Expand Down
10 changes: 10 additions & 0 deletions src/JsonApiDotNetCoreExample/Models/CamelCasedModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using JsonApiDotNetCore.Models;

namespace JsonApiDotNetCoreExample.Models
{
public class CamelCasedModel : Identifiable
{
[Attr("compoundAttr")]
public string CompoundAttr { get; set; }
}
}
Loading