diff --git a/README.md b/README.md index b5f3a9cd55..43b78cc023 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![MyGet CI](https://img.shields.io/myget/research-institute/vpre/JsonApiDotNetCore.svg)](https://www.myget.org/feed/research-institute/package/nuget/JsonApiDotNetCore) [![Join the chat at https://gitter.im/json-api-dotnet-core/Lobby](https://badges.gitter.im/json-api-dotnet-core/Lobby.svg)](https://gitter.im/json-api-dotnet-core/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -A framework for building [json:api](http://jsonapi.org/) compliant web servers. It allows you to eliminate a significant amount of boilerplate while offering out-of-the-box features such as sorting, filtering and pagination. This library provides all the required middleware to build a complete server. All you need to focus on is defining the resources and implementing your custom business logic. This library has been designed around dependency injection making extensibility incredibly easy. +A framework for building [json:api](http://jsonapi.org/) compliant web APIs. The ultimate goal of this library is to eliminate as much boilerplate as possible by offering out-of-the-box features such as sorting, filtering and pagination. You just need to focus on defining the resources and implementing your custom business logic. This library has been designed around dependency injection making extensibility incredibly easy. ## Installation And Usage @@ -18,7 +18,5 @@ See the documentation [here](https://research-institute.github.io/json-api-dotne Branch `feat/core-2` is where I am working on .Net Core 2 compatibility tests and package upgrades. There are several blockers to be aware of: -- Microsoft.AspNetCore.* packages target the runtime (netcoreapp) instead of netstandard. -This will be fixed in future versions. -- EF bug against netcoreapp2.0 runtime ([EntityFramework#8021](https://github.com/aspnet/EntityFramework/issues/8021)) +- Microsoft.AspNetCore.* packages target the runtime (netcoreapp) instead of netstandard. [This will be changed in future versions.](https://blogs.msdn.microsoft.com/webdev/2017/05/10/aspnet-2-preview-1/). - Can't run acceptance testing against postgres on preview runtime [pgsql.EntityFrameworkCore.PostgreSQL#171](https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/171#issuecomment-301287257) diff --git a/docs/Controllers.md b/docs/Controllers.md index 2d365becd3..0ec47b8774 100644 --- a/docs/Controllers.md +++ b/docs/Controllers.md @@ -38,9 +38,31 @@ public class ThingsController : JsonApiController } ``` -### Controller-level customizations +### Limiting Write Access -If you need to customize things at the controller level, you can override the virtual +It is possible to limit write resource access on the controller entirely using the following attributes: + +- `NoHttpPost`: disallow POST requests +- `NoHttpPatch`: disallow PATCH requests +- `NoHttpDelete`: disallow DELETE requests +- `HttpReadOnly`: all of the above + +```csharp +[HttpReadOnly] +public class ThingsController : JsonApiController +{ + public ThingsController( + IJsonApiContext jsonApiContext, + IResourceService resourceService, + ILoggerFactory loggerFactory) + : base(jsonApiContext, resourceService, loggerFactory) + { } +} +``` + +### Additional customizations + +If you need additional customization at the controller level, you can override the virtual methods. Please be aware that this is not the place for advanced business logic which should be performed at the [service](resourceservices.html) or [repository](entityrepositories.html) layers. Here is an example override at the controller layer: diff --git a/src/JsonApiDotNetCore/Internal/TypeHelper.cs b/src/JsonApiDotNetCore/Internal/TypeHelper.cs index 60574ff295..ca382c10d6 100644 --- a/src/JsonApiDotNetCore/Internal/TypeHelper.cs +++ b/src/JsonApiDotNetCore/Internal/TypeHelper.cs @@ -22,6 +22,9 @@ public static object ConvertType(object value, Type type) if (type == typeof(DateTimeOffset)) return DateTimeOffset.Parse(stringValue); + if (type.GetTypeInfo().IsEnum) + return Enum.Parse(type, stringValue); + return Convert.ChangeType(stringValue, type); } catch (Exception e) diff --git a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj index 1920ae0f45..55775d73f5 100755 --- a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj +++ b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj @@ -21,4 +21,4 @@ - \ No newline at end of file + diff --git a/test/UnitTests/Internal/TypeHelper_Tests.cs b/test/UnitTests/Internal/TypeHelper_Tests.cs index 7f05edd892..1e75e705d2 100644 --- a/test/UnitTests/Internal/TypeHelper_Tests.cs +++ b/test/UnitTests/Internal/TypeHelper_Tests.cs @@ -30,5 +30,23 @@ public void Bad_DateTimeOffset_String_Throws() // assert Assert.Throws(() => TypeHelper.ConvertType(formattedString, typeof(DateTimeOffset))); } + + [Fact] + public void Can_Convert_Enums() + { + // arrange + var formattedString = "1"; + + // act + var result = TypeHelper.ConvertType(formattedString, typeof(TestEnum)); + + // assert + Assert.Equal(TestEnum.Test, result); + } + + public enum TestEnum + { + Test = 1 + } } }