Skip to content

Commit

Permalink
fix: Allow deserialization of GitProvenance (#4399)
Browse files Browse the repository at this point in the history
* fix: Allow deserialization of GitProvenance

* transient is not needed

* cleanup

* simplify onConstructor and fix javadoc generation

* replace with explicit constructor. javadocs does not like the onConstructor approach
  • Loading branch information
pstreef authored Aug 8, 2024
1 parent d98fc05 commit 3a12433
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package org.openrewrite.marker;


import lombok.*;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.NonFinal;
import org.openrewrite.GitRemote;
import org.openrewrite.Incubating;
Expand Down Expand Up @@ -44,7 +48,6 @@

@Value
@AllArgsConstructor(access = AccessLevel.PACKAGE) // required for @With and tests
@RequiredArgsConstructor
@With
public class GitProvenance implements Marker {
UUID id;
Expand Down Expand Up @@ -77,6 +80,24 @@ public class GitProvenance implements Marker {
@Nullable
GitRemote gitRemote;

// javadoc does not like @RequiredArgsConstructor(onConstructor_ = { @JsonCreator })
@JsonCreator
public GitProvenance(UUID id,
@Nullable String origin,
@Nullable String branch,
@Nullable String change,
@Nullable AutoCRLF autocrlf,
@Nullable EOL eol,
@Nullable List<Committer> committers) {
this.id = id;
this.origin = origin;
this.branch = branch;
this.change = change;
this.autocrlf = autocrlf;
this.eol = eol;
this.committers = committers;
}

public @Nullable GitRemote getGitRemote() {
if (gitRemote == null && origin != null) {
gitRemote = new GitRemote.Parser().parse(origin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.openrewrite.marker;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -45,6 +48,8 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static com.fasterxml.jackson.core.JsonParser.Feature.IGNORE_UNDEFINED;
import static com.fasterxml.jackson.core.JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
Expand Down Expand Up @@ -396,6 +401,18 @@ void supportsTravis(@TempDir Path projectDir) throws Exception {
}
}

@Test
void serialization() throws JsonProcessingException {
GitProvenance gitProvenance = new GitProvenance(randomId(), "https://github.com/octocat/Hello-World.git", "main", "123", null, null, List.of());
ObjectMapper mapper = new ObjectMapper()
.findAndRegisterModules()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(INCLUDE_SOURCE_IN_LOCATION);
String json = mapper.writeValueAsString(gitProvenance);
GitProvenance read = mapper.readValue(json, GitProvenance.class);
assertThat(read).isEqualTo(gitProvenance);
}

void runCommand(Path workingDir, String command) {
//noinspection ResultOfMethodCallIgnored
workingDir.toFile().mkdirs();
Expand Down

0 comments on commit 3a12433

Please sign in to comment.