Skip to content

Commit ac5c469

Browse files
author
Chris Santero
committed
use href key for link templates
1 parent 97091ed commit ac5c469

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="Json\ErrorSerializerTests.cs" />
8080
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
8181
<Compile Include="Json\JsonHelpers.cs" />
82+
<Compile Include="Json\LinkTemplateTests.cs" />
8283
<Compile Include="Models\Author.cs" />
8384
<Compile Include="Models\Comment.cs" />
8485
<Compile Include="Models\Post.cs" />
@@ -98,6 +99,9 @@
9899
<None Include="Data\ErrorSerializerTest.json">
99100
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
100101
</None>
102+
<None Include="Data\LinkTemplateTest.json">
103+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
104+
</None>
101105
<None Include="Data\SerializerIntegrationTest.json">
102106
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
103107
</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
@@ -296,7 +296,10 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
296296
string link = String.Format(lt, objId,
297297
value.GetType().GetProperty("Id").GetValue(value, null));
298298
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
299+
writer.WriteStartObject();
300+
writer.WritePropertyName("href");
299301
writer.WriteValue(link);
302+
writer.WriteEndObject();
300303
break;
301304
case SerializeAsOptions.Embedded:
302305
// Not really supported by Ember Data yet, incidentally...but easy to implement here.

0 commit comments

Comments
 (0)