Skip to content

Fix enum parsing #133

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 9 commits into from
Jun 14, 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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
26 changes: 24 additions & 2 deletions docs/Controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,31 @@ public class ThingsController : JsonApiController<Thing, Guid>
}
```

### 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<Thing>
{
public ThingsController(
IJsonApiContext jsonApiContext,
IResourceService<Thing> 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:

Expand Down
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Internal/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="System.ValueTuple" Version="4.3.1" />
</ItemGroup>
</Project>
</Project>
18 changes: 18 additions & 0 deletions test/UnitTests/Internal/TypeHelper_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,23 @@ public void Bad_DateTimeOffset_String_Throws()
// assert
Assert.Throws<FormatException>(() => 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
}
}
}