From 76a154997cd2b447bc4cc4fc8336fcba7407dd29 Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Thu, 7 Jul 2022 10:36:32 +0200 Subject: [PATCH] SONAR-16614 add to api/rules/search and api/rules/show payload, the subfield key in the field ruleDescriptionSection.context --- .../java/org/sonar/server/rule/ws/RuleMapper.java | 5 ++++- .../org/sonar/server/rule/ws/search-example.json | 6 ++++-- .../org/sonar/server/rule/ws/show-example.json | 6 ++++-- .../org/sonar/server/rule/ws/SearchActionTest.java | 10 ++++++---- .../org/sonar/server/rule/ws/ShowActionTest.java | 12 ++++++------ sonar-ws/src/main/protobuf/ws-rules.proto | 1 + 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index 682fc6a3efa..196cf330a26 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -435,7 +435,10 @@ public class RuleMapper { private static Optional toProtobufContext(@Nullable RuleDescriptionSectionContextDto context) { return Optional.ofNullable(context) - .map(c -> newBuilder().setDisplayName(c.getDisplayName()).build()); + .map(c -> newBuilder() + .setKey(c.getKey()) + .setDisplayName(c.getDisplayName()) + .build()); } private static boolean isRemediationFunctionOverloaded(RuleDto rule) { diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/search-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/search-example.json index 830e4711565..cc1dfc76622 100644 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/search-example.json +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/search-example.json @@ -30,14 +30,16 @@ "key": "how_to_fix", "content": "

Recommended Secure Coding Practices

", "context": { - "displayName": "Spring" + "displayName": "Spring", + "key": "spring" } }, { "key": "how_to_fix", "content": "

Recommended Secure Coding Practices

", "context": { - "displayName": "Hibernate" + "displayName": "Hibernate", + "key": "hibernate" } } ], diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/show-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/show-example.json index efdc0860fac..ea42b51a842 100644 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/show-example.json +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/rule/ws/show-example.json @@ -31,14 +31,16 @@ "key": "how_to_fix", "content": "

Recommended Secure Coding Practices

", "context": { - "displayName": "Spring" + "displayName": "Spring", + "key": "spring" } }, { "key": "how_to_fix", "content": "

Recommended Secure Coding Practices

", "context": { - "displayName": "Hibernate" + "displayName": "Hibernate", + "key": "hibernate" } } ], diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java index de9ff93ddda..a4b683a5040 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java @@ -97,6 +97,7 @@ import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_ACTIVATION; import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_COMPARE_TO_PROFILE; import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_QPROFILE; import static org.sonar.server.rule.ws.RulesWsParameters.PARAM_RULE_KEY; +import static org.sonarqube.ws.Rules.Rule.DescriptionSection.Context.newBuilder; public class SearchActionTest { @@ -500,10 +501,11 @@ public class SearchActionTest { .build(); } - private static Rule.DescriptionSection toProtobufDto(RuleDescriptionSectionDto s) { - Rule.DescriptionSection.Builder builder = Rule.DescriptionSection.newBuilder().setKey(s.getKey()).setContent(s.getContent()); - if (s.getContext() != null) { - builder.setContext(Rule.DescriptionSection.Context.newBuilder().setDisplayName(s.getContext().getDisplayName()).build()); + private static Rule.DescriptionSection toProtobufDto(RuleDescriptionSectionDto section) { + Rule.DescriptionSection.Builder builder = Rule.DescriptionSection.newBuilder().setKey(section.getKey()).setContent(section.getContent()); + if (section.getContext() != null) { + RuleDescriptionSectionContextDto context = section.getContext(); + builder.setContext(newBuilder().setKey(context.getKey()).setDisplayName(context.getDisplayName()).build()); } return builder.build(); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java index 2645ff9c990..54b5775528a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java @@ -394,13 +394,13 @@ public class ShowActionTest { assertThat(resultRule.getMdDesc()).isEqualTo(resultRule.getHtmlDesc()); assertThat(resultRule.getDescriptionSections().getDescriptionSectionsList()) - .extracting(Rule.DescriptionSection::getKey, Rule.DescriptionSection::getContent, section -> section.getContext().getDisplayName()) + .extracting(Rule.DescriptionSection::getKey, Rule.DescriptionSection::getContent, section -> section.getContext().getKey(), section -> section.getContext().getDisplayName()) .containsExactlyInAnyOrder( - tuple(ROOT_CAUSE_SECTION_KEY, "
Root is Root
", ""), - tuple(ASSESS_THE_PROBLEM_SECTION_KEY, "
This is not a problem
", ""), - tuple(HOW_TO_FIX_SECTION_KEY, "
I don't want to fix
", ""), - tuple(RESOURCES_SECTION_KEY, "
I want to fix with Spring
", section4context1.getContext().getDisplayName()), - tuple(RESOURCES_SECTION_KEY, "
I want to fix with Servlet
", section4context2.getContext().getDisplayName()) + tuple(ROOT_CAUSE_SECTION_KEY, "
Root is Root
", "", ""), + tuple(ASSESS_THE_PROBLEM_SECTION_KEY, "
This is not a problem
", "", ""), + tuple(HOW_TO_FIX_SECTION_KEY, "
I don't want to fix
", "", ""), + tuple(RESOURCES_SECTION_KEY, "
I want to fix with Spring
", section4context1.getContext().getKey(), section4context1.getContext().getDisplayName()), + tuple(RESOURCES_SECTION_KEY, "
I want to fix with Servlet
", section4context2.getContext().getKey(), section4context2.getContext().getDisplayName()) ); } diff --git a/sonar-ws/src/main/protobuf/ws-rules.proto b/sonar-ws/src/main/protobuf/ws-rules.proto index fb943a1d97e..e1772529d3c 100644 --- a/sonar-ws/src/main/protobuf/ws-rules.proto +++ b/sonar-ws/src/main/protobuf/ws-rules.proto @@ -135,6 +135,7 @@ message Rule { message Context { required string displayName = 1; + required string key = 2; } } -- 2.39.5