Skip to content

Commit d6c3a02

Browse files
committed
fix(model-server): respond with 404 when querying a non-existent branch
1 parent 7df96c5 commit d6c3a02

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/ModelReplicationServer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ class ModelReplicationServer(
287287
val branchRef = repositoryId(repository).getBranchReference(branch)
288288
checkPermission(ModelServerPermissionSchema.branch(branchRef).query)
289289
runWithRepository(repository) {
290-
val version = repositoriesManager.getVersion(branchRef)
290+
val version = repositoriesManager.getVersion(branchRef) ?: throw BranchNotFoundException(branchRef)
291291
LOG.trace("Running query on {} @ {}", branchRef, version)
292-
val initialTree = version!!.getTree()
292+
val initialTree = version.getTree()
293293
val otBranch = OTBranch(
294294
PBranch(initialTree, modelClient.idGenerator),
295295
modelClient.idGenerator,

model-server/src/test/kotlin/org/modelix/model/server/handlers/ModelReplicationServerTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import io.ktor.client.plugins.defaultRequest
2828
import io.ktor.client.request.accept
2929
import io.ktor.client.request.delete
3030
import io.ktor.client.request.get
31+
import io.ktor.client.request.post
3132
import io.ktor.client.statement.bodyAsText
3233
import io.ktor.http.ContentType
3334
import io.ktor.http.HttpHeaders
@@ -191,6 +192,15 @@ class ModelReplicationServerTest {
191192
}
192193
}
193194

195+
@Test
196+
fun `responds with 404 when querying non-existent branch`() = runWithTestModelServer { _, _ ->
197+
val repositoryId = "abc"
198+
val branch = "non-existing-branch"
199+
val response = client.post("/v2/repositories/$repositoryId/branches/$branch/query")
200+
response shouldHaveStatus 404
201+
assertContains(response.bodyAsText(), "Branch '$branch' does not exist in repository '$repositoryId'")
202+
}
203+
194204
@Test
195205
fun `successfully deletes existing branches`() {
196206
val repositoryId = RepositoryId("repo1")

0 commit comments

Comments
 (0)