Skip to content

Allow PATCH objects with nullable attribute (e.g. DateTime?) #96

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 7 commits into from Apr 26, 2017
Merged

Allow PATCH objects with nullable attribute (e.g. DateTime?) #96

merged 7 commits into from Apr 26, 2017

Conversation

ghost
Copy link

@ghost ghost commented Apr 25, 2017

Closes #95.

Ready to merge, tests are running.

Problem was located in the AttrAttribute class, while setting the new value on model attributes. It was crashing on nullable types (e.g DateTime?).

Fixed as http://stackoverflow.com/questions/3531318/convert-changetype-fails-on-nullable-types.

Thank you!

@@ -26,10 +26,15 @@ public void SetValue(object entity, object newValue)
var propertyInfo = entity
.GetType()
.GetProperty(InternalAttributeName);

var convertedValue = Convert.ChangeType(newValue, propertyInfo.PropertyType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

We do have a method called TypeHelper.ConvertType which is where type conversions should be processed, but looks like I missed this here. Can you try using that method instead and see if your tests still pass?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jaredcnance, I tested with TypeHelper.ConvertType and all tests are green!

While reading this method, I was wondering if it was not a good idea to replace

if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                type = Nullable.GetUnderlyingType(type);

by

type = Nullable.GetUnderlyingType(type) ?? type;

It changes nothing about the result (I think), but it is more easy to read (maybe subjective), WDYT?

Thanks a lot for review!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks fine to me and I agree the intent is clearer. Thanks again for the PR!

var convertedValue = (newValue == null) ? null : Convert.ChangeType(newValue, t);

propertyInfo.SetValue(entity, convertedValue, null);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some funkiness with your tab spacing throughout...not a big deal just a note

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review! I fixed it 😄

@jaredcnance jaredcnance changed the base branch from master to develop April 25, 2017 22:05
@jaredcnance jaredcnance merged commit 30e1c0d into json-api-dotnet:develop Apr 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant