Skip to content

Commit 490aad8

Browse files
author
Chris Santero
committed
rewrite JSONAPI.NET to be compatible with JSON API 1.0
1 parent dc5cc3c commit 490aad8

274 files changed

Lines changed: 11436 additions & 4700 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{AF7861F3-550B-4F70-A33E-1E5F48D39333}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>JSONAPI.Autofac</RootNamespace>
11+
<AssemblyName>JSONAPI.Autofac</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
15+
<RestorePackages>true</RestorePackages>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="Autofac">
36+
<HintPath>..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
37+
</Reference>
38+
<Reference Include="Autofac.Integration.WebApi">
39+
<HintPath>..\packages\Autofac.WebApi2.3.4.0\lib\net45\Autofac.Integration.WebApi.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Newtonsoft.Json">
42+
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
43+
</Reference>
44+
<Reference Include="System" />
45+
<Reference Include="System.Core" />
46+
<Reference Include="System.Net.Http" />
47+
<Reference Include="System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
48+
<SpecificVersion>False</SpecificVersion>
49+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll</HintPath>
50+
</Reference>
51+
<Reference Include="System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
52+
<SpecificVersion>False</SpecificVersion>
53+
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\System.Web.Http.dll</HintPath>
54+
</Reference>
55+
<Reference Include="System.Xml.Linq" />
56+
<Reference Include="System.Data.DataSetExtensions" />
57+
<Reference Include="Microsoft.CSharp" />
58+
<Reference Include="System.Data" />
59+
<Reference Include="System.Xml" />
60+
</ItemGroup>
61+
<ItemGroup>
62+
<Compile Include="JsonApiAutofacConfiguration.cs" />
63+
<Compile Include="Properties\AssemblyInfo.cs" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<None Include="app.config" />
67+
<None Include="packages.config" />
68+
</ItemGroup>
69+
<ItemGroup>
70+
<ProjectReference Include="..\JSONAPI\JSONAPI.csproj">
71+
<Project>{52b19fd6-efaa-45b5-9c3e-a652e27608d1}</Project>
72+
<Name>JSONAPI</Name>
73+
</ProjectReference>
74+
</ItemGroup>
75+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
76+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
77+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
78+
<PropertyGroup>
79+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
80+
</PropertyGroup>
81+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
82+
</Target>
83+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
84+
Other similar extension points exist, see Microsoft.Common.targets.
85+
<Target Name="BeforeBuild">
86+
</Target>
87+
<Target Name="AfterBuild">
88+
</Target>
89+
-->
90+
</Project>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Web.Http;
4+
using Autofac;
5+
using JSONAPI.ActionFilters;
6+
using JSONAPI.Core;
7+
using JSONAPI.Json;
8+
using JSONAPI.Payload;
9+
using JSONAPI.Payload.Builders;
10+
11+
namespace JSONAPI.Autofac
12+
{
13+
public class JsonApiAutofacConfiguration
14+
{
15+
private readonly INamingConventions _namingConventions;
16+
private readonly List<Type> _typesToRegister;
17+
private readonly List<Action<ContainerBuilder>> _containerBuildingActions;
18+
private ILinkConventions _linkConventions;
19+
20+
public JsonApiAutofacConfiguration(INamingConventions namingConventions)
21+
{
22+
if (namingConventions == null) throw new ArgumentNullException("namingConventions");
23+
24+
_namingConventions = namingConventions;
25+
_typesToRegister = new List<Type>();
26+
_containerBuildingActions = new List<Action<ContainerBuilder>>();
27+
}
28+
29+
public void RegisterResourceType(Type resourceType)
30+
{
31+
_typesToRegister.Add(resourceType);
32+
}
33+
34+
public void OverrideLinkConventions(ILinkConventions linkConventions)
35+
{
36+
_linkConventions = linkConventions;
37+
}
38+
39+
public void OnContainerBuilding(Action<ContainerBuilder> action)
40+
{
41+
_containerBuildingActions.Add(action);
42+
}
43+
44+
public IContainer Apply(HttpConfiguration httpConfig)
45+
{
46+
var container = GetContainer();
47+
var jsonApiConfiguration = container.Resolve<JsonApiHttpConfiguration>();
48+
jsonApiConfiguration.Apply(httpConfig);
49+
return container;
50+
}
51+
52+
private IContainer GetContainer()
53+
{
54+
var builder = new ContainerBuilder();
55+
56+
// Registry
57+
builder.Register(c => _namingConventions).As<INamingConventions>().SingleInstance();
58+
builder.RegisterType<ResourceTypeRegistry>().AsSelf().SingleInstance();
59+
builder.Register(c =>
60+
{
61+
var registry = c.Resolve<ResourceTypeRegistry>();
62+
foreach (var type in _typesToRegister)
63+
registry.RegisterResourceType(type);
64+
return registry;
65+
}).As<IResourceTypeRegistry>().SingleInstance();
66+
67+
// Serialization
68+
builder.RegisterType<MetadataSerializer>().As<IMetadataSerializer>().SingleInstance();
69+
builder.RegisterType<LinkSerializer>().As<ILinkSerializer>().SingleInstance();
70+
builder.RegisterType<ResourceLinkageSerializer>().As<IResourceLinkageSerializer>().SingleInstance();
71+
builder.RegisterType<RelationshipObjectSerializer>().As<IRelationshipObjectSerializer>().SingleInstance();
72+
builder.RegisterType<ResourceObjectSerializer>().As<IResourceObjectSerializer>().SingleInstance();
73+
builder.RegisterType<SingleResourcePayloadSerializer>().As<ISingleResourcePayloadSerializer>().SingleInstance();
74+
builder.RegisterType<ResourceCollectionPayloadSerializer>().As<IResourceCollectionPayloadSerializer>().SingleInstance();
75+
builder.RegisterType<ErrorSerializer>().As<IErrorSerializer>().SingleInstance();
76+
builder.RegisterType<ErrorPayloadSerializer>().As<IErrorPayloadSerializer>().SingleInstance();
77+
78+
// Queryable transforms
79+
builder.RegisterType<SynchronousEnumerationTransformer>().As<IQueryableEnumerationTransformer>().SingleInstance();
80+
builder.RegisterType<DefaultFilteringTransformer>().As<IQueryableFilteringTransformer>().SingleInstance();
81+
builder.RegisterType<DefaultSortingTransformer>().As<IQueryableSortingTransformer>().SingleInstance();
82+
builder.RegisterType<DefaultPaginationTransformer>().As<IQueryablePaginationTransformer>().SingleInstance();
83+
84+
// Payload building
85+
var linkConventions = _linkConventions ?? new DefaultLinkConventions();
86+
builder.Register(c => linkConventions).As<ILinkConventions>().SingleInstance();
87+
builder.RegisterType<JsonApiFormatter>().SingleInstance();
88+
builder.RegisterType<RegistryDrivenResourceCollectionPayloadBuilder>().As<IResourceCollectionPayloadBuilder>().SingleInstance();
89+
builder.RegisterType<RegistryDrivenSingleResourcePayloadBuilder>().As<ISingleResourcePayloadBuilder>().SingleInstance();
90+
builder.RegisterType<FallbackPayloadBuilder>().As<IFallbackPayloadBuilder>().SingleInstance();
91+
builder.RegisterType<ErrorPayloadBuilder>().As<IErrorPayloadBuilder>().SingleInstance();
92+
builder.RegisterType<FallbackPayloadBuilderAttribute>().SingleInstance();
93+
builder.RegisterType<JsonApiExceptionFilterAttribute>().SingleInstance();
94+
builder.RegisterType<DefaultQueryableResourceCollectionPayloadBuilder>().As<IQueryableResourceCollectionPayloadBuilder>();
95+
96+
builder.RegisterType<JsonApiHttpConfiguration>();
97+
98+
foreach (var containerBuildingAction in _containerBuildingActions)
99+
{
100+
containerBuildingAction(builder);
101+
}
102+
103+
return builder.Build();
104+
}
105+
}
106+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("JSONAPI.Autofac")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("JSONAPI.Autofac")]
13+
[assembly: AssemblyCopyright("Copyright © 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("868ac502-6972-4889-bcbd-9d6160258667")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

JSONAPI.Autofac/app.config

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<runtime>
4+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
5+
<dependentAssembly>
6+
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
7+
<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
8+
</dependentAssembly>
9+
<dependentAssembly>
10+
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
11+
<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
12+
</dependentAssembly>
13+
</assemblyBinding>
14+
</runtime>
15+
</configuration>

JSONAPI.Autofac/packages.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Autofac" version="3.5.2" targetFramework="net45" />
4+
<package id="Autofac.WebApi2" version="3.4.0" targetFramework="net45" />
5+
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
6+
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
7+
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
8+
</packages>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Web.Http;
2+
using JSONAPI.EntityFramework.Tests.TestWebApp.Models;
3+
4+
namespace JSONAPI.EntityFramework.Tests.TestWebApp.Controllers
5+
{
6+
public class CitiesController : ApiController
7+
{
8+
public IHttpActionResult Get(string id)
9+
{
10+
City city;
11+
if (id == "9000")
12+
{
13+
city =
14+
new City
15+
{
16+
Id = "9000",
17+
Name = "Seattle",
18+
State = new State
19+
{
20+
Id = "4000",
21+
Name = "Washington"
22+
}
23+
};
24+
}
25+
else if (id == "9001")
26+
{
27+
city =
28+
new City
29+
{
30+
Id = "9001",
31+
Name = "Tacoma"
32+
};
33+
}
34+
else
35+
{
36+
return NotFound();
37+
}
38+
39+
return Ok(city);
40+
}
41+
}
42+
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
using JSONAPI.Core;
2-
using JSONAPI.EntityFramework.Http;
3-
using JSONAPI.EntityFramework.Tests.TestWebApp.Models;
1+
using JSONAPI.EntityFramework.Tests.TestWebApp.Models;
2+
using JSONAPI.Http;
43

54
namespace JSONAPI.EntityFramework.Tests.TestWebApp.Controllers
65
{
7-
public class CommentsController : ApiController<Comment, TestDbContext>
6+
public class CommentsController : JsonApiController<Comment>
87
{
9-
protected readonly TestDbContext DbContext;
10-
11-
public CommentsController(TestDbContext dbContext)
12-
{
13-
DbContext = dbContext;
14-
}
15-
16-
protected override IMaterializer MaterializerFactory()
8+
public CommentsController(IPayloadMaterializer payloadMaterializer) : base(payloadMaterializer)
179
{
18-
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1910
}
2011
}
2112
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
using JSONAPI.Core;
2-
using JSONAPI.EntityFramework.Http;
3-
using JSONAPI.EntityFramework.Tests.TestWebApp.Models;
1+
using JSONAPI.EntityFramework.Tests.TestWebApp.Models;
2+
using JSONAPI.Http;
43

54
namespace JSONAPI.EntityFramework.Tests.TestWebApp.Controllers
65
{
7-
public class PostsController : ApiController<Post, TestDbContext>
6+
public class PostsController : JsonApiController<Post>
87
{
9-
protected readonly TestDbContext DbContext;
10-
11-
public PostsController(TestDbContext dbContext)
12-
{
13-
DbContext = dbContext;
14-
}
15-
16-
protected override IMaterializer MaterializerFactory()
8+
public PostsController(IPayloadMaterializer payloadMaterializer) : base(payloadMaterializer)
179
{
18-
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1910
}
2011
}
2112
}

0 commit comments

Comments
 (0)