diff --git a/internal/maven/maven.go b/internal/maven/maven.go index 872a57f..f175295 100644 --- a/internal/maven/maven.go +++ b/internal/maven/maven.go @@ -92,10 +92,11 @@ type searchResponseBody struct { } type searchDoc struct { - ID string `json:"id"` - GroupID string `json:"g"` - ArtifactID string `json:"a"` - Version string `json:"latestVersion"` + ID string `json:"id"` + GroupID string `json:"g"` + ArtifactID string `json:"a"` + // core=gav returns the version in "v"; "latestVersion" only exists on the non-gav aggregated core. + Version string `json:"v"` Timestamp int64 `json:"timestamp"` VersionCount int `json:"versionCount"` } diff --git a/internal/maven/maven_test.go b/internal/maven/maven_test.go index b4eef91..7f684ca 100644 --- a/internal/maven/maven_test.go +++ b/internal/maven/maven_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "reflect" "testing" "github.com/git-pkgs/registries/internal/core" @@ -141,6 +142,40 @@ func TestFetchVersions(t *testing.T) { } } +func TestFetchVersionsParsesRealSolrGavShape(t *testing.T) { + const body = `{"responseHeader":{"status":0},"response":{"numFound":3,"start":0,"docs":[ + {"id":"org.slf4j:slf4j-api:2.0.17","g":"org.slf4j","a":"slf4j-api","v":"2.0.17","p":"jar","timestamp":1740501794416}, + {"id":"org.slf4j:slf4j-api:2.0.16","g":"org.slf4j","a":"slf4j-api","v":"2.0.16","p":"jar","timestamp":1725000000000}, + {"id":"org.slf4j:slf4j-api:1.7.36","g":"org.slf4j","a":"slf4j-api","v":"1.7.36","p":"jar","timestamp":1645000000000} + ]}}` + + mux := http.NewServeMux() + mux.HandleFunc("/solrsearch/select", func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(body)) + }) + server := httptest.NewServer(mux) + defer server.Close() + + reg := New(server.URL, core.DefaultClient()) + reg.searchURL = server.URL + + versions, err := reg.FetchVersions(context.Background(), "org.slf4j:slf4j-api") + if err != nil { + t.Fatalf("FetchVersions failed: %v", err) + } + got := make([]string, len(versions)) + for i, v := range versions { + if v.Number == "" { + t.Fatalf("version %d has empty Number - searchDoc is not reading the Solr \"v\" field", i) + } + got[i] = v.Number + } + want := []string{"2.0.17", "2.0.16", "1.7.36"} + if !reflect.DeepEqual(got, want) { + t.Fatalf("versions = %v, want %v", got, want) + } +} + func TestFetchVersionsFallback(t *testing.T) { mux := http.NewServeMux()