Skip to content

Commit 61848e6

Browse files
committed
review
1 parent 068dd6d commit 61848e6

6 files changed

Lines changed: 50 additions & 49 deletions

File tree

src/main/java/org/kohsuke/github/GitHubBuilder.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static GitHubBuilder fromCredentials() throws IOException {
161161

162162
private GitHubConnector connector;
163163

164-
private GitHubJackson jackson;
164+
private GitHubJackson jackson = DefaultGitHubJackson.createDefault();
165165

166166
private GitHubRateLimitChecker rateLimitChecker = new GitHubRateLimitChecker();
167167

@@ -194,7 +194,7 @@ public GitHub build() throws IOException {
194194
abuseLimitHandler,
195195
rateLimitChecker,
196196
authorizationProvider,
197-
jackson != null ? jackson : DefaultGitHubJackson.createDefault());
197+
jackson);
198198
}
199199

200200
/**
@@ -212,29 +212,23 @@ public GitHubBuilder clone() {
212212
}
213213

214214
/**
215-
* Configures the client to use Jackson 3.x for JSON serialization/deserialization.
215+
* Configures the client to use Jackson 2.x for JSON serialization/deserialization.
216216
*
217217
* <p>
218-
* By default, Jackson 2.x is used. Call this method to use Jackson 3.x instead.
218+
* By default, Jackson 3.x is used if available on the classpath, otherwise Jackson 2.x is used. Call this method to
219+
* explicitly use Jackson 2.x.
219220
* </p>
220221
*
221222
* <h3>Example</h3>
222223
*
223224
* <pre>
224-
* GitHub github = new GitHubBuilder().withOAuthToken("token").useJackson3().build();
225+
* GitHub github = new GitHubBuilder().withOAuthToken("token").useJackson2().build();
225226
* </pre>
226227
*
227-
* <p>
228-
* <strong>Note:</strong> To use Jackson 3.x, you must add the Jackson 3 {@code tools.jackson.core:jackson-databind}
229-
* dependency to your project.
230-
* </p>
231-
*
232228
* @return the GitHubBuilder
233-
* @throws IllegalStateException
234-
* if Jackson 3.x is not available on the classpath
235229
*/
236-
public GitHubBuilder useJackson3() {
237-
this.jackson = DefaultGitHubJackson.createJackson3();
230+
public GitHubBuilder useJackson2() {
231+
this.jackson = DefaultGitHubJackson.createJackson2();
238232
return this;
239233
}
240234

src/main/java/org/kohsuke/github/internal/DefaultGitHubJackson.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
* <h2>Usage</h2>
1212
*
1313
* <p>
14-
* By default, Jackson 2.x is used. To use Jackson 3.x, configure the {@link org.kohsuke.github.GitHubBuilder}:
14+
* By default, Jackson 3.x is used if available on the classpath, otherwise Jackson 2.x is used. To explicitly use
15+
* Jackson 2.x, configure the {@link org.kohsuke.github.GitHubBuilder}:
1516
* </p>
1617
*
1718
* <pre>
18-
* // Using Jackson 2.x (default)
19+
* // Using default (Jackson 3.x if available, otherwise Jackson 2.x)
1920
* GitHub github = new GitHubBuilder().withOAuthToken("token").build();
2021
*
21-
* // Using Jackson 3.x
22-
* GitHub github = new GitHubBuilder().withOAuthToken("token").useJackson3().build();
22+
* // Explicitly using Jackson 2.x
23+
* GitHub github = new GitHubBuilder().withOAuthToken("token").useJackson2().build();
2324
* </pre>
2425
*
2526
* <h2>Jackson 3.x Dependencies</h2>
@@ -39,13 +40,18 @@ public final class DefaultGitHubJackson {
3940
* Creates the default {@link GitHubJackson} instance.
4041
*
4142
* <p>
42-
* This method returns a Jackson 2.x implementation, which is the default and most stable option.
43+
* This method returns a Jackson 3.x implementation if available on the classpath, otherwise it returns a Jackson
44+
* 2.x implementation.
4345
* </p>
4446
*
45-
* @return a GitHubJackson2 instance
47+
* @return a GitHubJackson3 instance if Jackson 3.x is available, otherwise a GitHubJackson2 instance
4648
*/
4749
public static GitHubJackson createDefault() {
48-
return new GitHubJackson2();
50+
if (isJackson3Available()) {
51+
return new GitHubJackson3();
52+
} else {
53+
return new GitHubJackson2();
54+
}
4955
}
5056

5157
/**

src/main/java/org/kohsuke/github/internal/GitHubJackson.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@
1818
*
1919
* <h2>Available Implementations</h2>
2020
* <ul>
21-
* <li>{@link GitHubJackson2} - Jackson 2.x implementation (default)</li>
22-
* <li>{@link GitHubJackson3} - Jackson 3.x implementation (requires additional dependencies)</li>
21+
* <li>{@link GitHubJackson2} - Jackson 2.x implementation</li>
22+
* <li>{@link GitHubJackson3} - Jackson 3.x implementation (default when available)</li>
2323
* </ul>
2424
*
2525
* <h2>Configuration</h2>
2626
* <p>
27-
* Use {@link org.kohsuke.github.GitHubBuilder#useJackson3()} to configure the client to use Jackson 3.x:
27+
* By default, Jackson 3.x is used if available on the classpath, otherwise Jackson 2.x is used. Use
28+
* {@link org.kohsuke.github.GitHubBuilder#useJackson2()} to explicitly use Jackson 2.x:
2829
* </p>
2930
*
3031
* <pre>
31-
* // Use Jackson 3.x
32-
* GitHub github = new GitHubBuilder().useJackson3().build();
32+
* // Explicitly use Jackson 2.x
33+
* GitHub github = new GitHubBuilder().useJackson2().build();
3334
* </pre>
3435
*
3536
* @author Pierre Villard

src/main/java/org/kohsuke/github/internal/GitHubJackson3.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,10 @@
3131
* </p>
3232
*
3333
* <p>
34-
* To use Jackson 3.x, add the {@code tools.jackson.core:jackson-databind} dependency to your project.
34+
* To use Jackson 3.x, add the {@code tools.jackson.core:jackson-databind} dependency to your project. When Jackson 3.x
35+
* is available on the classpath, it is used by default.
3536
* </p>
3637
*
37-
* <p>
38-
* Then configure the GitHub client to use Jackson 3:
39-
* </p>
40-
*
41-
* <pre>
42-
* GitHub github = new GitHubBuilder().useJackson3().build();
43-
* </pre>
44-
*
4538
* @author Pierre Villard
4639
*/
4740
public class GitHubJackson3 implements GitHubJackson {

src/test/java/org/kohsuke/github/GitHubJacksonTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static org.hamcrest.CoreMatchers.*;
99

1010
/**
11-
* Tests for Jackson implementation selection via {@link GitHubBuilder#useJackson3()}.
11+
* Tests for Jackson implementation selection via {@link GitHubBuilder#useJackson2()}.
1212
*/
1313
public class GitHubJacksonTest extends AbstractGitHubWireMockTest {
1414

@@ -20,30 +20,32 @@ public GitHubJacksonTest() {
2020
}
2121

2222
/**
23-
* Test that the default Jackson implementation is Jackson 2.
23+
* Test that the default Jackson implementation is Jackson 3 when available.
2424
*
2525
* @throws IOException
2626
* the io exception
2727
*/
2828
@Test
29-
public void testDefaultJacksonIsJackson2() throws IOException {
29+
public void testDefaultJacksonIsJackson3WhenAvailable() throws IOException {
3030
gitHub = getGitHubBuilder().build();
3131
String implementationName = gitHub.getClient().getJacksonImplementationName();
32-
assertThat(implementationName, startsWith("Jackson 2."));
32+
if (DefaultGitHubJackson.isJackson3Available()) {
33+
assertThat(implementationName, startsWith("Jackson 3."));
34+
} else {
35+
assertThat(implementationName, startsWith("Jackson 2."));
36+
}
3337
}
3438

3539
/**
36-
* Test that Jackson 3 can be configured via builder when available.
40+
* Test that Jackson 2 can be explicitly configured via builder.
3741
*
3842
* @throws IOException
3943
* the io exception
4044
*/
4145
@Test
42-
public void testJackson3ViaBuilder() throws IOException {
43-
if (DefaultGitHubJackson.isJackson3Available()) {
44-
gitHub = getGitHubBuilder().useJackson3().build();
45-
String implementationName = gitHub.getClient().getJacksonImplementationName();
46-
assertThat(implementationName, startsWith("Jackson 3."));
47-
}
46+
public void testJackson2ViaBuilder() throws IOException {
47+
gitHub = getGitHubBuilder().useJackson2().build();
48+
String implementationName = gitHub.getClient().getJacksonImplementationName();
49+
assertThat(implementationName, startsWith("Jackson 2."));
4850
}
4951
}

src/test/java/org/kohsuke/github/internal/DefaultGitHubJacksonTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ public DefaultGitHubJacksonTest() {
1717
}
1818

1919
/**
20-
* Test that createDefault returns Jackson 2 implementation.
20+
* Test that createDefault returns Jackson 3 when available, otherwise Jackson 2.
2121
*/
2222
@Test
2323
public void testCreateDefault() {
2424
GitHubJackson jackson = DefaultGitHubJackson.createDefault();
2525
assertThat(jackson, notNullValue());
26-
assertThat(jackson, instanceOf(GitHubJackson2.class));
27-
assertThat(jackson.getImplementationName(), startsWith("Jackson 2."));
26+
if (DefaultGitHubJackson.isJackson3Available()) {
27+
assertThat(jackson, instanceOf(GitHubJackson3.class));
28+
assertThat(jackson.getImplementationName(), startsWith("Jackson 3."));
29+
} else {
30+
assertThat(jackson, instanceOf(GitHubJackson2.class));
31+
assertThat(jackson.getImplementationName(), startsWith("Jackson 2."));
32+
}
2833
}
2934

3035
/**

0 commit comments

Comments
 (0)