Skip to content

Commit 862d044

Browse files
authored
fix: use the JDK version from the JAR when no language version is recorded (#1466)
Use the JDK version read from the Maven Central JAR when the database has no recorded language version. Signed-off-by: Jeff Lenamon <jlenamon@gmail.com>
1 parent 16c6a1e commit 862d044

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎src/macaron/build_spec_generator/common_spec/maven_spec.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def resolve_fields(self, purl: PackageURL) -> None:
8484
existing = self.data["language_version"][0] if self.data["language_version"] else None
8585

8686
# Select JDK from jar or another source, with a default of version 8.
87-
selected_jdk_version = jdk_from_jar or existing if existing else "8"
87+
selected_jdk_version = jdk_from_jar or existing or "8"
8888

8989
major_jdk_version = normalize_jdk_version(selected_jdk_version)
9090
if not major_jdk_version:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright (c) 2026 - 2026, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module tests the Maven build specification field resolution."""
5+
6+
from unittest.mock import patch
7+
8+
import pytest
9+
from packageurl import PackageURL
10+
11+
from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpecDict
12+
from macaron.build_spec_generator.common_spec.maven_spec import MavenBuildSpec
13+
14+
PURL_STRING = "pkg:maven/com.example/demo@1.0.0"
15+
16+
17+
def _build_spec_dict(language_version: list[str]) -> BaseBuildSpecDict:
18+
"""Return a minimal Maven build spec dict carrying the passed language version list."""
19+
return BaseBuildSpecDict(
20+
ecosystem="maven",
21+
purl=PURL_STRING,
22+
language="java",
23+
build_tools=["maven"],
24+
macaron_version="0.0.0",
25+
group_id="com.example",
26+
artifact_id="demo",
27+
version="1.0.0",
28+
language_version=list(language_version),
29+
build_commands=[
30+
{
31+
"build_tool": "maven",
32+
"build_config_path": "pom.xml",
33+
"command": ["mvn", "clean", "package"],
34+
"confidence_score": 1.0,
35+
}
36+
],
37+
)
38+
39+
40+
def _resolve(language_version: list[str], jdk_from_jar: str | None) -> list[str]:
41+
"""Resolve a Maven build spec with the JAR lookup pinned, and return the resulting language version."""
42+
data = _build_spec_dict(language_version)
43+
with patch(
44+
"macaron.build_spec_generator.common_spec.maven_spec.find_jdk_version_from_central_maven_repo",
45+
return_value=jdk_from_jar,
46+
):
47+
MavenBuildSpec(data).resolve_fields(PackageURL.from_string(PURL_STRING))
48+
return data["language_version"]
49+
50+
51+
@pytest.mark.parametrize(
52+
("existing", "jdk_from_jar", "expected"),
53+
[
54+
# The JAR manifest is the strongest evidence and wins whether or not the database
55+
# recorded a language version. The empty-list case is the ordinary one: core.py sets
56+
# "language_version" to [] whenever the database holds no recorded version.
57+
([], "17", ["17"]),
58+
(["11"], "17", ["17"]),
59+
# No JAR evidence: fall back to what the database recorded, then to the default of 8.
60+
(["11"], None, ["11"]),
61+
([], None, ["8"]),
62+
],
63+
)
64+
def test_resolve_fields_selects_jdk_version(existing: list[str], jdk_from_jar: str | None, expected: list[str]) -> None:
65+
"""The JDK read from the Maven Central JAR must not be discarded when the database recorded none."""
66+
assert _resolve(existing, jdk_from_jar) == expected

0 commit comments

Comments
 (0)