Skip to content

Commit fb3714c

Browse files
committed
fix: error for parsing feature/usageLimit without value key
1 parent 9e1bf62 commit fb3714c

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

src/main/java/io/github/isagroup/services/parsing/AddOnParser.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,13 @@ private static void setAddOnFeatures(String addOnName, Map<String, Object> addOn
178178

179179
for (String addOnFeatureName : addOnFeaturesMap.keySet()) {
180180

181-
Map<String, Object> addOnFeatureMap = (Map<String, Object>) addOnFeaturesMap.get(addOnFeatureName);
181+
Object featureObj = addOnFeaturesMap.get(addOnFeatureName);
182+
if (!(featureObj instanceof Map)) {
183+
throw new PricingParsingException("The feature " + addOnFeatureName
184+
+ " of the add-on " + addOnName + " is not a valid map. Maybe 'value' attribute is missing to set the value of the feature");
185+
}
186+
@SuppressWarnings("unchecked")
187+
Map<String, Object> addOnFeatureMap = (Map<String, Object>) featureObj;
182188

183189
if (!globalFeaturesMap.containsKey(addOnFeatureName)) {
184190
throw new FeatureNotFoundException(
@@ -232,9 +238,23 @@ private static void setAddOnUsageLimits(String addOnName, Map<String, Object> ad
232238
Map<String, Object> addOnUsageLimitsMap = null;
233239

234240
if (areExtensions) {
235-
addOnUsageLimitsMap = (Map<String, Object>) addOnMap.get("usageLimitsExtensions");
241+
Object usageLimitsExtensionsObj = addOnMap.get("usageLimitsExtensions");
242+
if (usageLimitsExtensionsObj instanceof Map<?, ?>) {
243+
addOnUsageLimitsMap = (Map<String, Object>) usageLimitsExtensionsObj;
244+
} else if (usageLimitsExtensionsObj != null) {
245+
throw new PricingParsingException("The field \"usageLimitsExtensions\" should be a map. It is currently: "
246+
+ usageLimitsExtensionsObj.getClass().getSimpleName() + ". "
247+
+ "Maybe you forgot to add the 'value' attribute to the usage limit in the add-on definition.");
248+
}
236249
} else {
237-
addOnUsageLimitsMap = (Map<String, Object>) addOnMap.get("usageLimits");
250+
Object usageLimitsObj = addOnMap.get("usageLimits");
251+
if (usageLimitsObj instanceof Map<?, ?>) {
252+
addOnUsageLimitsMap = (Map<String, Object>) usageLimitsObj;
253+
} else if (usageLimitsObj != null) {
254+
throw new PricingParsingException("The field \"usageLimits\" should be a map. It is currently: "
255+
+ usageLimitsObj.getClass().getSimpleName() + ". "
256+
+ "Maybe you forgot to add the 'value' attribute to the usage limit in the add-on definition.");
257+
}
238258
}
239259
Map<String, UsageLimit> globalUsageLimitsMap = pricingManager.getUsageLimits();
240260
Map<String, UsageLimit> addOnUsageLimits = new LinkedHashMap<>();

src/main/java/io/github/isagroup/services/parsing/PlanParser.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ private static void setFeaturesToPlan(String planName, Map<String, Object> map,
103103

104104
for (String planFeatureName : planFeaturesMap.keySet()) {
105105

106-
Map<String, Object> planFeatureMap = (Map<String, Object>) planFeaturesMap.get(planFeatureName);
106+
Object planFeatureObj = planFeaturesMap.get(planFeatureName);
107+
if (!(planFeatureObj instanceof Map)) {
108+
throw new PricingParsingException("The feature " + planFeatureName
109+
+ " of the plan " + planName + " is not a valid map. Maybe 'value' attribute is missing to set the value of the feature");
110+
}
111+
@SuppressWarnings("unchecked")
112+
Map<String, Object> planFeatureMap = (Map<String, Object>) planFeatureObj;
107113

108114
if (!plan.getFeatures().containsKey(planFeatureName)) {
109115
throw new FeatureNotFoundException(
@@ -188,7 +194,13 @@ private static void setUsageLimitsToPlan(String planName, Map<String, Object> ma
188194

189195
for (String planUsageLimitName : planUsageLimitsMap.keySet()) {
190196

191-
Map<String, Object> planUsageLimitMap = (Map<String, Object>) planUsageLimitsMap.get(planUsageLimitName);
197+
Object planUsageLimitObj = planUsageLimitsMap.get(planUsageLimitName);
198+
if (!(planUsageLimitObj instanceof Map)) {
199+
throw new PricingParsingException("The usageLimit " + planUsageLimitName
200+
+ " of the plan " + planName + " is not a valid map. Maybe 'value' attribute is missing to set the value of the usageLimit");
201+
}
202+
@SuppressWarnings("unchecked")
203+
Map<String, Object> planUsageLimitMap = (Map<String, Object>) planUsageLimitObj;
192204

193205
if (!plan.getUsageLimits().containsKey(planUsageLimitName)) {
194206
throw new FeatureNotFoundException(

0 commit comments

Comments
 (0)