EDI.Net icon indicating copy to clipboard operation
EDI.Net copied to clipboard

Double UNB segment after creating EDIFACT

Open GingerNinjaa opened this issue 2 years ago • 2 comments

Hi,

I dont understand why I have double UNB segment when I deserialize my poco to EDIFACT. Could you pelase help me find the issue?

XML file

<?xml version="1.0" encoding="utf-16"?>
<APERAK xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<UNB>
		<S001>
			<E0001>UNOA</E0001>
			<E0002>3</E0002>
		</S001>
		<S002>
			<E0004>8717982000004</E0004>
			<E0007>14</E0007>
		</S002>
		<S003>
			<E0010>8717045000002</E0010>
			<E0007>14</E0007>
		</S003>
		<S004>
			<E0017>210504</E0017>
			<E0019>1310</E0019>
		</S004>
		<E0020>4</E0020>
		<S005>
			<E0022>210504</E0022>
			<E0025>1310</E0025>
		</S005>
		<E0026>5</E0026>
		<E0029>6</E0029>
		<E0031>7</E0031>
		<E0032>8</E0032>
	</UNB>
	<CONTENT>
		<UNH>
			<E0062>041310181</E0062>
			<S009>
				<E0065>APERAK</E0065>
				<E0052>D</E0052>
				<E0054>96A</E0054>
				<E0051>UN</E0051>
			</S009>
		</UNH>
		<BGM>
			<C002/>
			<C106>
				<E1004>0109094405</E1004>
			</C106>
		</BGM>
		<DTM>
			<C507>
				<E2005>137</E2005>
				<E2380>202105041310</E2380>
				<E2379>203</E2379>
			</C507>
		</DTM>
		<SG2>
			<RFF>
				<C506>
					<E1153>ON</E1153>
					<E1154>4506701767</E1154>
				</C506>
			</RFF>
			<DTM>
				<C507>
					<E2005>171</E2005>
					<E2380>20210504</E2380>
					<E2379>102</E2379>
				</C507>
			</DTM>
		</SG2>
		<SG3>
			<NAD>
				<E3035>BY</E3035>
				<C082>
					<E3039>8717045000248</E3039>
					<E3055>9</E3055>
				</C082>
			</NAD>
		</SG3>
		<SG3>
			<NAD>
				<E3035>SU</E3035>
				<C082>
					<E3039>8717982000004</E3039>
					<E3055>9</E3055>
				</C082>
			</NAD>
		</SG3>
		<SG3>
			<NAD>
				<E3035>DP</E3035>
				<C082>
					<E3039>8717045000248</E3039>
					<E3055>9</E3055>
				</C082>
			</NAD>
		</SG3>
		<UNT>
			<E0074>9</E0074>
			<E0062>041310181</E0062>
		</UNT>
	</CONTENT>
</APERAK>

Output File

UNA:+.? '
UNB++++4+++5+6+7+8'
UNB+UNOA:3+8717982000004:14+8717045000002:14+210504:1310++210504:1310'
UNH+041310181+APERAK:D:96A:UN'
BGM++0109094405'
DTM+137:202105041310:203'
RFF+ON:4506701767'
DTM+171:20210504:102'
NAD+BY+8717045000248::9'
NAD+SU+8717982000004::9'
NAD+DP+8717045000248::9'
UNT+9+041310181'

POCO obiect part

[EdiSegment, EdiPath("UNB")]
        public class UNB
        {
            [JsonProperty("S001")]
            [XmlElement(ElementName = "S001")]
            public S001? S001 { get; set; }

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


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

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

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

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

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

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

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

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

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

        [EdiElement, EdiPath("*/0")]
        public class S001
        {

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

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

        [EdiElement, EdiPath("*/1")]
        public class S002
        {
            [EdiValue("9(99)", Path = "*/*/0")]
            [JsonProperty("E0004")]
            [XmlElement(ElementName = "E0004")]
            public string? x_0004 { get; set; }

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

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

        [EdiElement, EdiPath("*/2")]
        public class S003
        {
            [EdiValue("9(99)", Path = "*/*/0")]
            [JsonProperty("E0010")]
            [XmlElement(ElementName = "E0010")]
            public string? x_0010 { get; set; }

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

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

        [EdiElement, EdiPath("*/3")]
        public class S004
        {
            [EdiValue("9(99)", Path = "*/*/0")]
            [JsonProperty("E0017")]
            [XmlElement(ElementName = "E0017")]
            public string? x_0017 { get; set; }

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

        }

        [EdiElement, EdiPath("*/5")]
        public class S005
        {
            [EdiValue("9(99)", Path = "*/*/0")]
            [JsonProperty("E0022")]
            [XmlElement(ElementName = "E0022")]
            public string? x_0022 { get; set; }

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

GingerNinjaa avatar Oct 29 '22 12:10 GingerNinjaa

Hi @GingerNinjaa can you add a pull request with a test that showcases the issue. This way I may be able to give it a try.

cleftheris avatar Oct 31 '22 15:10 cleftheris

I'm encountering the same - the string values in the object are getting output as a separate UNB before the nested objects in their own UNB

I saw that there was a PR created for this at https://github.com/indice-co/EDI.Net/pull/236 - does that help diagnose the issue?

EionRobb avatar Apr 29 '24 04:04 EionRobb

Hello @cleftheris, Cna you tell me if this issue is solved? if yes what is the solution?

GingerNinjaa avatar May 20 '24 06:05 GingerNinjaa

As a workaround I went with post-processing the output and merging all the UNB lines together. It's gross but it gets the job done

        var lines = outString.Split('\n');
        var mergedLines = new List<string>();
        for (int i = 0; i < lines.Length; i++)
        {
            if (lines[i].StartsWith("UNB"))
            {
                if (i + 1 < lines.Length && lines[i + 1].StartsWith("UNB"))
                {
                    var line1 = lines[i].Split('+');
                    var line2 = lines[i + 1].Split('+');
                    var maxLength = line1.Length > line2.Length ? line1.Length : line2.Length;
                    var mergedLine = new string[maxLength];
                    for (int j = 0; j < maxLength; j++)
                    {
                        if (j < line1.Length && !string.IsNullOrEmpty(line1[j]))
                        {
                            mergedLine[j] = line1[j];
                        }
                        else if (j < line2.Length)
                        {
                            mergedLine[j] = line2[j];
                        }
                    }

                    mergedLines.Add(string.Join("+", mergedLine));
                    i++; // Skip the next line since it has been merged
                }
                else
                {
                    mergedLines.Add(lines[i]);
                }
            }
            else
            {
                mergedLines.Add(lines[i]);
            }
        }
        var newOutString = string.Join("\n", mergedLines);

EionRobb avatar May 20 '24 07:05 EionRobb

Thank you @EionRobb this work for me

GingerNinjaa avatar May 20 '24 08:05 GingerNinjaa

Hi @GingerNinjaa thanks for the sample. I got this pinned down to property path substitution when using wildcard segments o the Segment definition itself. The wildcards where not substituted and when the UNB properties where ordered to prepare for serial write the element 4 what written before element 1. This made the serializer write a new element since it thought that the new path was starting from scratch.

cleftheris avatar May 24 '24 09:05 cleftheris

@EionRobb @GingerNinjaa The fix is up on nuget v1.12.0

cleftheris avatar May 24 '24 12:05 cleftheris