Skip to content

Commit

Permalink
add circular inclusion check
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Dec 25, 2024
1 parent ed9a901 commit 7b0f2cc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions data/circular/circular-1.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<mj-section css-class="bg-white">
<mj-column padding="32px 48px">
<mj-include path="circular-2.mjml" />
</mj-column>
</mj-section>
5 changes: 5 additions & 0 deletions data/circular/circular-2.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<mj-section css-class="bg-white">
<mj-column padding="32px 48px">
<mj-include path="circular-1.mjml" />
</mj-column>
</mj-section>
14 changes: 14 additions & 0 deletions data/circular/include-circular.mjml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<mjml>
<mj-body>

<mj-section css-class="bg-white">
<mj-column>
<mj-text padding="48px 48px 0 48px">
<p>This is the Index!</p>
</mj-text>
</mj-column>
</mj-section>

<mj-include path="/circular/circular-1.mjml" />
</mj-body>
</mjml>
5 changes: 4 additions & 1 deletion src/main/java/ch/digitalfondue/mjml4j/Mjml4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,10 @@ private static BaseComponent handleInclude(Element element, BaseComponent parent
try {
resolvedPath = includeResolver.resolvePath(path, context.currentResourcePaths.peek());
resource = includeResolver.readResource(resolvedPath);
} catch (IOException | IllegalStateException e) {
if (context.currentResourcePaths.contains(resolvedPath)) {
throw new IllegalStateException("Circular inclusion detected on file : " + resolvedPath);
}
} catch (IOException e) {
resource = "<!-- mj-include fails to read file : " + path + " at " + resolvedPath + " -->";
return new MjmlComponentRaw(element, parent, context, resource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static ch.digitalfondue.mjml4j.Helpers.testTemplate;

/**
* Unit test for simple App.
* Unit test for base cases.
*/
class BaseComponentTests {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.digitalfondue.mjml4j;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static ch.digitalfondue.mjml4j.Helpers.testTemplate;
Expand Down Expand Up @@ -127,4 +128,10 @@ void testIncludeAbout() {
void testDividerAttributeAll() {
testTemplate("mj-divider-mj-all");
}

@Test
void testCircularInclude() {
var t = Assertions.assertThrows(IllegalStateException.class, () -> testTemplate("circular/include-circular"));
Assertions.assertTrue(t.getMessage().startsWith("Circular inclusion detected on file"));
}
}

0 comments on commit 7b0f2cc

Please sign in to comment.