Repository: xlate/staedi
Pull request #332 (2023-02-13 20:25:09)

Exception when attempting to copy an 837
I attempted to upgrade staedi from 1.17.0 to 1.22.0 and got this exception when attempting to create a copy of an 837 transaction in a manner very similar to what's happening in `StaEDIStreamWriterTest.testInputEquivalence`.

When I add a test case using the existing sample837 it fails with the same exception that I'm seeing:

```
io.xlate.edi.stream.EDIStreamException: Invalid X12 ISA segment: too short or elements missing
 at io.xlate.edi.internal.stream.StaEDIStreamWriter.writeEndSegment(StaEDIStreamWriter.java:549)
 at io.xlate.edi.internal.stream.StaEDIStreamWriterTest.writeFromReader(StaEDIStreamWriterTest.java:757)
 at io.xlate.edi.internal.stream.StaEDIStreamWriterTest.testInputEquivalence(StaEDIStreamWriterTest.java:717)
```

I'm assuming this is some sort of regression? but please advise. Thanks!

MikeEdgar: Hi @hoffrocket , thanks for finding this. If you don't mind adding commit https://github.com/MikeEdgar/staedi/commit/383523797e94e7a6eec487f62e81c0ce36712d0b to your branch, I'll get that merged in and release 1.22.1.
hoffrocket: @MikeEdgar I merged in your fix. Thanks for the amazingly fast response!
sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=xlate_staedi&pullRequest=332)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=xlate_staedi&pullRequest=332&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=xlate_staedi&pullRequest=332&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=xlate_staedi&pullRequest=332&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=xlate_staedi&pullRequest=332&resolved=false&types=CODE_SMELL)

[![100.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/100-16px.png '100.0%')](https://sonarcloud.io/component_measures?id=xlate_staedi&pullRequest=332&metric=new_coverage&view=list) [100.0% Coverage](https://sonarcloud.io/component_measures?id=xlate_staedi&pullRequest=332&metric=new_coverage&view=list)  
[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=xlate_staedi&pullRequest=332&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=xlate_staedi&pullRequest=332&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/src/main/java/io/xlate/edi/internal/stream/tokenization/State.java b/src/main/java/io/xlate/edi/internal/stream/tokenization/State.java
index 448ce6c..877651d 100644
--- a/src/main/java/io/xlate/edi/internal/stream/tokenization/State.java
+++ b/src/main/java/io/xlate/edi/internal/stream/tokenization/State.java
@@ -358,7 +358,7 @@ public enum State {
     }
 
     public boolean isHeaderState() {
-        return Category.HEADER == code;
+        return Category.HEADER == code && DialectCode.UNKNOWN != table;
     }
 
 }


Test patch:
diff --git a/src/test/java/io/xlate/edi/internal/stream/StaEDIStreamWriterTest.java b/src/test/java/io/xlate/edi/internal/stream/StaEDIStreamWriterTest.java
index 93b56e2..03168c8 100644
--- a/src/test/java/io/xlate/edi/internal/stream/StaEDIStreamWriterTest.java
+++ b/src/test/java/io/xlate/edi/internal/stream/StaEDIStreamWriterTest.java
@@ -682,12 +682,13 @@ class StaEDIStreamWriterTest {
 
     @ParameterizedTest
     @CsvSource({
-                 "X12 with BIN Segment, /x12/sample275_with_HL7_valid_BIN01.edi, false",
-                 "X12 with Non-ASCII Segment Terminator, /x12/issue109/ts214_ellipses_segterm.edi, false",
-                 "Basic TRADACOMS, /TRADACOMS/order.edi, false",
-                 "Basic TRADACOMS 40 Blocked, /TRADACOMS/order-blocked.edi, true",
+                 "X12 837, /x12/sample837-original.edi, false, true",
+                 "X12 with BIN Segment, /x12/sample275_with_HL7_valid_BIN01.edi, false, false",
+                 "X12 with Non-ASCII Segment Terminator, /x12/issue109/ts214_ellipses_segterm.edi, false, false",
+                 "Basic TRADACOMS, /TRADACOMS/order.edi, false, false",
+                 "Basic TRADACOMS 40 Blocked, /TRADACOMS/order-blocked.edi, true, false",
     })
-    void testInputEquivalence(String title, String resourceName, boolean blocked) throws Exception {
+    void testInputEquivalence(String title, String resourceName, boolean blocked, boolean indented) throws Exception {
         EDIInputFactory inputFactory = EDIInputFactory.newFactory();
         if (blocked) {
             inputFactory.setProperty(EDIInputFactory.EDI_IGNORE_EXTRANEOUS_CHARACTERS, "true");
@@ -715,7 +716,8 @@ class StaEDIStreamWriterTest {
         ByteArrayOutputStream result = new ByteArrayOutputStream(16384);
         writeFromReader(reader, blocked, result);
         final String expected = Stream.of(readBuffer.toString("UTF-8"))
-        	.map(StaEDITestUtil::normalizeLines)
+            .map(StaEDITestUtil::normalizeLines)
+            .map(exp -> indented ? exp.replaceAll("(?m)^\\s+", "") : exp) // Remove leading spaces in indented examples
             .map(exp -> blocked ? exp.replace("\n", "") : exp)
             .map(String::trim)
             .findFirst()