Skip to content

fix(AttrAttribute): unexpected scenarios cause breaking change #328

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 2 commits into from
Jul 5, 2018
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
6 changes: 3 additions & 3 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ If($env:APPVEYOR_REPO_TAG -eq $true) {

IF ([string]::IsNullOrWhitespace($revision)){
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --include-symbols
CheckLastExitCode
}
Else {
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$revision --include-symbols
CheckLastExitCode
}
}
Else {
Write-Output "VERSION-SUFFIX: alpha1-$revision"
Write-Output "RUNNING dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=alpha1-$revision"
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=alpha1-$revision
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=alpha1-$revision --include-symbols
CheckLastExitCode
}
45 changes: 42 additions & 3 deletions src/JsonApiDotNetCore/Models/AttrAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,54 @@ public AttrAttribute(string publicName, string internalName, bool isImmutable =
/// Returns null if the attribute does not belong to the
/// provided object.
/// </summary>
public object GetValue(object entity) => PropertyInfo.GetValue(entity);
public object GetValue(object entity)
{
if (entity == null)
throw new InvalidOperationException("Cannot GetValue from null object.");

var prop = GetResourceProperty(entity);
return prop?.GetValue(entity);
}

/// <summary>
/// Sets the value of the attribute on the given object.
/// </summary>
public void SetValue(object entity, object newValue)
{
var convertedValue = TypeHelper.ConvertType(newValue, PropertyInfo.PropertyType);
PropertyInfo.SetValue(entity, convertedValue);
if (entity == null)
throw new InvalidOperationException("Cannot SetValue on null object.");

var prop = GetResourceProperty(entity);
if(prop != null)
{
var convertedValue = TypeHelper.ConvertType(newValue, prop.PropertyType);
prop.SetValue(entity, convertedValue);
}
}

private PropertyInfo GetResourceProperty(object resource)
{
// There are some scenarios, especially ones where users are using a different
// data model than view model, where they may use a repository implmentation
// that does not match the deserialized type. For now, we will continue to support
// this use case.
var targetType = resource.GetType();
if (targetType != PropertyInfo.DeclaringType)
{
var propertyInfo = resource
.GetType()
.GetProperty(InternalAttributeName);

return propertyInfo;

// TODO: this should throw but will be a breaking change in some cases
//if (propertyInfo == null)
// throw new InvalidOperationException(
// $"'{targetType}' does not contain a member named '{InternalAttributeName}'." +
// $"There is also a mismatch in target types. Expected '{PropertyInfo.DeclaringType}' but instead received '{targetType}'.");
}

return PropertyInfo;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Models/ResourceDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ protected List<AttrAttribute> Remove(Expression<Func<T, dynamic>> filter, List<A
// model => model.Attribute
if (filter.Body is MemberExpression memberExpression)
return _contextEntity.Attributes
.Where(a => a.PropertyInfo.Name != memberExpression.Member.Name)
.Where(a => a.InternalAttributeName != memberExpression.Member.Name)
.ToList();

// model => new { model.Attribute1, model.Attribute2 }
if (filter.Body is NewExpression newExpression)
{
var attributes = new List<AttrAttribute>();
foreach (var attr in _contextEntity.Attributes)
if (newExpression.Members.Any(m => m.Name == attr.PropertyInfo.Name) == false)
if (newExpression.Members.Any(m => m.Name == attr.InternalAttributeName) == false)
attributes.Add(attr);

return attributes;
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private object SetEntityAttributes(
if (attributeValues.TryGetValue(attr.PublicAttributeName, out object newValue))
{
var convertedValue = ConvertAttrValue(newValue, attr.PropertyInfo.PropertyType);
attr.PropertyInfo.SetValue(entity, convertedValue);
attr.SetValue(entity, convertedValue);

if (attr.IsImmutable == false)
_jsonApiContext.AttributesToUpdate[attr] = convertedValue;
Expand Down