-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Deserializer should handle nullable StringEnum<T> #1760
Changes from 7 commits
2af9044
5fa9975
ae3fcb2
6ba82d4
2bfb345
8382cec
0b6e19d
fb156d3
42d40ea
1f86849
959a34c
60089fe
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 |
---|---|---|
|
@@ -29,7 +29,9 @@ public StringEnum(TEnum parsedValue) | |
|
||
public StringEnum(string stringValue) | ||
{ | ||
_stringValue = stringValue ?? string.Empty; | ||
Ensure.ArgumentNotNull(stringValue, nameof(stringValue)); | ||
|
||
_stringValue = stringValue; | ||
_parsedValue = null; | ||
} | ||
|
||
|
@@ -66,7 +68,7 @@ public bool TryParse(out TEnum value) | |
return true; | ||
} | ||
|
||
if (string.IsNullOrEmpty(StringValue)) | ||
if (StringValue == 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. How can 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. It seems to still be 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 added a test to repro this here: https://github.com/octokit/octokit.net/pull/1760/files#diff-728143ef4b5a14ceed6d74d06f9c9df7R62 this is certainly doing my head in! 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. Couldn't this potentially lead to incorrect behavior? If you have an uninitialized property, it means that the payload is missing the property, which means it's optional, but Octokit has modeled it as required (by it not being nullable). By just silently falling back to the default value, you could potentially give the user the incorrect default (who decides what the "default value" is?) 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. right... I was losing sight of the fact we only use this But from a technical point of view - the same would be true of an I guess the question is, should an incorrectly not nullable 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.
Yes, but that doesn't make it any less wrong 😉 There's a reason you should always declare all value-type properties as nullable when binding to external input. It's the only way to check whether that input actually existed, or if it's just the default (uninitialized) value.
I vote "no", simply because it's a nice way to "flush out" mistakes where we've assumed something was required, but was actually optional. 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. Let's say you have a model: public class UpdateAccount
{
public Guid Id { get; set; }
public decimal Amount { get; set; }
} And someone posts { "id": "3d216536-0869-4c9d-b2b7-2a735498c634" } You'd end up with Instead, you'd want to declare it nullable and be able to check |
||
{ | ||
value = default(TEnum); | ||
return false; | ||
|
@@ -144,6 +146,11 @@ public override string ToString() | |
|
||
private TEnum ParseValue() | ||
{ | ||
if (_stringValue == 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. You've already verified in the ctor that |
||
{ | ||
return default(TEnum); | ||
} | ||
|
||
TEnum value; | ||
if (TryParse(out value)) | ||
{ | ||
|
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.
should we enforce no empty strings here too?
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.
Yeah, I guess it wouldn't hurt.