Skip to content

Commit 69eb110

Browse files
committed
[#747] Ensure model name is mandatory for ItemDefinition types
1 parent acb8279 commit 69eb110

20 files changed

+80
-108
lines changed

dmn-core/src/main/java/com/gs/dmn/feel/analysis/semantics/type/ItemDefinitionType.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@
1818
import java.util.stream.Collectors;
1919

2020
public class ItemDefinitionType extends NamedType implements CompositeDataType, com.gs.dmn.el.analysis.semantics.type.ItemDefinitionType {
21-
public static final Type ANY_ITEM_DEFINITION = new ItemDefinitionType("");
21+
public static final Type ANY_ITEM_DEFINITION = new ItemDefinitionType("", "");
2222

2323
private final String modelName;
2424
private final Map<String, Type> members = new LinkedHashMap<>();
2525
private final Map<String, List<String>> aliases = new LinkedHashMap<>();
2626

27-
public ItemDefinitionType(String name) {
28-
this(name, null);
29-
}
30-
3127
public ItemDefinitionType(String name, String modelName) {
3228
super(name);
3329
this.modelName = modelName;

dmn-core/src/main/java/com/gs/dmn/feel/analysis/semantics/type/NamedType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
*/
1313
package com.gs.dmn.feel.analysis.semantics.type;
1414

15+
import java.util.Objects;
16+
1517
public abstract class NamedType implements com.gs.dmn.el.analysis.semantics.type.NamedType, FEELType {
1618
protected final String name;
1719

1820
protected NamedType(String name) {
1921
this.name = name;
22+
Objects.requireNonNull(name, "Missing mandatory type name");
2023
}
2124

2225
@Override

dmn-core/src/main/java/com/gs/dmn/transformation/basic/BasicDMNToJavaTransformer.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,13 +1824,13 @@ private String toNativeTypeNoCache(Type type) {
18241824
if (StringUtils.isBlank(typeName)) {
18251825
throw new DMNRuntimeException(String.format("Missing type name in '%s'", type));
18261826
}
1827-
String primitiveType = this.nativeTypeFactory.toNativeType(typeName);
1828-
if (!StringUtils.isBlank(primitiveType)) {
1829-
return primitiveType;
1827+
if (type instanceof ItemDefinitionType) {
1828+
String modelName = ((ItemDefinitionType) type).getModelName();
1829+
return qualifiedName(nativeTypePackageName(modelName), upperCaseFirst(typeName));
18301830
} else {
1831-
if (type instanceof ItemDefinitionType) {
1832-
String modelName = ((ItemDefinitionType) type).getModelName();
1833-
return qualifiedName(nativeTypePackageName(modelName), upperCaseFirst(typeName));
1831+
String primitiveType = this.nativeTypeFactory.toNativeType(typeName);
1832+
if (!StringUtils.isBlank(primitiveType)) {
1833+
return primitiveType;
18341834
} else {
18351835
throw new DMNRuntimeException(String.format("Cannot infer native type for '%s' type", type));
18361836
}
@@ -2084,11 +2084,7 @@ protected String javaModelName(String name) {
20842084
@Override
20852085
public String nativeRootPackageName() {
20862086
String javaRootPackage = this.inputParameters.getJavaRootPackage();
2087-
if (javaRootPackage == null) {
2088-
return "";
2089-
} else {
2090-
return javaRootPackage;
2091-
}
2087+
return Objects.requireNonNullElse(javaRootPackage, "");
20922088
}
20932089

20942090
@Override

dmn-core/src/test/java/com/gs/dmn/AbstractTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.gs.dmn;
1414

15+
import com.gs.dmn.feel.analysis.semantics.type.ItemDefinitionType;
1516
import com.gs.dmn.log.BuildLogger;
1617
import com.gs.dmn.log.Slf4jBuildLogger;
1718
import com.gs.dmn.runtime.DMNRuntimeException;
@@ -67,8 +68,12 @@ protected String completePath(String pathFormat, String dmnVersion, String dmnFi
6768
protected Map<String, String> makeInputParametersMap() {
6869
Map<String, String> inputParams = new LinkedHashMap<>();
6970
inputParams.put("dmnVersion", "1.1");
70-
inputParams.put("modelVersion", "1.0");
71+
inputParams.put("modelVersion", "2.0");
7172
inputParams.put("platformVersion", "1.0");
7273
return inputParams;
7374
}
75+
76+
protected ItemDefinitionType makeItemDefinitionType(String itemDefinitionName) {
77+
return new ItemDefinitionType(itemDefinitionName, "model name");
78+
}
7479
}

dmn-core/src/test/java/com/gs/dmn/dialect/AbstractDMNDialectDefinitionTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
import com.gs.dmn.validation.NopDMNValidator;
2929
import org.junit.jupiter.api.Test;
3030

31-
import java.util.LinkedHashMap;
32-
import java.util.Map;
33-
3431
import static org.junit.jupiter.api.Assertions.assertEquals;
3532

3633
public abstract class AbstractDMNDialectDefinitionTest<NUMBER, DATE, TIME, DATE_TIME, DURATION, TEST> extends AbstractTest {
@@ -74,15 +71,6 @@ public void testGetDecisionBaseClass() {
7471
assertEquals(getExpectedDecisionBaseClass(), decisionBaseClass);
7572
}
7673

77-
@Override
78-
protected Map<String, String> makeInputParametersMap() {
79-
Map<String, String> map = new LinkedHashMap<>();
80-
map.put("dmnVersion", "1.1");
81-
map.put("modelVersion", "1.2");
82-
map.put("platformVersion", "3.2");
83-
return map;
84-
}
85-
8674
protected abstract DMNDialectDefinition<NUMBER,DATE,TIME,DATE_TIME,DURATION,TEST> makeDialect();
8775
protected abstract DMNModelRepository makeRepository();
8876
protected abstract TemplateProvider makeTemplateProvider();

dmn-core/src/test/java/com/gs/dmn/feel/AbstractFEELProcessorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,12 +2584,12 @@ public void testComplexArithmeticExpression() {
25842584

25852585
@Test
25862586
public void testPostfixExpression() {
2587-
ItemDefinitionType employeeTableType = new ItemDefinitionType("tEmployeeTable")
2587+
ItemDefinitionType employeeTableType = makeItemDefinitionType("tEmployeeTable")
25882588
.addMember("id", Collections.emptyList(), STRING)
25892589
.addMember("name", Collections.emptyList(), STRING)
25902590
.addMember("deptNum", Collections.emptyList(), NUMBER)
25912591
;
2592-
ItemDefinitionType deptTableType = new ItemDefinitionType("tDeptTable")
2592+
ItemDefinitionType deptTableType = makeItemDefinitionType("tDeptTable")
25932593
.addMember("number", Collections.emptyList(), NUMBER)
25942594
.addMember("name", Collections.emptyList(), STRING)
25952595
.addMember("manager", Collections.emptyList(), STRING)
@@ -2777,7 +2777,7 @@ public void testFunctionInvocationWhenMultipleMatch() {
27772777

27782778
@Test
27792779
public void testPathExpression() {
2780-
ItemDefinitionType type = new ItemDefinitionType("PrivateFundRequirements").addMember("HierarchyNode", Collections.emptyList(), STRING);
2780+
ItemDefinitionType type = makeItemDefinitionType("PrivateFundRequirements").addMember("HierarchyNode", Collections.emptyList(), STRING);
27812781
List<EnvironmentEntry> entries = Collections.singletonList(
27822782
new EnvironmentEntry("PrivateFundRequirements", type, null));
27832783

@@ -2791,15 +2791,15 @@ public void testPathExpression() {
27912791

27922792
@Test
27932793
public void testQualifiedName() {
2794-
Type bType = new ItemDefinitionType("b").addMember("c", Collections.singletonList("C"), STRING);
2795-
Type aType = new ItemDefinitionType("a").addMember("b", Collections.singletonList("B"), bType);
2794+
Type bType = makeItemDefinitionType("b").addMember("c", Collections.singletonList("C"), STRING);
2795+
Type aType = makeItemDefinitionType("a").addMember("b", Collections.singletonList("B"), bType);
27962796
List<EnvironmentEntry> entries = Collections.singletonList(
27972797
new EnvironmentEntry("a", aType, null));
27982798

27992799
doExpressionTest(entries, "", "a.b.c",
28002800
"PathExpression(PathExpression(Name(a), b), c)",
28012801
"string",
2802-
"((String)(((type.B)(a != null ? a.getB() : null)) != null ? ((type.B)(a != null ? a.getB() : null)).getC() : null))",
2802+
"((String)(((model_name.type.B)(a != null ? a.getB() : null)) != null ? ((model_name.type.B)(a != null ? a.getB() : null)).getC() : null))",
28032803
null,
28042804
null);
28052805
}

dmn-core/src/test/java/com/gs/dmn/feel/AbstractStandardFEELProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public void testFilterExpression() {
363363
public void testPathExpression() {
364364
super.testPathExpression();
365365

366-
ItemDefinitionType type = new ItemDefinitionType("PrivateFundRequirements").addMember("HierarchyNode", Collections.emptyList(), STRING);
366+
ItemDefinitionType type = makeItemDefinitionType("PrivateFundRequirements").addMember("HierarchyNode", Collections.emptyList(), STRING);
367367
List<EnvironmentEntry> entries = Collections.singletonList(
368368
new EnvironmentEntry("PrivateFundRequirements", type, null));
369369

dmn-core/src/test/java/com/gs/dmn/feel/OperatorDecisionTableTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.gs.dmn.feel;
1414

15+
import com.gs.dmn.AbstractTest;
1516
import com.gs.dmn.el.analysis.semantics.type.Type;
1617
import com.gs.dmn.feel.analysis.semantics.type.ContextType;
1718
import com.gs.dmn.feel.analysis.semantics.type.ItemDefinitionType;
@@ -39,7 +40,7 @@
3940
import static org.junit.jupiter.api.Assertions.assertEquals;
4041
import static org.junit.jupiter.api.Assertions.assertNull;
4142

42-
class OperatorDecisionTableTest {
43+
class OperatorDecisionTableTest extends AbstractTest {
4344
@Test
4445
public void testEquality() {
4546
// Exact types
@@ -60,7 +61,7 @@ public void testEquality() {
6061
ContextType contextType = new ContextType();
6162
contextType.addMember("a", Collections.emptyList(), NUMBER);
6263
check("=", contextType, CONTEXT, BOOLEAN, "contextEqual");
63-
ItemDefinitionType itemDefinitionType = new ItemDefinitionType("test");
64+
ItemDefinitionType itemDefinitionType = makeItemDefinitionType("test");
6465
itemDefinitionType.addMember("a", Collections.emptyList(), NUMBER);
6566
check("=", itemDefinitionType, ANY_ITEM_DEFINITION, BOOLEAN, "contextEqual");
6667
Type rangeType = new RangeType(NUMBER);

dmn-core/src/test/java/com/gs/dmn/feel/analysis/semantics/AddItemFilterVisitorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class AddItemFilterVisitorTest extends BaseVisitorTest {
3434
private final String parameterName = "parameterName";
3535
private final Type stringType = StringType.STRING;
3636
private final ContextType contextType = new ContextType().addMember(this.parameterName, Collections.emptyList(), this.stringType);
37-
private final ItemDefinitionType itemDefinitionType = new ItemDefinitionType("personType").addMember(this.parameterName, Collections.emptyList(), this.stringType);
37+
private final ItemDefinitionType itemDefinitionType = makeItemDefinitionType("personType").addMember(this.parameterName, Collections.emptyList(), this.stringType);
3838

3939
private final ErrorHandler errorHandler = NopErrorHandler.INSTANCE;
4040
private final AddItemFilterVisitor<Type, DMNContext> contextTypeVisitor = new AddItemFilterVisitor<>(this.parameterName, this.contextType, this.errorHandler);

dmn-core/src/test/java/com/gs/dmn/feel/analysis/semantics/type/TypeConformanceTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.gs.dmn.feel.analysis.semantics.type;
1414

15+
import com.gs.dmn.AbstractTest;
1516
import com.gs.dmn.context.environment.VariableDeclaration;
1617
import com.gs.dmn.el.analysis.semantics.type.NullType;
1718
import com.gs.dmn.el.analysis.semantics.type.Type;
@@ -40,7 +41,7 @@
4041
import static com.gs.dmn.feel.analysis.semantics.type.YearsAndMonthsDurationType.YEAR_MONTH_DURATION;
4142
import static org.junit.jupiter.api.Assertions.assertEquals;
4243

43-
public class TypeConformanceTest {
44+
public class TypeConformanceTest extends AbstractTest {
4445
public final Map<Pair<Type, Type>, Boolean> dataTypesTable = new LinkedHashMap<>() {{
4546
// Concrete types
4647
put(new Pair<>(NUMBER, ANY), true);
@@ -217,13 +218,13 @@ public void testContextType() {
217218

218219
@Test
219220
public void testItemDefinitionType() {
220-
ItemDefinitionType type1 = new ItemDefinitionType("ID1");
221+
ItemDefinitionType type1 = makeItemDefinitionType("ID1");
221222
type1.addMember("m", Collections.emptyList(), NUMBER);
222223

223-
ItemDefinitionType type2 = new ItemDefinitionType("ID2");
224+
ItemDefinitionType type2 = makeItemDefinitionType("ID2");
224225
type2.addMember("m", Collections.emptyList(), BOOLEAN);
225226

226-
ItemDefinitionType type3 = new ItemDefinitionType("ID3");
227+
ItemDefinitionType type3 = makeItemDefinitionType("ID3");
227228
type3.addMember("m", Collections.emptyList(), NUMBER);
228229
type3.addMember("x", Collections.emptyList(), NUMBER);
229230

0 commit comments

Comments
 (0)