Merge "Do not fail with 500 ISE if user doesn't provide content for change edit"

This commit is contained in:
David Pursehouse
2019-12-31 16:23:19 +00:00
committed by Gerrit Code Review
2 changed files with 19 additions and 6 deletions

View File

@@ -30,7 +30,6 @@ import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RawInput;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
@@ -120,7 +119,8 @@ public class ChangeEdits implements ChildCollection<ChangeResource, ChangeEditRe
@Override
public Response<?> apply(ChangeResource resource, IdString id, FileContentInput input)
throws AuthException, ResourceConflictException, IOException, PermissionBackendException {
throws AuthException, BadRequestException, ResourceConflictException, IOException,
PermissionBackendException {
putEdit.apply(resource, id.get(), input);
return Response.none();
}
@@ -280,19 +280,24 @@ public class ChangeEdits implements ChildCollection<ChangeResource, ChangeEditRe
@Override
public Response<?> apply(ChangeEditResource rsrc, FileContentInput input)
throws AuthException, ResourceConflictException, IOException, PermissionBackendException {
throws AuthException, BadRequestException, ResourceConflictException, IOException,
PermissionBackendException {
return apply(rsrc.getChangeResource(), rsrc.getPath(), input);
}
public Response<?> apply(ChangeResource rsrc, String path, FileContentInput input)
throws ResourceConflictException, AuthException, IOException, PermissionBackendException {
throws AuthException, BadRequestException, ResourceConflictException, IOException,
PermissionBackendException {
if (Strings.isNullOrEmpty(path) || path.charAt(0) == '/') {
throw new ResourceConflictException("Invalid path: " + path);
}
RawInput newContent = input.content;
if (input.content == null) {
throw new BadRequestException("new content required");
}
try (Repository repository = repositoryManager.openRepository(rsrc.getProject())) {
editModifier.modifyFile(repository, rsrc.getNotes(), path, newContent);
editModifier.modifyFile(repository, rsrc.getNotes(), path, input.content);
} catch (InvalidChangeOperationException e) {
throw new ResourceConflictException(e.getMessage());
}

View File

@@ -519,6 +519,14 @@ public class ChangeEditIT extends AbstractDaemonTest {
ensureSameBytes(getFileContentOfEdit(changeId, FILE_NAME), CONTENT_NEW);
}
@Test
public void changeEditNoContentProvidedRest() throws Exception {
createEmptyEditFor(changeId);
adminRestSession
.put(urlEditFile(changeId, FILE_NAME), new FileContentInput())
.assertBadRequest();
}
@Test
public void emptyPutRequest() throws Exception {
createEmptyEditFor(changeId);