Skip to content

Commit

Permalink
PLUGINAPI-94 added enum to define response type for the endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-jarocki-sonarsource committed Jun 21, 2024
1 parent 0d3821a commit ea34fdc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 10.8

* Fixed an issue where WebService which was not meant to return any response still showed the warning in the logs when response example was not set.
* Introduced 'org.sonar.api.server.ws.WebService.NewAction.setContentType' for optionally setting a response type of Action.
* Replace internal library `commons-lang:commons-lang` by `org.apache.commons:commons-lang3`.
* Do not throw an exception when a rule parameter is not known in `org.sonar.api.batch.rule.Checks`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ interface Stream {

Stream stream();

/**
* The list of content types doesn't necessarily need to correspond to the ones defined in RFC2046.
*/
enum ContentType {
NO_CONTENT,
BINARY,
TEXT
}

}
18 changes: 17 additions & 1 deletion plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public boolean isInternal() {
class NewAction {
private final String key;
private static final String PAGE_PARAM_DESCRIPTION = "1-based page number";
private Enum<Response.ContentType> contentType;
private String deprecatedKey;
private String description;
private String since;
Expand Down Expand Up @@ -312,6 +313,15 @@ public NewAction setHandler(RequestHandler h) {
return this;
}

/**
* Sets content type of the response. This is optional to do.
* @since 10.8
*/
public NewAction setContentType(Enum<Response.ContentType> contentType) {
this.contentType = contentType;
return this;
}

/**
* Link to the document containing an example of response. Content must be UTF-8 encoded.
* <br>
Expand Down Expand Up @@ -503,6 +513,7 @@ class Action {
private final Map<String, Param> params;
private final URL responseExample;
private final List<Change> changelog;
private final Enum<Response.ContentType> contentType;

private Action(Controller controller, NewAction newAction) {
this.key = newAction.key;
Expand All @@ -516,11 +527,12 @@ private Action(Controller controller, NewAction newAction) {
this.responseExample = newAction.responseExample;
this.handler = newAction.handler;
this.changelog = newAction.changelog;
this.contentType = newAction.contentType;

checkState(this.handler != null, "RequestHandler is not set on action %s", path);
logWarningIf(this.description == null || this.description.isEmpty(), "Description is not set on action " + path);
logWarningIf(this.since == null || this.since.isEmpty(), "Since is not set on action " + path);
logWarningIf(!this.post && this.responseExample == null, "The response example is not set on action " + path);
logWarningIf(this.responseExample == null && isResponseExampleNeeded(), "The response example is not set on action " + path);

Map<String, Param> paramsBuilder = new HashMap<>();
for (NewParam newParam : newAction.newParams.values()) {
Expand All @@ -529,6 +541,10 @@ private Action(Controller controller, NewAction newAction) {
this.params = Collections.unmodifiableMap(paramsBuilder);
}

private boolean isResponseExampleNeeded() {
return !(this.post || this.contentType == Response.ContentType.BINARY || this.contentType == Response.ContentType.NO_CONTENT);
}

private static void logWarningIf(boolean condition, String message) {
if (condition) {
LOGGER.warn(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.event.Level;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.server.ws.WebService.NewController;
Expand Down Expand Up @@ -111,6 +112,21 @@ public void add_changelog_if_called_twice() {
tuple("2.0", "change2"));
}

@Test
public void dont_display_warning_if_response_example_is_not_set_on_no_content_endpoint() {
WebService webService = ctx -> {
NewController newController = ctx.createController("api/custom_action");
newDefaultAction(newController, "list")
.setResponseExample(null)
.setContentType(Response.ContentType.NO_CONTENT);
newController.done();
};

webService.define(context);

assertThat(logTester.getLogs(Level.WARN)).isEmpty();
}

@Test
public void fail_if_duplicated_ws_keys() {
MetricWs metricWs = new MetricWs();
Expand Down

0 comments on commit ea34fdc

Please sign in to comment.