Skip to content
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
11 changes: 11 additions & 0 deletions JSONAPI.Tests/Data/LinkTemplateTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"posts": {
"id": "2",
"title": "How to fry an egg",
"links": {
"author": {
"href": "/users/5"
}
}
}
}
4 changes: 4 additions & 0 deletions JSONAPI.Tests/JSONAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Json\ErrorSerializerTests.cs" />
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
<Compile Include="Json\JsonHelpers.cs" />
<Compile Include="Json\LinkTemplateTests.cs" />
<Compile Include="Models\Author.cs" />
<Compile Include="Models\Comment.cs" />
<Compile Include="Models\Post.cs" />
Expand All @@ -98,6 +99,9 @@
<None Include="Data\ErrorSerializerTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\LinkTemplateTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\SerializerIntegrationTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
67 changes: 67 additions & 0 deletions JSONAPI.Tests/Json/LinkTemplateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using JSONAPI.Attributes;
using JSONAPI.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace JSONAPI.Tests.Json
{
[TestClass]
public class LinkTemplateTests
{
private class Post
{
public string Id { get; set; }

public string Title { get; set; }

[SerializeAs(SerializeAsOptions.Link)]
[LinkTemplate("/users/{0}")]
public virtual User Author { get; set; }
}

private class User
{
public string Id { get; set; }

public string Name { get; set; }
}

private Post ThePost { get; set; }

[TestInitialize]
public void SetupModels()
{
ThePost = new Post
{
Id = "2",
Title = "How to fry an egg",
Author = new User
{
Id = "5",
Name = "Bob"
}
};
}

[TestMethod]
[DeploymentItem(@"Data\LinkTemplateTest.json")]
public void GetResourceWithLinkTemplateRelationship()
{
var formatter = new JsonApiFormatter
{
PluralizationService = new JSONAPI.Core.PluralizationService()
};
var stream = new MemoryStream();

formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);

// Assert
var expected = JsonHelpers.MinifyJson(File.ReadAllText("LinkTemplateTest.json"));
var output = Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
Assert.AreEqual(output.Trim(), expected);
}
}
}
3 changes: 3 additions & 0 deletions JSONAPI/Json/JsonApiFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
string link = String.Format(lt, objId,
value.GetType().GetProperty("Id").GetValue(value, null));
//writer.WritePropertyName(ContractResolver.FormatPropertyName(prop.Name));
writer.WriteStartObject();
writer.WritePropertyName("href");
writer.WriteValue(link);
writer.WriteEndObject();
break;
case SerializeAsOptions.Embedded:
// Not really supported by Ember Data yet, incidentally...but easy to implement here.
Expand Down