Skip to content
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

Implement int to enum deserialization. #1590

Open
wants to merge 1 commit into
base: release-7.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,25 @@ private object ReadEnumValue(bool insideJsonObjectValue, IEdmEnumTypeReference e
throw new ODataException(ODataErrorStrings.JsonReaderExtensions_UnexpectedNodeDetectedWithPropertyName(JsonNodeType.PrimitiveValue, JsonNodeType.StartObject, propertyName));
}

string enumStr = this.JsonReader.ReadStringValue();
return new ODataEnumValue(enumStr, expectedValueTypeReference.FullName());
object enumValue = this.JsonReader.ReadPrimitiveValue();
string enumString;
if (enumValue is int enumIntValue)
Copy link
Member

Choose a reason for hiding this comment

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

enumValue is int enumIntValue [](start = 16, length = 29)

This new syntax maybe not pass .NET 4.5 and .NET 3.5 compiler. Please change it to normal.

{
IEdmEnumType edmEnumType = expectedValueTypeReference.EnumDefinition();
IEdmEnumMember selectedMember = edmEnumType.Members.FirstOrDefault(member => member.Value.Value == enumIntValue);
enumString = selectedMember.Name;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't selectedMember be null here because of FirstOrDefault potentially not finding an element?

}
else if (enumValue is string enumStringValue)
{
enumString = enumStringValue;
}
else
{
throw new ODataException(ODataErrorStrings.ReaderValidationUtils_CannotConvertPrimitiveValue(enumValue, typeof(IEdmEnumTypeReference)));
}
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to see Unit test to cover the changes.



return new ODataEnumValue(enumString, expectedValueTypeReference.FullName());
}

/// <summary>
Expand Down