Skip to content

Commit

Permalink
Handle MavenMetadata parser result NPE as reported by community member (
Browse files Browse the repository at this point in the history
#4285)

* Handle MavenMetadata parser result NPE as reported by community member

* Replicate how metadata would be null
  • Loading branch information
timtebeek authored Jul 3, 2024
1 parent 477d3df commit 5698664
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,17 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
// A maven repository can be expressed as a URI with a file scheme
Path path = Paths.get(URI.create(uri));
if (Files.exists(path)) {
result = Optional.of(MavenMetadata.parse(Files.readAllBytes(path)));
MavenMetadata parsed = MavenMetadata.parse(Files.readAllBytes(path));
if (parsed != null) {
result = Optional.of(parsed);
}
}
} else {
byte[] responseBody = requestAsAuthenticatedOrAnonymous(repo, uri);
result = Optional.of(MavenMetadata.parse(responseBody));
MavenMetadata parsed = MavenMetadata.parse(responseBody);
if (parsed != null) {
result = Optional.of(parsed);
}
}
} catch (HttpSenderResponseException e) {
repositoryResponses.put(repo, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public static MavenMetadata parse(InputStream document) {
}
}

public static MavenMetadata parse(byte[] document) throws IOException {
public static @Nullable MavenMetadata parse(byte[] document) throws IOException {
MavenMetadata metadata = MavenXmlMapper.readMapper().readValue(document, MavenMetadata.class);
if (metadata.getVersioning() != null && metadata.getVersioning().getVersions() == null) {
if (metadata != null && metadata.getVersioning() != null && metadata.getVersioning().getVersions() == null) {
return new MavenMetadata(new Versioning(emptyList(), metadata.getVersioning().getSnapshotVersions(), metadata.getVersioning().getSnapshot()));
}
return metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.maven.tree.MavenMetadata;

import java.io.IOException;
Expand All @@ -28,20 +29,20 @@ class MavenMetadataTest {
@Test
void deserializeMetadata() throws IOException {
@Language("xml") String metadata = """
<metadata>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<versioning>
<latest>2.4.2</latest>
<release>2.4.2</release>
<versions>
<version>2.4.1</version>
<version>2.4.2</version>
</versions>
<lastUpdated>20210115042754</lastUpdated>
</versioning>
</metadata>
""";
<metadata>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<versioning>
<latest>2.4.2</latest>
<release>2.4.2</release>
<versions>
<version>2.4.1</version>
<version>2.4.2</version>
</versions>
<lastUpdated>20210115042754</lastUpdated>
</versioning>
</metadata>
""";

MavenMetadata parsed = MavenMetadata.parse(metadata.getBytes());
assertThat(parsed.getVersioning().getVersions()).hasSize(2);
Expand All @@ -51,39 +52,47 @@ void deserializeMetadata() throws IOException {
@Test
void deserializeSnapshotMetadata() throws IOException {
@Language("xml") String metadata = """
<metadata modelVersion="1.1.0">
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recommendations</artifactId>
<version>0.1.0-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20220927.033510</timestamp>
<buildNumber>223</buildNumber>
</snapshot>
<snapshotVersions>
<snapshotVersion>
<extension>pom.asc</extension>
<value>0.1.0-20220927.033510-223</value>
<updated>20220927033510</updated>
</snapshotVersion>
<snapshotVersion>
<extension>pom</extension>
<value>0.1.0-20220927.033510-223</value>
<updated>20220927033510</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>
</metadata>
""";
<metadata modelVersion="1.1.0">
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recommendations</artifactId>
<version>0.1.0-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20220927.033510</timestamp>
<buildNumber>223</buildNumber>
</snapshot>
<snapshotVersions>
<snapshotVersion>
<extension>pom.asc</extension>
<value>0.1.0-20220927.033510-223</value>
<updated>20220927033510</updated>
</snapshotVersion>
<snapshotVersion>
<extension>pom</extension>
<value>0.1.0-20220927.033510-223</value>
<updated>20220927033510</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>
</metadata>
""";

MavenMetadata parsed = MavenMetadata.parse(metadata.getBytes());
MavenMetadata.Versioning versioning = parsed.getVersioning();

assertThat(parsed.getVersioning().getSnapshot().getTimestamp()).isEqualTo("20220927.033510");
assertThat(parsed.getVersioning().getSnapshot().getBuildNumber()).isEqualTo("223");
assertThat(parsed.getVersioning().getVersions()).isNotNull();
assertThat(parsed.getVersioning().getSnapshotVersions()).hasSize(2);
assertThat(parsed.getVersioning().getSnapshotVersions().get(0).getExtension()).isNotNull();
assertThat(parsed.getVersioning().getSnapshotVersions().get(0).getValue()).isNotNull();
assertThat(parsed.getVersioning().getSnapshotVersions().get(0).getUpdated()).isNotNull();
assertThat(versioning.getSnapshot().getTimestamp()).isEqualTo("20220927.033510");
assertThat(versioning.getSnapshot().getBuildNumber()).isEqualTo("223");
assertThat(versioning.getVersions()).isNotNull();
assertThat(versioning.getSnapshotVersions()).hasSize(2);
assertThat(versioning.getSnapshotVersions().get(0).getExtension()).isNotNull();
assertThat(versioning.getSnapshotVersions().get(0).getValue()).isNotNull();
assertThat(versioning.getSnapshotVersions().get(0).getUpdated()).isNotNull();
}


@Test
@Issue("https://github.com/openrewrite/rewrite/pull/4285")
void deserializeMetadataWithEmptyVersions() throws IOException {
assertThat(MavenMetadata.parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata/>\n".getBytes())).isNull();
}
}

0 comments on commit 5698664

Please sign in to comment.