1

I have the following class and enum:

public class Email   
{
    string Address;
    EEmailType Type;
}

public enum EEmailType 
{
    Primary,
    Alternative
}

The problem I'm seeing is that I have a handler where I return a collection of these objects as a json response.

The response is coming fine and includes the email address for all the emails I can fetch, but the email type is not returned.

When I get the response I instead get :

[
  {
    address : "Joe@gmail.com"
  }
]

So the type of email is nowhere to be seen. Also, in the mapper where I'm building this collection I call a function that converts a string we receive from the backend to an enum:

public EEmailType convertToEnum(string input) 
{
  switch(input) 
  {
      case "Primary" :
           return EEmailType.Primary;
     ......... *and so on*
  }
}

But if instead of using my function I directly hardcode a value then it does show up in the json string which is returned:

emails.Add(
new Email {
    Address : response.address,
    Type : convertToEnum(response.type) <----- THIS DOES NOT WORK
});

emails.Add(
new Email {
    Address : response.address,
    Type : EEmailType.Primary <----- THIS WORKS FINE
});

What am I doing wrong here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
  • 1
    Hmm interesting, could it be that the `convertToEnum` path is returning null for those properties in those cases? – IronMan Sep 17 '19 at 17:59
  • 1
    FYI you can use `[EnumMember(Value = "Primary")]` attributes on the enum, along with a `StringEnumConverter` for automatic de/serialization using enums when using newtonsoft – Kritner Sep 17 '19 at 18:05
  • Decorat your email type property with ""JsonConverter(typeof(StringEnumConverter))" attribute in Email class. – Nauty Sep 17 '19 at 18:07
  • @Nauty when I add that the application ceashes with an invalid cast exception – Matias Barrios Sep 17 '19 at 18:20
  • 2
    [It seems to work fine when I try it](https://dotnetfiddle.net/0z4Jtq). Can you put together a [mcve] which demonstrates the problem? – Brian Rogers Sep 17 '19 at 20:45
  • In your real code, did you remember to declare the member `EEmailType Type` as public? If not you will need to mark it with `[JsonProperty]`, see [Json.net serialize specific private field](https://stackoverflow.com/q/32008869) or [JSON Serializer object with internal properties](https://stackoverflow.com/q/26873755). – dbc Sep 17 '19 at 21:59
  • @dbc yes. Everything's is public. Also when I hardcode the value then it shows up normally. Only when I call my function then is missing. I debug the app and the object im deserializing contains all the right properties just a line before actually getting transformed into the response – Matias Barrios Sep 18 '19 at 11:47
  • @MatiasBarrios - then you might want to [edit] your question to include a [mcve]. At the moment your code does not compile, because e.g. all the members shown in `Email` are private: https://dotnetfiddle.net/oHEb9Q – dbc Sep 18 '19 at 15:56

1 Answers1

1

Well, I fixed this issue. Believe or not I started an issue in Carter framework repo for something which I thought might be unrelated but as I implemented it it also corrected the issues I was seeing with vanishing fields.

This is the issue in question : https://github.com/CarterCommunity/Carter/issues/204

I created that class with that exact name in the root of my project and Carter automagically picked it up.

Now all problems are gone.

Thanks everyone for your answers!

Matias Barrios
  • 4,674
  • 3
  • 22
  • 49