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

EnumMemberAttribute does not work. (expectedly) #332

Open
aberkromb opened this issue Oct 16, 2019 · 0 comments
Open

EnumMemberAttribute does not work. (expectedly) #332

aberkromb opened this issue Oct 16, 2019 · 0 comments

Comments

@aberkromb
Copy link

Hi, we want to use MessagePack in our production and ran into a problem.
Current stable version does not work with EnumMemberAttribute.
It's important for us.
I've wrote some tests for example

public class Tests
    {
        [Fact]
        public void Failed()
        {
            // arrange

            // act
            var firstDto = new DtoWithFirstEnum {Enum = FirstEnum.NotOk};

            var serializerForFirst = MessagePackSerializer.Get<DtoWithFirstEnum>();
            var bytes = serializerForFirst.PackSingleObject(firstDto);

            var serializerForSecond = MessagePackSerializer.Get<DtoWithSecondEnum>();
            var secondDto = serializerForSecond.UnpackSingleObject(bytes);

            // assert 
            Assert.Equal(SecondEnum.SecondValue, secondDto.Enum);
        }

        [Fact]
        public void Pass()
        {
            // arrange

            // act
            var firstDto = new DtoWithFirstEnum {Enum = FirstEnum.NotOk};

            var json = JsonConvert.SerializeObject(firstDto);

            var secondDto = JsonConvert.DeserializeObject<DtoWithSecondEnum>(json);

            // assert 
            Assert.Equal(SecondEnum.SecondValue, secondDto.Enum);
        }
    }

    [DataContract]
    public class DtoWithFirstEnum
    {
        [DataMember(Name = "enum")] public FirstEnum Enum { get; set; }
    }

    [DataContract]
    public class DtoWithSecondEnum
    {
        [DataMember(Name = "enum")] public SecondEnum Enum { get; set; }
    }

    public enum FirstEnum
    {
        [EnumMember(Value = "ok")]
        OK,

        [EnumMember(Value = "not_ok")]
        NotOk
    }

    public enum SecondEnum
    {
        [EnumMember(Value = "ok")]
        FirstValue,

        [EnumMember(Value = "not_ok")]
        SecondValue
    }

Looks like we can refine the EnumMessagePackSerializer class in ctor
and add new value ByEnumMember in EnumSerializationMethod

 protected EnumMessagePackSerializer( SerializationContext ownerContext, EnumSerializationMethod serializationMethod )
			: base( ownerContext, SerializerCapabilities.PackTo | SerializerCapabilities.UnpackFrom )
		{
			if ( !typeof( TEnum ).GetIsEnum() )
			{
				throw new InvalidOperationException(
					String.Format( CultureInfo.CurrentCulture, "Type '{0}' is not enum.", typeof( TEnum ) )
				);
			}

			this._serializationMethod = serializationMethod;
			this._underlyingType = Enum.GetUnderlyingType( typeof( TEnum ) );
			var members = Enum.GetValues( typeof( TEnum ) ) as TEnum[];
			this._serializationMapping = new Dictionary<TEnum, string>( members.Length );
			this._deserializationMapping = new Dictionary<string, TEnum>( members.Length );
			foreach ( var member in members )
			{
				var enumName = serializationMethod == EnumSerializationMethod.ByEnumMember
					? GetNameFromEnumMemberAttr( member )
					: member.ToString();
				
				var asString = ownerContext.EnumSerializationOptions.SafeNameTransformer( enumName);
				this._serializationMapping[ member ] = asString;
				this._deserializationMapping[ asString ] = member;
			}
		}

Can you suggest a quick way to solve the problem?
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant