Skip to content

Commit 3a19f5d

Browse files
committed
fix: addOn availableFor nullPointerException
1 parent b066d1e commit 3a19f5d

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,40 @@ public static AddOn parseMapToAddOn(String addOnName, Map<String, Object> addOnM
9191

9292
private static void setAvailableFor(Map<String, Object> addOnMap, PricingManager pricingManager, AddOn addOn) {
9393

94-
List<String> plansAvailable = (List<String>) addOnMap.get("availableFor");
94+
Object plansAvailable = addOnMap.get("availableFor");
95+
List<String> plansAvailableList;
9596

96-
if (plansAvailable == null || plansAvailable.isEmpty()) {
97+
if (plansAvailable == null) {
9798
// If no plans are defined, the addOn is available for all plans
9899
plansAvailable = pricingManager.getPlans().keySet().stream().toList();
99100
}
100101

101-
for (String planName : plansAvailable) {
102+
if (!(plansAvailable instanceof List<?>)) {
103+
throw new PricingParsingException("The field \"availableFor\" should be a list");
104+
}
105+
106+
if (plansAvailable instanceof List<?>) {
107+
plansAvailableList = ((List<?>) plansAvailable).stream()
108+
.filter(item -> item instanceof String)
109+
.map(item -> (String) item)
110+
.toList();
111+
} else {
112+
throw new PricingParsingException("The field \"availableFor\" should be a list of strings");
113+
}
114+
115+
if (plansAvailableList.isEmpty()) {
116+
plansAvailableList = pricingManager.getPlans().keySet().stream().toList();
117+
}
118+
119+
for (String planName : plansAvailableList) {
102120
if (!pricingManager.getPlans().containsKey(planName)
103121
&& !pricingManager.getAddOns().containsKey(planName)) {
104122
throw new InvalidPlanException(
105123
"The plan or addOn " + planName + " is not defined in the pricing manager");
106124
}
107125
}
108126

109-
addOn.setAvailableFor(plansAvailable);
127+
addOn.setAvailableFor(plansAvailableList);
110128

111129
}
112130

0 commit comments

Comments
 (0)