Skip to content

Commit cac3019

Browse files
author
Chris Santero
committed
use href key for link templates
1 parent 1fc6b4c commit cac3019

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"posts": {
3+
"id": "2",
4+
"title": "How to fry an egg",
5+
"links": {
6+
"author": {
7+
"href": "/users/5"
8+
}
9+
}
10+
}
11+
}

JSONAPI.Tests/JSONAPI.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
<ItemGroup>
7070
<Compile Include="Core\MetadataManagerTests.cs" />
7171
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
72+
<Compile Include="Json\JsonHelpers.cs" />
73+
<Compile Include="Json\LinkTemplateTests.cs" />
7274
<Compile Include="Models\Author.cs" />
7375
<Compile Include="Models\Comment.cs" />
7476
<Compile Include="Models\Post.cs" />
@@ -82,6 +84,9 @@
8284
</ItemGroup>
8385
<ItemGroup>
8486
<None Include="app.config" />
87+
<None Include="Data\LinkTemplateTest.json">
88+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
89+
</None>
8590
<None Include="Data\SerializerIntegrationTest.json">
8691
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
8792
</None>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using JSONAPI.Attributes;
2+
using JSONAPI.Json;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Text;
7+
8+
namespace JSONAPI.Tests.Json
9+
{
10+
[TestClass]
11+
public class LinkTemplateTests
12+
{
13+
private class Post
14+
{
15+
public string Id { get; set; }
16+
17+
public string Title { get; set; }
18+
19+
[SerializeAs(SerializeAsOptions.Link)]
20+
[LinkTemplate("/users/{0}")]
21+
public virtual User Author { get; set; }
22+
}
23+
24+
private class User
25+
{
26+
public string Id { get; set; }
27+
28+
public string Name { get; set; }
29+
}
30+
31+
private Post ThePost { get; set; }
32+
33+
[TestInitialize]
34+
public void SetupModels()
35+
{
36+
ThePost = new Post
37+
{
38+
Id = "2",
39+
Title = "How to fry an egg",
40+
Author = new User
41+
{
42+
Id = "5",
43+
Name = "Bob"
44+
}
45+
};
46+
}
47+
48+
[TestMethod]
49+
[DeploymentItem(@"Data\LinkTemplateTest.json")]
50+
public void GetResourceWithLinkTemplateRelationship()
51+
{
52+
var formatter = new JsonApiFormatter
53+
{
54+
PluralizationService = new JSONAPI.Core.PluralizationService()
55+
};
56+
var stream = new MemoryStream();
57+
58+
formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);
59+
60+
// Assert
61+
var expected = JsonHelpers.MinifyJson(File.ReadAllText("LinkTemplateTest.json"));
62+
var output = Encoding.ASCII.GetString(stream.ToArray());
63+
Trace.WriteLine(output);
64+
Assert.AreEqual(output.Trim(), expected);
65+
}
66+
}
67+
}

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
281281
string link = String.Format(lt, objId,
282282
value.GetType().GetProperty("Id").GetValue(value, null));
283283
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
284+
writer.WriteStartObject();
285+
writer.WritePropertyName("href");
284286
writer.WriteValue(link);
287+
writer.WriteEndObject();
285288
break;
286289
case SerializeAsOptions.Embedded:
287290
// Not really supported by Ember Data yet, incidentally...but easy to implement here.

0 commit comments

Comments
 (0)