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
75 changes: 75 additions & 0 deletions JSONAPI.Tests/Data/AttributeSerializationTest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"samples": [
{
"id": "1",
"booleanField": false,
"nullableBooleanField": false,
"sByteField": 0,
"nullableSByteField": null,
"byteField": 0,
"nullableByteField": null,
"int16Field": 0,
"nullableInt16Field": null,
"uInt16Field": 0,
"nullableUInt16Field": null,
"int32Field": 0,
"nullableInt32Field": null,
"uInt32Field": 0,
"nullableUInt32Field": null,
"int64Field": 0,
"nullableInt64Field": null,
"uInt64Field": 0,
"nullableUInt64Field": null,
"doubleField": 0.0,
"nullableDoubleField": null,
"singleField": 0.0,
"nullableSingleField": null,
"decimalField": "0",
"nullableDecimalField": null,
"dateTimeField": "0001-01-01T00:00:00",
"nullableDateTimeField": null,
"dateTimeOffsetField": "0001-01-01T00:00:00+00:00",
"nullableDateTimeOffsetField": null,
"guidField": "00000000-0000-0000-0000-000000000000",
"nullableGuidField": null,
"stringField": null,
"enumField": 0,
"nullableEnumField": null
}, {
"id": "2",
"booleanField": true,
"nullableBooleanField": true,
"sByteField": 123,
"nullableSByteField": 123,
"byteField": 253,
"nullableByteField": 253,
"int16Field": 32000,
"nullableInt16Field": 32000,
"uInt16Field": 64000,
"nullableUInt16Field": 64000,
"int32Field": 2000000000,
"nullableInt32Field": 2000000000,
"uInt32Field": 3000000000,
"nullableUInt32Field": 3000000000,
"int64Field": 9223372036854775807,
"nullableInt64Field": 9223372036854775807,
"uInt64Field": 9223372036854775808,
"nullableUInt64Field": 9223372036854775808,
"doubleField": 1056789.123,
"nullableDoubleField": 1056789.123,
"singleField": 1056789.13,
"nullableSingleField": 1056789.13,
"decimalField": "1056789.123",
"nullableDecimalField": "1056789.123",
"dateTimeField": "1776-07-04T00:00:00",
"nullableDateTimeField": "1776-07-04T00:00:00",
"dateTimeOffsetField": "1776-07-04T00:00:00-05:00",
"nullableDateTimeOffsetField": "1776-07-04T00:00:00-05:00",
"guidField": "6566f9b4-5245-40de-890d-98b40a4ad656",
"nullableGuidField": "3d1fb81e-43ee-4d04-af91-c8a326341293",
"stringField": "Some string 156",
"enumField": 1,
"nullableEnumField": 2
}
]
}
53 changes: 53 additions & 0 deletions JSONAPI.Tests/Extensions/TypeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using JSONAPI.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSONAPI.Tests.Extensions
{
[TestClass]
public class TypeExtensionsTests
{
private enum TestEnum
{

}

[TestMethod]
public void CanWriteAsJsonApiAttributeTest()
{
Assert.IsTrue(typeof(Byte).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Byte!");
Assert.IsTrue(typeof(Byte?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Byte!");
Assert.IsTrue(typeof(SByte).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for SByte!");
Assert.IsTrue(typeof(SByte?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable SByte!");
Assert.IsTrue(typeof(UInt16).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for UInt16!");
Assert.IsTrue(typeof(UInt16?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable UInt16!");
Assert.IsTrue(typeof(Int16).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Int16!");
Assert.IsTrue(typeof(Int16?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Int16!");
Assert.IsTrue(typeof(UInt32).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for UInt32!");
Assert.IsTrue(typeof(UInt32?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable UInt32!");
Assert.IsTrue(typeof(Int32).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Int32!");
Assert.IsTrue(typeof(Int32?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Int32!");
Assert.IsTrue(typeof(UInt64).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for UInt64!");
Assert.IsTrue(typeof(UInt64?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable UInt64!");
Assert.IsTrue(typeof(Int64).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Int64!");
Assert.IsTrue(typeof(Int64?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Int64!");
Assert.IsTrue(typeof(Double).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Double!");
Assert.IsTrue(typeof(Double?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Double!");
Assert.IsTrue(typeof(Single).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Single!");
Assert.IsTrue(typeof(Single?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Single!");
Assert.IsTrue(typeof(Decimal).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Decimal!");
Assert.IsTrue(typeof(Decimal?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Decimal!");
Assert.IsTrue(typeof(DateTime).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for DateTime!");
Assert.IsTrue(typeof(DateTime?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable DateTime!");
Assert.IsTrue(typeof(DateTimeOffset).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for DateTimeOffset!");
Assert.IsTrue(typeof(DateTimeOffset?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable DateTimeOffset!");
Assert.IsTrue(typeof(Guid).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Guid!");
Assert.IsTrue(typeof(Guid?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable Guid!");
Assert.IsTrue(typeof(String).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for String!");
Assert.IsTrue(typeof(TestEnum).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for enum!");
Assert.IsTrue(typeof(TestEnum?).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for nullable enum!");
Assert.IsFalse(typeof(Object).CanWriteAsJsonApiAttribute(), "CanWriteTypeAsAttribute returned wrong answer for Object!");
}

}
}
7 changes: 6 additions & 1 deletion JSONAPI.Tests/JSONAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@
<Compile Include="ActionFilters\EnableFilteringAttributeTests.cs" />
<Compile Include="Core\MetadataManagerTests.cs" />
<Compile Include="Core\ModelManagerTests.cs" />
<Compile Include="Extensions\TypeExtensionsTests.cs" />
<Compile Include="Json\ErrorSerializerTests.cs" />
<Compile Include="Json\JsonApiMediaFormaterTests.cs" />
<Compile Include="Json\LinkTemplateTests.cs" />
<Compile Include="Models\Author.cs" />
<Compile Include="Models\Comment.cs" />
<Compile Include="Models\Sample.cs" />
<Compile Include="Models\Post.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -109,14 +111,17 @@
<None Include="Data\NonStandardIdTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\AttributeSerializationTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Data\SerializerIntegrationTest.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down
137 changes: 111 additions & 26 deletions JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class JsonApiMediaFormaterTests
{
Author a;
Post p, p2, p3, p4;
Sample s1, s2;

private class MockErrorSerializer : IErrorSerializer
{
Expand Down Expand Up @@ -105,32 +106,80 @@ public void SetupModels()
}
};

}

private enum TestEnum
{

}

[TestMethod]
public void CanWritePrimitiveTest()
{
// Arrange
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
// Act
// Assert
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Int32)), "CanWriteTypeAsPrimitive returned wrong answer for Integer!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Double)), "CanWriteTypeAsPrimitive returned wrong answer for Double!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTime)), "CanWriteTypeAsPrimitive returned wrong answer for DateTime!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTimeOffset)), "CanWriteTypeAsPrimitive returned wrong answer for DateTimeOffset!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Guid)), "CanWriteTypeAsPrimitive returned wrong answer for Guid!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(String)), "CanWriteTypeAsPrimitive returned wrong answer for String!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTime?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable DateTime!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(DateTimeOffset?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable DateTimeOffset!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Guid?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable Guid!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(TestEnum)), "CanWriteTypeAsPrimitive returned wrong answer for enum!");
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(TestEnum?)), "CanWriteTypeAsPrimitive returned wrong answer for nullable enum!");
Assert.IsFalse(formatter.CanWriteTypeAsPrimitive(typeof(Object)), "CanWriteTypeAsPrimitive returned wrong answer for Object!");
s1 = new Sample
{
Id = "1",
BooleanField = false,
NullableBooleanField = false,
SByteField = default(SByte),
NullableSByteField = null,
ByteField = default(Byte),
NullableByteField = null,
Int16Field = default(Int16),
NullableInt16Field = null,
UInt16Field = default(UInt16),
NullableUInt16Field = null,
Int32Field = default(Int32),
NullableInt32Field = null,
UInt32Field = default(Int32),
NullableUInt32Field = null,
Int64Field = default(Int64),
NullableInt64Field = null,
UInt64Field = default(UInt64),
NullableUInt64Field = null,
DoubleField = default(Double),
NullableDoubleField = null,
SingleField = default(Single),
NullableSingleField = null,
DecimalField = default(Decimal),
NullableDecimalField = null,
DateTimeField = default(DateTime),
NullableDateTimeField = null,
DateTimeOffsetField = default(DateTimeOffset),
NullableDateTimeOffsetField = null,
GuidField = default(Guid),
NullableGuidField = null,
StringField = default(String),
EnumField = default(SampleEnum),
NullableEnumField = null,
};
s2 = new Sample
{
Id = "2",
BooleanField = true,
NullableBooleanField = true,
SByteField = 123,
NullableSByteField = 123,
ByteField = 253,
NullableByteField = 253,
Int16Field = 32000,
NullableInt16Field = 32000,
UInt16Field = 64000,
NullableUInt16Field = 64000,
Int32Field = 2000000000,
NullableInt32Field = 2000000000,
UInt32Field = 3000000000,
NullableUInt32Field = 3000000000,
Int64Field = 9223372036854775807,
NullableInt64Field = 9223372036854775807,
UInt64Field = 9223372036854775808,
NullableUInt64Field = 9223372036854775808,
DoubleField = 1056789.123,
NullableDoubleField = 1056789.123,
SingleField = 1056789.123f,
NullableSingleField = 1056789.123f,
DecimalField = 1056789.123m,
NullableDecimalField = 1056789.123m,
DateTimeField = new DateTime(1776, 07, 04),
NullableDateTimeField = new DateTime(1776, 07, 04),
DateTimeOffsetField = new DateTimeOffset(new DateTime(1776, 07, 04), new TimeSpan(-5, 0, 0)),
NullableDateTimeOffsetField = new DateTimeOffset(new DateTime(1776, 07, 04), new TimeSpan(-5, 0, 0)),
GuidField = new Guid("6566F9B4-5245-40DE-890D-98B40A4AD656"),
NullableGuidField = new Guid("3D1FB81E-43EE-4D04-AF91-C8A326341293"),
StringField = "Some string 156",
EnumField = SampleEnum.Value1,
NullableEnumField = SampleEnum.Value2,
};
}

[TestMethod]
Expand Down Expand Up @@ -183,6 +232,24 @@ public void SerializeArrayIntegrationTest()
//Assert.AreEqual("[2,3,4]", sw.ToString());
}

[TestMethod]
[DeploymentItem(@"Data\AttributeSerializationTest.json")]
public void Serializes_attributes_properly()
{
// Arrang
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
MemoryStream stream = new MemoryStream();

// Act
formatter.WriteToStreamAsync(typeof(Sample), new[] { s1, s2 }, stream, null, null);

// Assert
string output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
Trace.WriteLine(output);
var expected = JsonHelpers.MinifyJson(File.ReadAllText("AttributeSerializationTest.json"));
Assert.AreEqual(expected, output.Trim());
}

[TestMethod]
[DeploymentItem(@"Data\FormatterErrorSerializationTest.json")]
public void Should_serialize_error()
Expand Down Expand Up @@ -246,6 +313,24 @@ public void DeserializeCollectionIntegrationTest()
Assert.AreEqual(p.Id, posts[0].Id); // Order matters, right?
}

[TestMethod]
public async Task Deserializes_attributes_properly()
{
// Arrange
JsonApiFormatter formatter = new JsonApiFormatter(new PluralizationService());
MemoryStream stream = new MemoryStream();

await formatter.WriteToStreamAsync(typeof(Sample), new List<Sample> { s1, s2 }, stream, null, null);
stream.Seek(0, SeekOrigin.Begin);

var deserialized = (IList<Sample>)await formatter.ReadFromStreamAsync(typeof(Sample), stream, null, null);

// Assert
deserialized.Count.Should().Be(2);
deserialized[0].ShouldBeEquivalentTo(s1);
deserialized[1].ShouldBeEquivalentTo(s2);
}

[TestMethod]
[DeploymentItem(@"Data\DeserializeRawJsonTest.json")]
public async Task DeserializeRawJsonTest()
Expand Down
48 changes: 48 additions & 0 deletions JSONAPI.Tests/Models/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;

namespace JSONAPI.Tests.Models
{
enum SampleEnum
{
Value1 = 1,
Value2 = 2
}

class Sample
{
public string Id { get; set; }
public Boolean BooleanField { get; set; }
public Boolean? NullableBooleanField { get; set; }
public SByte SByteField { get; set; }
public SByte? NullableSByteField { get; set; }
public Byte ByteField { get; set; }
public Byte? NullableByteField { get; set; }
public Int16 Int16Field { get; set; }
public Int16? NullableInt16Field { get; set; }
public UInt16 UInt16Field { get; set; }
public UInt16? NullableUInt16Field { get; set; }
public Int32 Int32Field { get; set; }
public Int32? NullableInt32Field { get; set; }
public UInt32 UInt32Field { get; set; }
public UInt32? NullableUInt32Field { get; set; }
public Int64 Int64Field { get; set; }
public Int64? NullableInt64Field { get; set; }
public UInt64 UInt64Field { get; set; }
public UInt64? NullableUInt64Field { get; set; }
public Double DoubleField { get; set; }
public Double? NullableDoubleField { get; set; }
public Single SingleField { get; set; }
public Single? NullableSingleField { get; set; }
public Decimal DecimalField { get; set; }
public Decimal? NullableDecimalField { get; set; }
public DateTime DateTimeField { get; set; }
public DateTime? NullableDateTimeField { get; set; }
public DateTimeOffset DateTimeOffsetField { get; set; }
public DateTimeOffset? NullableDateTimeOffsetField { get; set; }
public Guid GuidField { get; set; }
public Guid? NullableGuidField { get; set; }
public string StringField { get; set; }
public SampleEnum EnumField { get; set; }
public SampleEnum? NullableEnumField { get; set; }
}
}
22 changes: 22 additions & 0 deletions JSONAPI/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace JSONAPI.Extensions
{
internal static class TypeExtensions
{
internal static bool CanWriteAsJsonApiAttribute(this Type objectType)
{
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof (Nullable<>))
objectType = objectType.GetGenericArguments()[0];

return objectType.IsPrimitive
|| typeof (Decimal).IsAssignableFrom(objectType)
|| typeof (Guid).IsAssignableFrom(objectType)
|| typeof (DateTime).IsAssignableFrom(objectType)
|| typeof (DateTimeOffset).IsAssignableFrom(objectType)
|| typeof (String).IsAssignableFrom(objectType)
|| objectType.IsEnum;
}

}
}
Loading