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

Automapper not respecting EnumMemberAttribute value when mapping #1741

Closed
TsengSR opened this issue Oct 17, 2016 · 4 comments
Closed

Automapper not respecting EnumMemberAttribute value when mapping #1741

TsengSR opened this issue Oct 17, 2016 · 4 comments

Comments

@TsengSR
Copy link

TsengSR commented Oct 17, 2016

It seems that automapper only looks at the defined enum value but ignores [EnumMember(Value = "..")] attribute.

class Test 
{
    public string Value { get; set; }
}

class TestDto
{
    public TestEnum Value { get; set; }
}

public enum TestEnum 
{
    [EnumMember(Value = "some-value")]
    SomeValue
}

Mapper.CreateMap<Test, TestDto>();

var test = new Test { Value = "some-value };
// fails: ArgumentException: Requested value 'some-value' was not found.
var testDto = Mapper.Map<TestDto>(test);

var test = new Test { Value = "SomeValue };
// works
var testDto = Mapper.Map<TestDto>(test);

When converting the testDto to JSON it all works fine and displays the EnumMember's value.

I'm not huge fan of this, but I'm working on an ASP.NET Core application which uses an legacy database and the values there are stored with dashes, so it'd be nice if AutoMapper respect EnumMember too. Using strings here isn't an option.

@lbargaoanu
Copy link
Member

PR? :)

@Tornhoof
Copy link
Contributor

Hi,
do you still want a PR for that?
I implemented the string->enum side here : https://github.com/Tornhoof/AutoMapper/commit/d3b8daacf3b6d13d6d959bc6aaa8d4b51557cc9e
I used a switch table for those EnumMember values and defaulted to the original impl if there are none

Do you also need the other side, enum->string?
Because as far as I see this is a bit more involved, as currently this way is done via the default StringMapper.cs and I don't think the handling of that Enum specific attribute has a real place in there, so probably a EnumToStringMapper.cs would be necessary.

@Tornhoof
Copy link
Contributor

I added the other side too, was way less involved than previously anticipated.

@lock
Copy link

lock bot commented May 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants