package org.sonar.server.issue.ws;
import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
.setExampleValue("5bccd6e8-f525-43a2-8d76-fcb13dde79ef")
.setRequired(true);
action.createParam("tags")
- .setDescription("A comma separated list of tags")
- .setExampleValue("security,cwe,misra-c")
- .setRequired(true)
- .setDefaultValue("");
+ .setDescription("Comma-separated list of tags. All tags are removed if parameter is empty or not set.")
+ .setExampleValue("security,cwe,misra-c");
}
@Override
public void handle(Request request, Response response) throws Exception {
String key = request.mandatoryParam("key");
- String tags = request.mandatoryParam("tags");
+ String tags = Strings.nullToEmpty(request.param("tags"));
Collection<String> resultTags = service.setTags(key, ImmutableSet.copyOf(WS_TAGS_SPLITTER.split(tags)));
JsonWriter json = response.newJsonWriter().beginObject().name("tags").beginArray();
for (String tag : resultTags) {
action.createParam("ps")
.setDescription("The size of the list to return")
.setExampleValue("25")
- .setRequired(true)
.setDefaultValue("10");
}
@Override
public void handle(Request request, Response response) throws Exception {
- DbSession session = dbClient.openSession(false);
- try {
+ try (DbSession session = dbClient.openSession(false)) {
String componentKey = request.mandatoryParam("key");
UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, componentKey);
ComponentDto component = dbClient.componentDao().getByKey(session, componentKey);
if (lineHashes == null) {
response.noContent();
} else {
- OutputStream output = response.stream().setMediaType("text/plain").output();
- output.write(lineHashes.getBytes(Charsets.UTF_8));
- output.close();
+ try (OutputStream output = response.stream().setMediaType("text/plain").output()) {
+ output.write(lineHashes.getBytes(Charsets.UTF_8));
+ }
}
- } finally {
- session.close();
}
}
}
action
.createParam("from")
- .setRequired(true)
.setDefaultValue(1)
.setDescription("First line");
action
.createParam("to")
- .setRequired(false)
.setDescription("Last line (excluded). If not specified, all lines are returned until end of file");
}
UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
Integer from = request.mandatoryParamAsInt("from");
Integer to = request.paramAsInt("to");
- DbSession session = dbClient.openSession(false);
- try {
+ try (DbSession session = dbClient.openSession(false)) {
ComponentDto componentDto = dbClient.componentDao().getByKey(session, fileKey);
List<String> lines = sourceService.getLinesAsTxt(componentDto.uuid(), from, to == null ? null : to - 1);
JsonWriter json = response.newJsonWriter().beginArray().beginObject();
Integer lineCounter = from;
- for (String line: lines) {
+ for (String line : lines) {
json.prop(lineCounter.toString(), line);
- lineCounter ++;
+ lineCounter++;
}
json.endObject().endArray().close();
- } finally {
- session.close();
}
}
}
public void handle(Request request, Response response) {
String fileKey = request.mandatoryParam("key");
UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
- DbSession session = dbClient.openSession(false);
- try {
+ try (DbSession session = dbClient.openSession(false)) {
ComponentDto componentDto = dbClient.componentDao().getByKey(session, fileKey);
List<String> lines = sourceService.getLinesAsTxt(componentDto.uuid(), null, null);
response.stream().setMediaType("text/plain");
IOUtils.writeLines(lines, "\n", response.stream().output(), Charsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Fail to write raw source of file " + fileKey, e);
- } finally {
- session.close();
}
}
}
public class SourcesWs implements WebService {
private final ShowAction showAction;
- private final LinesAction show2Action;
+ private final LinesAction linesAction;
private final RawAction rawAction;
private final ScmAction scmAction;
private final HashAction hashAction;
private final IndexAction indexAction;
- public SourcesWs(ShowAction showAction, RawAction rawAction, ScmAction scmAction, LinesAction show2Action, HashAction hashAction, IndexAction indexAction) {
+ public SourcesWs(ShowAction showAction, RawAction rawAction, ScmAction scmAction, LinesAction linesAction, HashAction hashAction, IndexAction indexAction) {
this.showAction = showAction;
- this.show2Action = show2Action;
+ this.linesAction = linesAction;
this.rawAction = rawAction;
this.scmAction = scmAction;
this.hashAction = hashAction;
.setSince("4.2")
.setDescription("Display sources information");
showAction.define(controller);
- show2Action.define(controller);
+ linesAction.define(controller);
rawAction.define(controller);
scmAction.define(controller);
hashAction.define(controller);
assertThat(query.description()).isNotEmpty();
assertThat(query.exampleValue()).isNotEmpty();
Param pageSize = action.param("ps");
- assertThat(pageSize.isRequired()).isTrue();
+ assertThat(pageSize.isRequired()).isFalse();
assertThat(pageSize.defaultValue()).isEqualTo("10");
assertThat(pageSize.description()).isNotEmpty();
assertThat(pageSize.exampleValue()).isNotEmpty();
assertThat(query.description()).isNotEmpty();
assertThat(query.exampleValue()).isNotEmpty();
Param pageSize = action.param("tags");
- assertThat(pageSize.isRequired()).isTrue();
- assertThat(pageSize.defaultValue()).isEqualTo("");
+ assertThat(pageSize.isRequired()).isFalse();
+ assertThat(pageSize.defaultValue()).isNull();
assertThat(pageSize.description()).isNotEmpty();
assertThat(pageSize.exampleValue()).isNotEmpty();
}
create
.createParam("severity")
.setDescription("Severity")
- .setRequired(true)
+ .setRequired(false)
.setPossibleValues("BLOCKER", "INFO")
.setExampleValue("INFO")
.setDefaultValue("BLOCKER");
{
"key": "severity",
"description": "Severity",
- "required": true,
+ "required": false,
"defaultValue": "BLOCKER",
"exampleValue": "INFO",
"possibleValues": ["BLOCKER", "INFO"]
{
"key": "severity",
"description": "Severity",
- "required": true,
+ "required": false,
"defaultValue": "BLOCKER",
"exampleValue": "INFO",
"possibleValues": ["BLOCKER", "INFO"]
ImmutableMap.Builder<String, Param> paramsBuilder = ImmutableMap.builder();
for (NewParam newParam : newAction.newParams.values()) {
- paramsBuilder.put(newParam.key, new Param(newParam));
+ paramsBuilder.put(newParam.key, new Param(this, newParam));
}
this.params = paramsBuilder.build();
}
private final boolean required;
private final Set<String> possibleValues;
- public Param(NewParam newParam) {
+ public Param(Action action, NewParam newParam) {
this.key = newParam.key;
this.deprecatedKey = newParam.deprecatedKey;
this.description = newParam.description;
this.defaultValue = newParam.defaultValue;
this.required = newParam.required;
this.possibleValues = newParam.possibleValues;
+ if (required && defaultValue != null) {
+ throw new IllegalArgumentException(String.format("Default value must not be set on parameter '%s?%s' as it's marked as required", action, key));
+ }
}
public String key() {
assertThat(action.param("max").possibleValues()).isNull();
}
+ @Test
+ public void fail_if_required_param_has_default_value() {
+ try {
+ new WebService() {
+ @Override
+ public void define(Context context) {
+ NewController controller = context.createController("api/rule");
+ NewAction action = controller.createAction("create").setHandler(mock(RequestHandler.class));
+ action.createParam("key").setRequired(true).setDefaultValue("abc");
+ controller.done();
+ }
+ }.define(context);
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("Default value must not be set on parameter 'api/rule/create?key' as it's marked as required");
+ }
+ }
+
+
@Test
public void fail_if_duplicated_action_parameters() {
try {