cezarypiatek / MappingGeneratorIssueTracker

This is a issue tracker for MappingGenerator
2 stars 0 forks source link

Set null if source is null #11

Closed GingerNinjaa closed 2 years ago

GingerNinjaa commented 2 years ago

Hi,

Is there a posibility to map null to target if the source is null? I have a case where from a buissnes point of view this S010 class is not mandatory and very often this is a null. Is there a posiblity to forward null value to target?

image

cezarypiatek commented 2 years ago

If you are using annotations for nullable reference types then MappingGenerator should handle them and generate conditional mapping with null-check using ?: operator.

GingerNinjaa commented 2 years ago

Could you please show me how to do it in order to work. This is the source clas. Target clas is the same.

`

       public class UNH

    {

        [EdiValue("9(99)", Path = "*/*/0")]
        [JsonProperty("E0062")]
        [XmlElement(ElementName = "E0062")]
        public string x_0062 { get; set; }

        [JsonProperty("S009")]
        [XmlElement(ElementName = "S009")]
        public S009? S009 { get; set; }

        [EdiValue("9(99)", Path = "*/*/2")]
        [JsonProperty("E0068")]
        [XmlElement(ElementName = "E0068")]
        private string x_0068 { get; set; }

        [JsonProperty("S010")]
        [XmlElement(ElementName = "S010")]
        public S010? S010 { get; set; }

        [JsonProperty("S016")]
        [XmlElement(ElementName = "S016")]
        public S016 S016 { get; set; }

        [JsonProperty("S017")]
        [XmlElement(ElementName = "S017")]
        public S017 S017 { get; set; }

        [JsonProperty("S018")]
        [XmlElement(ElementName = "S018")]
        public S018? S018 { get; set; }

    }

    [EdiElement, EdiPath("*/1")]
    public class S009
    {
        //Mandatory = true,
        [EdiValue("9(99)", Path = "*/*/0")]
        [JsonProperty("E0065")]
        [XmlElement(ElementName = "E0065")]
        public string x_0065 { get; set; }

        [EdiValue("9(99)", Path = "*/*/1")]
        [JsonProperty("E0052")]
        [XmlElement(ElementName = "E0052")]
        public string x_0052 { get; set; }

        [EdiValue("9(99)", Path = "*/*/2")]
        [JsonProperty("E0054")]
        [XmlElement(ElementName = "E0054")]
        public string x_0054 { get; set; }

        [EdiValue("9(99)", Path = "*/*/3")]
        [JsonProperty("E0051")]
        [XmlElement(ElementName = "E0051")]
        public string x_0051 { get; set; }

        [EdiValue("9(99)", Path = "*/*/4")]
        [JsonProperty("E0057")]
        [XmlElement(ElementName = "E0057")]
        public string x_0057 { get; set; }

        [EdiValue("9(99)", Path = "*/*/5")]
        [JsonProperty("E0110")]
        [XmlElement(ElementName = "E0110")]
        public string x_0110 { get; set; }

        [EdiValue("9(99)", Path = "*/*/6")]
        [JsonProperty("E0113")]
        [XmlElement(ElementName = "E0113")]
        public string x_0113 { get; set; }
    }

    [EdiElement, EdiPath("*/3")]
    public class S010
    {

        [EdiValue("9(99)", Path = "*/*/0")]
        [JsonProperty("E0070")]
        [XmlElement(ElementName = "E0070")]
        public string? x_0070 { get; set; }

        [EdiValue("9(99)", Path = "*/*/1")]
        [JsonProperty("E0073")]
        [XmlElement(ElementName = "E0073")]
        public string? x_0073 { get; set; }
    }

`

cezarypiatek commented 2 years ago

Looks like you already have nullable annotations. I will check your example and I will get back to you

cezarypiatek commented 2 years ago

I've just tested your example using the latest version MappingGenerator on VisualStudio 2022 and the mapping looks fine - nullable fields are handled properly. What version of VisualStudio and MappingGenerator do you use? What distribution of MappigGenerator - VSIX or nuget?

image

GingerNinjaa commented 2 years ago

Visual studio 2022 - 17.2.5 MappigGenerator - VSIX Current version 2022.7.45

cezarypiatek commented 2 years ago

What's the dotnet/csharp version of the project? Do you have this setting in project file https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/language#nullable ?

GingerNinjaa commented 2 years ago

Thank you! This solve my problem.