Skip to content
This repository was archived by the owner on Mar 28, 2026. It is now read-only.

Commit 34cd149

Browse files
Adds a way to configure whether the DSL source is retained via a workspace property named structurizr.dsl.source - true (default) or false.
1 parent f103a7a commit 34cd149

7 files changed

Lines changed: 74 additions & 10 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- structurizr-dsl: Adds `supportingTypes implementation-suffix <suffix>`.
1010
- structurizr-dsl: Fixes https://github.com/structurizr/java/issues/346 (`// comment \` joins lines).
1111
- structurizr-dsl: Anonymous identifiers for relationships (i.e. relationships not assigned to an identifier) are excluded from the model, and therefore also excluded from the serialised JSON.
12+
- structurizr-dsl: Adds a way to configure whether the DSL source is retained via a workspace property named `structurizr.dsl.source` - `true` (default) or `false`.
1213

1314
## 3.0.0 (19th September 2024)
1415

structurizr-dsl/src/main/java/com/structurizr/dsl/DslUtils.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
*/
1212
public class DslUtils {
1313

14-
private static final String STRUCTURIZR_DSL_PROPERTY_NAME = "structurizr.dsl";
14+
static final String STRUCTURIZR_DSL_PROPERTY_NAME = "structurizr.dsl";
15+
static final String STRUCTURIZR_DSL_RETAIN_SOURCE_PROPERTY_NAME = "structurizr.dsl.source";
1516

1617
/**
1718
* Gets the DSL associated with a workspace.
@@ -21,13 +22,11 @@ public class DslUtils {
2122
*/
2223
public static String getDsl(Workspace workspace) {
2324
String base64 = workspace.getProperties().get(STRUCTURIZR_DSL_PROPERTY_NAME);
24-
String dsl = "";
25-
2625
if (!StringUtils.isNullOrEmpty(base64)) {
27-
dsl = new String(Base64.getDecoder().decode(base64));
26+
return new String(Base64.getDecoder().decode(base64));
2827
}
2928

30-
return dsl;
29+
return "";
3130
}
3231

3332
/**
@@ -37,12 +36,10 @@ public static String getDsl(Workspace workspace) {
3736
* @param dsl the DSL string
3837
*/
3938
public static void setDsl(Workspace workspace, String dsl) {
40-
String base64 = "";
4139
if (!StringUtils.isNullOrEmpty(dsl)) {
42-
base64 = Base64.getEncoder().encodeToString(dsl.getBytes(StandardCharsets.UTF_8));
40+
String base64 = Base64.getEncoder().encodeToString(dsl.getBytes(StandardCharsets.UTF_8));
41+
workspace.addProperty(STRUCTURIZR_DSL_PROPERTY_NAME, base64);
4342
}
44-
45-
workspace.addProperty(STRUCTURIZR_DSL_PROPERTY_NAME, base64);
4643
}
4744

4845
}

structurizr-dsl/src/main/java/com/structurizr/dsl/StructurizrDslParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ public void setRestricted(boolean restricted) {
101101
*/
102102
public Workspace getWorkspace() {
103103
if (workspace != null) {
104-
DslUtils.setDsl(workspace, getParsedDsl());
104+
String value = workspace.getProperties().get(DslUtils.STRUCTURIZR_DSL_RETAIN_SOURCE_PROPERTY_NAME);
105+
if (value == null || value.equalsIgnoreCase("true")) {
106+
DslUtils.setDsl(workspace, getParsedDsl());
107+
}
105108
}
106109

107110
return workspace;

structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,4 +1377,42 @@ void test_ImageView_WhenParserIsInRestrictedMode() {
13771377
}
13781378
}
13791379

1380+
@Test
1381+
void test_sourceIsRetained() throws Exception {
1382+
File parentDslFile = new File("src/test/resources/dsl/source-parent.dsl");
1383+
StructurizrDslParser parser = new StructurizrDslParser();
1384+
parser.parse(parentDslFile);
1385+
Workspace workspace = parser.getWorkspace();
1386+
assertEquals("""
1387+
workspace {
1388+
1389+
model {
1390+
a = softwareSystem "A"
1391+
}
1392+
1393+
}""", DslUtils.getDsl(workspace));
1394+
1395+
File childDslFile = new File("src/test/resources/dsl/source-child.dsl");
1396+
parser = new StructurizrDslParser();
1397+
parser.parse(childDslFile);
1398+
workspace = parser.getWorkspace();
1399+
assertEquals("""
1400+
workspace extends source-parent.dsl {
1401+
1402+
model {
1403+
b = softwareSystem "B"
1404+
}
1405+
1406+
}""", DslUtils.getDsl(workspace));
1407+
}
1408+
1409+
@Test
1410+
void test_sourceIsNotRetained() throws Exception {
1411+
File parentDslFile = new File("src/test/resources/dsl/source-not-retained.dsl");
1412+
StructurizrDslParser parser = new StructurizrDslParser();
1413+
parser.parse(parentDslFile);
1414+
Workspace workspace = parser.getWorkspace();
1415+
assertNull(workspace.getProperties().get(DslUtils.STRUCTURIZR_DSL_PROPERTY_NAME));
1416+
}
1417+
13801418
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
workspace extends source-parent.dsl {
2+
3+
model {
4+
b = softwareSystem "B"
5+
}
6+
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
workspace {
2+
3+
properties {
4+
structurizr.dsl.source false
5+
}
6+
7+
model {
8+
a = softwareSystem "A"
9+
}
10+
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
workspace {
2+
3+
model {
4+
a = softwareSystem "A"
5+
}
6+
7+
}

0 commit comments

Comments
 (0)