-
-
Notifications
You must be signed in to change notification settings - Fork 158
Fix: crash when deserializing post with relationship to abstract base class #828
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,6 +137,7 @@ public override void SetValue(object resource, object newValue, IResourceFactory | |
List<object> throughResources = new List<object>(); | ||
foreach (IIdentifiable identifiable in (IEnumerable)newValue) | ||
{ | ||
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this one |
||
object throughResource = resourceFactory.CreateInstance(ThroughType); | ||
LeftProperty.SetValue(throughResource, resource); | ||
RightProperty.SetValue(throughResource, identifiable); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,15 +168,19 @@ private void SetHasOneRelationship(IIdentifiable resource, | |
var rio = (ResourceIdentifierObject)relationshipData.Data; | ||
var relatedId = rio?.Id; | ||
|
||
var resourceContext = relationshipData.SingleData == null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be simplified? var relatedResourceType = relationshipData.SingleData == null
? attr.RightType
: ResourceContextProvider.GetResourceContext(relationshipData.SingleData.Type).ResourceType;
// this does not make sense in the following case: if we're setting the dependent of a one-to-one relationship, IdentifiablePropertyName should be null.
var foreignKeyProperty = resourceProperties.FirstOrDefault(p => p.Name == attr.IdentifiablePropertyName);
if (foreignKeyProperty != null)
// there is a FK from the current resource pointing to the related object,
// i.e. we're populating the relationship from the dependent side.
SetForeignKey(resource, foreignKeyProperty, attr, relatedId, relatedResourceType);
SetNavigation(resource, attr, relatedId, relatedResourceType); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also: isn't |
||
? ResourceContextProvider.GetResourceContext(attr.RightType) | ||
: ResourceContextProvider.GetResourceContext(relationshipData.SingleData.Type); | ||
|
||
// this does not make sense in the following case: if we're setting the dependent of a one-to-one relationship, IdentifiablePropertyName should be null. | ||
var foreignKeyProperty = resourceProperties.FirstOrDefault(p => p.Name == attr.IdentifiablePropertyName); | ||
|
||
if (foreignKeyProperty != null) | ||
// there is a FK from the current resource pointing to the related object, | ||
// i.e. we're populating the relationship from the dependent side. | ||
SetForeignKey(resource, foreignKeyProperty, attr, relatedId); | ||
SetForeignKey(resource, foreignKeyProperty, attr, relatedId, resourceContext.ResourceType); | ||
|
||
SetNavigation(resource, attr, relatedId); | ||
SetNavigation(resource, attr, relatedId, resourceContext.ResourceType); | ||
|
||
// depending on if this base parser is used client-side or server-side, | ||
// different additional processing per field needs to be executed. | ||
|
@@ -187,7 +191,7 @@ private void SetHasOneRelationship(IIdentifiable resource, | |
/// Sets the dependent side of a HasOne relationship, which means that a | ||
/// foreign key also will to be populated. | ||
/// </summary> | ||
private void SetForeignKey(IIdentifiable resource, PropertyInfo foreignKey, HasOneAttribute attr, string id) | ||
private void SetForeignKey(IIdentifiable resource, PropertyInfo foreignKey, HasOneAttribute attr, string id, Type relationshipType) | ||
{ | ||
bool foreignKeyPropertyIsNullableType = Nullable.GetUnderlyingType(foreignKey.PropertyType) != null | ||
|| foreignKey.PropertyType == typeof(string); | ||
|
@@ -198,23 +202,24 @@ private void SetForeignKey(IIdentifiable resource, PropertyInfo foreignKey, HasO | |
throw new FormatException($"Cannot set required relationship identifier '{attr.IdentifiablePropertyName}' to null because it is a non-nullable type."); | ||
} | ||
|
||
var typedId = TypeHelper.ConvertStringIdToTypedId(attr.Property.PropertyType, id, ResourceFactory); | ||
var typedId = TypeHelper.ConvertStringIdToTypedId(relationshipType /* was: attr.Property.PropertyType */, id, ResourceFactory); | ||
foreignKey.SetValue(resource, typedId); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the principal side of a HasOne relationship, which means no | ||
/// foreign key is involved. | ||
/// </summary> | ||
private void SetNavigation(IIdentifiable resource, HasOneAttribute attr, string relatedId) | ||
private void SetNavigation(IIdentifiable resource, HasOneAttribute attr, string relatedId, | ||
Type relationshipType) | ||
{ | ||
if (relatedId == null) | ||
{ | ||
attr.SetValue(resource, null, ResourceFactory); | ||
} | ||
else | ||
{ | ||
var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(attr.RightType); | ||
var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(relationshipType /* was: attr.RightType */); | ||
relatedInstance.StringId = relatedId; | ||
attr.SetValue(resource, relatedInstance, ResourceFactory); | ||
} | ||
|
@@ -232,6 +237,7 @@ private void SetHasManyRelationship( | |
{ // if the relationship is set to null, no need to set the navigation property to null: this is the default value. | ||
var relatedResources = relationshipData.ManyData.Select(rio => | ||
{ | ||
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is valid. I think we'll have to introduce the equivalent approach as for SetHasOneRelationship |
||
var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(attr.RightType); | ||
relatedInstance.StringId = rio.Id; | ||
return relatedInstance; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ protected override void AfterProcessField(IIdentifiable resource, ResourceFieldA | |
/// </summary> | ||
private IIdentifiable ParseIncludedRelationship(RelationshipAttribute relationshipAttr, ResourceIdentifierObject relatedResourceIdentifier) | ||
{ | ||
// TODO: [#696] Potential location where we crash if the relationship targets an abstract base class. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True |
||
var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(relationshipAttr.RightType); | ||
relatedInstance.StringId = relatedResourceIdentifier.Id; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. This one seems a bit more difficult to resolve