Skip to content

fix: #528 #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2019
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
4 changes: 3 additions & 1 deletion src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ private void SetHasOneForeignKeyValue(object entity, HasOneAttribute hasOneAttr,

// e.g. PATCH /articles
// {... { "relationships":{ "Owner": { "data": null } } } }
if (rio == null && Nullable.GetUnderlyingType(foreignKeyProperty.PropertyType) == null)
bool foreignKeyPropertyIsNullableType = Nullable.GetUnderlyingType(foreignKeyProperty.PropertyType) != null
|| foreignKeyProperty.PropertyType == typeof(string);
if (rio == null && !foreignKeyPropertyIsNullableType)
throw new JsonApiException(400, $"Cannot set required relationship identifier '{hasOneAttr.IdentifiablePropertyName}' to null because it is a non-nullable type.");

var convertedValue = TypeHelper.ConvertType(foreignKeyPropertyValue, foreignKeyProperty.PropertyType);
Expand Down
60 changes: 59 additions & 1 deletion test/UnitTests/Serialization/JsonApiDeSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,51 @@ public void Can_Deserialize_Independent_Side_Of_One_To_One_Relationship()
Assert.Equal(property, result.Property);
}

[Fact]
public void Can_Deserialize_Independent_Side_Of_One_To_One_Relationship_With_String_Keys()
{
// arrange
var resourceGraphBuilder = new ResourceGraphBuilder();
resourceGraphBuilder.AddResource<IndependentWithStringKey, string>("independents");
resourceGraphBuilder.AddResource<DependentWithStringKey, string>("dependents");
var resourceGraph = resourceGraphBuilder.Build();

var jsonApiContextMock = new Mock<IJsonApiContext>();
jsonApiContextMock.SetupAllProperties();
jsonApiContextMock.Setup(m => m.ResourceGraph).Returns(resourceGraph);
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
jsonApiContextMock.Setup(m => m.HasOneRelationshipPointers).Returns(new HasOneRelationshipPointers());

var jsonApiOptions = new JsonApiOptions();
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);

var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object);

var property = Guid.NewGuid().ToString();
var content = new Document
{
Data = new ResourceObject
{
Type = "independents",
Id = "natural-key",
Attributes = new Dictionary<string, object> { { "property", property } },
Relationships = new Dictionary<string, RelationshipData>
{
{ "dependent" , new RelationshipData { } }
}
}
};

var contentString = JsonConvert.SerializeObject(content);

// act
var result = deserializer.Deserialize<IndependentWithStringKey>(contentString);

// assert
Assert.NotNull(result);
Assert.Equal(property, result.Property);
}

[Fact]
public void Can_Deserialize_Independent_Side_Of_One_To_One_Relationship_With_Relationship_Body()
{
Expand Down Expand Up @@ -348,6 +393,19 @@ private class Dependent : Identifiable
public int IndependentId { get; set; }
}

private class IndependentWithStringKey : Identifiable<string>
{
[Attr("property")] public string Property { get; set; }
[HasOne("dependent")] public Dependent Dependent { get; set; }
public string DependentId { get; set; }
}

private class DependentWithStringKey : Identifiable<string>
{
[HasOne("independent")] public Independent Independent { get; set; }
public string IndependentId { get; set; }
}

[Fact]
public void Can_Deserialize_Object_With_HasManyRelationship()
{
Expand Down Expand Up @@ -538,7 +596,7 @@ public void Can_Deserialize_Nested_Included_HasMany_Relationships()
resourceGraphBuilder.AddResource<ManyToManyNested>("many-to-manys");

var deserializer = GetDeserializer(resourceGraphBuilder);

var contentString =
@"{
""data"": {
Expand Down