fix(lambda): list $LATEST first, then versions in numeric order (#5376)

Sort stored versions numerically so ListVersionsByFunction is stable and matches AWS.

* Fix #5121

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-10 09:41:44 -04:00
1 parent 6d76af0834
commit 47dae887f9
3 files changed
+54 -4

No files matched your search

+1 -1
View File
@@ -23,7 +23,7 @@ Floci Lambda runs your function code locally inside real Docker containers - clo
| `UpdateEventSourceMapping` | Update a mapping |
| `DeleteEventSourceMapping` | Remove a mapping |
| `PublishVersion` | Publish an immutable version |
| `ListVersionsByFunction` | List all published versions of a function |
| `ListVersionsByFunction` | List all published versions of a function, `$LATEST` first and then published versions in ascending numeric order |
| `CreateAlias` | Create a named alias pointing to a version |
| `GetAlias` | Get alias details |
| `ListAliases` | List all aliases for a function |
@@ -10,6 +10,8 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
@@ -78,7 +80,7 @@ public class LambdaFunctionStore implements Resettable {
public void save(String region, LambdaFunction fn) {
// Remove old index entry if URL changed or was removed
get(region, fn.getFunctionName(), fn.getVersion()).ifPresent(this::deindexFunction);
backend.put(regionKey(region, fn.getFunctionName(), fn.getVersion()), fn);
indexFunction(fn);
}
@@ -140,7 +142,17 @@ public class LambdaFunctionStore implements Resettable {
public List<LambdaFunction> listVersions(String region, String functionName) {
String prefix = "lambda::" + region + "::" + functionName + "::";
return backend.scan(key -> key.startsWith(prefix));
List<LambdaFunction> versions = new ArrayList<>(backend.scan(key -> key.startsWith(prefix)));
versions.sort(Comparator.comparingLong(LambdaFunctionStore::versionRank));
return versions;
}
private static long versionRank(LambdaFunction fn) {
String version = fn.getVersion();
if (version == null || version.isEmpty() || !version.chars().allMatch(Character::isDigit)) {
return -1L;
}
return Long.parseLong(version);
}
/**
@@ -122,7 +122,7 @@ class LambdaVersionIntegrationTest {
.get(BASE_PATH + "/functions/" + FUNCTION_NAME)
.then()
.statusCode(404);
// Verify versions are gone (actually listVersionsByFunction should return 404 if function doesn't exist)
given()
.when()
@@ -258,6 +258,44 @@ class LambdaVersionIntegrationTest {
}
}
@Test
@Order(10)
void listVersionsByFunctionReturnsLatestFirstThenNumericOrder() {
String fnName = "ordered-versions-function";
given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"Runtime": "nodejs20.x",
"Role": "arn:aws:iam::000000000000:role/lambda-role",
"Handler": "index.handler"
}
""".formatted(fnName))
.when()
.post(BASE_PATH + "/functions")
.then()
.statusCode(201);
for (int i = 1; i <= 11; i++) {
given()
.contentType("application/json")
.body("{\"Description\": \"v" + i + "\"}")
.when()
.post(BASE_PATH + "/functions/" + fnName + "/versions")
.then()
.statusCode(201)
.body("Version", equalTo(String.valueOf(i)));
}
given()
.when()
.get(BASE_PATH + "/functions/" + fnName + "/versions")
.then()
.statusCode(200)
.body("Versions.Version", contains("$LATEST", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"));
}
private void createInvocableFunction(String fnName) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {