From 82141baa570f37adc2377b10f1cf3c3212a4602f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Tue, 24 Mar 2015 11:45:21 +0100 Subject: [PATCH] Do not show the debt (sub)characteristic of a rule when it has been overloaded as removed --- .../org/sonar/server/rule/ws/RuleMapping.java | 34 +++++++++++++++++-- ...how_rule_with_overridden_disable_debt.json | 2 -- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java index edb54625e5e..77cf01ec5a2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java @@ -106,8 +106,8 @@ public class RuleMapping extends BaseMapping { map("defaultDebtChar", new IndexStringMapper("defaultDebtChar", RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field())); map("defaultDebtSubChar", new IndexStringMapper("defaultDebtSubChar", RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field())); - map("debtChar", new IndexStringMapper("debtChar", RuleNormalizer.RuleField.CHARACTERISTIC.field())); - map("debtSubChar", new IndexStringMapper("debtSubChar", RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())); + map("debtChar", new CharacteristicMapper()); + map("debtSubChar", new SubCharacteristicMapper()); map("debtCharName", new CharacteristicNameMapper()); map("debtSubCharName", new SubCharacteristicNameMapper()); @@ -198,6 +198,34 @@ public class RuleMapping extends BaseMapping { return context == null || context.getFieldsToReturn().contains("debtSubCharName"); } + private static class CharacteristicMapper extends IndexMapper { + private CharacteristicMapper() { + super(RuleNormalizer.RuleField.CHARACTERISTIC.field()); + } + + @Override + public void write(JsonWriter json, RuleDoc rule, RuleMappingContext context) { + String debtCharacteristicKey = rule.debtCharacteristicKey(); + if (debtCharacteristicKey != null && !DebtCharacteristic.NONE.equals(debtCharacteristicKey)) { + json.prop("debtChar", debtCharacteristicKey); + } + } + } + + private static class SubCharacteristicMapper extends IndexMapper { + private SubCharacteristicMapper() { + super(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()); + } + + @Override + public void write(JsonWriter json, RuleDoc rule, RuleMappingContext context) { + String debtSubCharacteristicKey = rule.debtSubCharacteristicKey(); + if (debtSubCharacteristicKey != null && !DebtCharacteristic.NONE.equals(debtSubCharacteristicKey)) { + json.prop("debtSubChar", debtSubCharacteristicKey); + } + } + } + private static class CharacteristicNameMapper extends IndexMapper { private CharacteristicNameMapper() { super(RuleNormalizer.RuleField.CHARACTERISTIC.field()); @@ -237,7 +265,7 @@ class RuleMappingContext { private final Map debtCharacteristicNamesByKey = Maps.newHashMap(); @CheckForNull - public String debtCharacteristicName(String key) { + public String debtCharacteristicName(@Nullable String key) { return debtCharacteristicNamesByKey.get(key); } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule_with_overridden_disable_debt.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule_with_overridden_disable_debt.json index e4b2495aac3..10bb82b9b8a 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule_with_overridden_disable_debt.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/ShowActionMediumTest/show_rule_with_overridden_disable_debt.json @@ -7,8 +7,6 @@ "severity": "MINOR", "status": "BETA", "isTemplate": false, - "debtChar": "NONE", - "debtSubChar": "NONE", "debtOverloaded": true, "lang": "xoo" } -- 2.39.5