Skip to content

Commit c7a02d9

Browse files
authored
Remove jackson members from data objects (#2209)
* Remove jackson members from data objects * Fix coverage and site
1 parent 4e8a1ca commit c7a02d9

4 files changed

Lines changed: 94 additions & 82 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.kohsuke.github;
22

3-
import com.fasterxml.jackson.databind.node.ObjectNode;
43
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
54
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
65

@@ -84,8 +83,7 @@ static GHEvent transformTypeToGHEvent(String type) {
8483
private long id;
8584
private GHOrganization org;
8685

87-
// we don't want to expose Jackson dependency to the user. This needs databinding
88-
private ObjectNode payload;
86+
private Object payload;
8987

9088
// these are all shallow objects
9189
private GHEventRepository repo;
@@ -174,7 +172,9 @@ public GHOrganization getOrganization() throws IOException {
174172
* if payload cannot be parsed
175173
*/
176174
public <T extends GHEventPayload> T getPayload(Class<T> type) throws IOException {
177-
T v = GitHubClient.getMappingObjectReader(root()).forType(type).readValue(payload);
175+
T v = GitHubClient.getMappingObjectReader(root())
176+
.forType(type)
177+
.readValue(GitHubClient.getMappingObjectWriter().writeValueAsString(payload));
178178
v.lateBind();
179179
return v;
180180
}

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

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.kohsuke.github;
22

3-
import com.fasterxml.jackson.core.type.TypeReference;
4-
import com.fasterxml.jackson.databind.JsonNode;
3+
import com.fasterxml.jackson.databind.JavaType;
4+
import com.fasterxml.jackson.databind.ObjectReader;
55
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
66
import org.kohsuke.github.internal.EnumUtils;
77

@@ -13,7 +13,9 @@
1313
/**
1414
* Represents a repository rule.
1515
*/
16-
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
16+
@SuppressFBWarnings(
17+
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
18+
"CT_CONSTRUCTOR_THROW" },
1719
justification = "JSON API")
1820
public class GHRepositoryRule extends GitHubInteractiveObject {
1921

@@ -53,13 +55,7 @@ public static class BooleanParameter extends Parameter<Boolean> {
5355
* the key
5456
*/
5557
public BooleanParameter(String key) {
56-
super(key);
57-
}
58-
59-
@Override
60-
TypeReference<Boolean> getType() {
61-
return new TypeReference<Boolean>() {
62-
};
58+
super(key, Boolean.class);
6359
}
6460
}
6561
/**
@@ -115,30 +111,51 @@ public static class IntegerParameter extends Parameter<Integer> {
115111
* the key
116112
*/
117113
public IntegerParameter(String key) {
118-
super(key);
119-
}
120-
121-
@Override
122-
TypeReference<Integer> getType() {
123-
return new TypeReference<Integer>() {
124-
};
114+
super(key, Integer.class);
125115
}
126116
}
127117
/**
128118
* List parameter for a ruleset.
129119
*
130-
* @param <T>
131-
* the type of the list
120+
* @param <U>
121+
* the type of the items in the list
132122
*/
133-
public abstract static class ListParameter<T> extends Parameter<List<T>> {
123+
public abstract static class ListParameter<U> extends Parameter<List<U>> {
124+
125+
private final Class<U> itemClass;
126+
134127
/**
135128
* Instantiates a new list parameter.
136129
*
137130
* @param key
138131
* the key
139132
*/
140133
public ListParameter(String key) {
141-
super(key);
134+
super(key, null);
135+
throw new GHException("This constructor should not have been public.");
136+
}
137+
138+
/**
139+
* Instantiates a new list parameter.
140+
*
141+
* @param key
142+
* the key
143+
* @param itemClass
144+
* the class of items in the list parameter
145+
*/
146+
ListParameter(String key, Class<U> itemClass) {
147+
super(key, null);
148+
this.itemClass = itemClass;
149+
}
150+
151+
@Override
152+
List<U> apply(String value, GitHub root) throws IOException {
153+
if (value == null) {
154+
return null;
155+
}
156+
ObjectReader objectReader = GitHubClient.getMappingObjectReader(root);
157+
JavaType javaType = objectReader.getTypeFactory().constructParametricType(List.class, itemClass);
158+
return objectReader.forType(javaType).readValue(value);
142159
}
143160
}
144161
/**
@@ -174,6 +191,7 @@ public static enum Operator {
174191
*/
175192
public abstract static class Parameter<T> {
176193

194+
private final Class<T> clazz;
177195
private final String key;
178196

179197
/**
@@ -183,14 +201,28 @@ public abstract static class Parameter<T> {
183201
* the key
184202
*/
185203
protected Parameter(String key) {
204+
throw new GHException("This constructor should not have been protected.");
205+
}
206+
207+
/**
208+
* Instantiates a new parameter.
209+
*
210+
* @param key
211+
* the key
212+
* @param clazz
213+
* the class the the parameter
214+
*/
215+
Parameter(String key, Class<T> clazz) {
186216
this.key = key;
217+
this.clazz = clazz;
187218
}
188219

189-
T apply(JsonNode jsonNode, GitHub root) throws IOException {
190-
if (jsonNode == null) {
220+
T apply(String value, GitHub root) throws IOException {
221+
if (value == null) {
191222
return null;
192223
}
193-
return GitHubClient.getMappingObjectReader(root).forType(this.getType()).readValue(jsonNode);
224+
ObjectReader objectReader = GitHubClient.getMappingObjectReader(root);
225+
return objectReader.forType(clazz).readValue(value);
194226
}
195227

196228
/**
@@ -201,11 +233,6 @@ T apply(JsonNode jsonNode, GitHub root) throws IOException {
201233
String getKey() {
202234
return this.key;
203235
}
204-
205-
/**
206-
* Get the parameter type reference for type mapping.
207-
*/
208-
abstract TypeReference<T> getType();
209236
}
210237

211238
/**
@@ -216,12 +243,8 @@ public interface Parameters {
216243
* code_scanning_tools parameter
217244
*/
218245
public static final ListParameter<CodeScanningTool> CODE_SCANNING_TOOLS = new ListParameter<CodeScanningTool>(
219-
"code_scanning_tools") {
220-
@Override
221-
TypeReference<List<CodeScanningTool>> getType() {
222-
return new TypeReference<List<CodeScanningTool>>() {
223-
};
224-
}
246+
"code_scanning_tools",
247+
CodeScanningTool.class) {
225248
};
226249
/**
227250
* dismiss_stale_reviews_on_push parameter
@@ -239,12 +262,7 @@ TypeReference<List<CodeScanningTool>> getType() {
239262
/**
240263
* operator parameter
241264
*/
242-
public static final Parameter<Operator> OPERATOR = new Parameter<Operator>("operator") {
243-
@Override
244-
TypeReference<Operator> getType() {
245-
return new TypeReference<Operator>() {
246-
};
247-
}
265+
public static final Parameter<Operator> OPERATOR = new Parameter<Operator>("operator", Operator.class) {
248266
};
249267
/**
250268
* regex parameter
@@ -259,12 +277,8 @@ TypeReference<Operator> getType() {
259277
* required_deployment_environments parameter
260278
*/
261279
public static final ListParameter<String> REQUIRED_DEPLOYMENT_ENVIRONMENTS = new ListParameter<String>(
262-
"required_deployment_environments") {
263-
@Override
264-
TypeReference<List<String>> getType() {
265-
return new TypeReference<List<String>>() {
266-
};
267-
}
280+
"required_deployment_environments",
281+
String.class) {
268282
};
269283
/**
270284
* required_review_thread_resolution parameter
@@ -275,12 +289,8 @@ TypeReference<List<String>> getType() {
275289
* required_status_checks parameter
276290
*/
277291
public static final ListParameter<StatusCheckConfiguration> REQUIRED_STATUS_CHECKS = new ListParameter<StatusCheckConfiguration>(
278-
"required_status_checks") {
279-
@Override
280-
TypeReference<List<StatusCheckConfiguration>> getType() {
281-
return new TypeReference<List<StatusCheckConfiguration>>() {
282-
};
283-
}
292+
"required_status_checks",
293+
StatusCheckConfiguration.class) {
284294
};
285295
/**
286296
* require_code_owner_review parameter
@@ -306,12 +316,8 @@ TypeReference<List<StatusCheckConfiguration>> getType() {
306316
* workflows parameter
307317
*/
308318
public static final ListParameter<WorkflowFileReference> WORKFLOWS = new ListParameter<WorkflowFileReference>(
309-
"workflows") {
310-
@Override
311-
TypeReference<List<WorkflowFileReference>> getType() {
312-
return new TypeReference<List<WorkflowFileReference>>() {
313-
};
314-
}
319+
"workflows",
320+
WorkflowFileReference.class) {
315321
};
316322
}
317323

@@ -409,13 +415,7 @@ public static class StringParameter extends Parameter<String> {
409415
* the key
410416
*/
411417
public StringParameter(String key) {
412-
super(key);
413-
}
414-
415-
@Override
416-
TypeReference<String> getType() {
417-
return new TypeReference<String>() {
418-
};
418+
super(key, String.class);
419419
}
420420
}
421421

@@ -562,7 +562,7 @@ public String getSha() {
562562
}
563563
}
564564

565-
private Map<String, JsonNode> parameters;
565+
private Map<String, Object> parameters;
566566

567567
private long rulesetId;
568568

@@ -593,11 +593,12 @@ public <T> Optional<T> getParameter(Parameter<T> parameter) throws IOException {
593593
if (this.parameters == null) {
594594
return Optional.empty();
595595
}
596-
JsonNode jsonNode = this.parameters.get(parameter.getKey());
597-
if (jsonNode == null) {
596+
Object value = this.parameters.get(parameter.getKey());
597+
if (value == null) {
598598
return Optional.empty();
599599
}
600-
return Optional.ofNullable(parameter.apply(jsonNode, root()));
600+
return Optional
601+
.ofNullable(parameter.apply(GitHubClient.getMappingObjectWriter().writeValueAsString(value), root()));
601602
}
602603

603604
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void testIfAllRequiredClassesAreRegisteredForAot() throws IOException {
8080

8181
private Stream<String> readAotConfigToStreamOfClassNames(String reflectionConfig) throws IOException {
8282
byte[] reflectionConfigFileAsBytes = Files.readAllBytes(Path.of(reflectionConfig));
83+
// Test methods are allowed to directly use whatever jackson is available.
8384
ArrayNode reflectConfigJsonArray = (ArrayNode) JsonMapper.builder()
8485
.build()
8586
.readTree(reflectionConfigFileAsBytes);

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import static org.hamcrest.Matchers.is;
1717
import static org.hamcrest.Matchers.notNullValue;
1818
import static org.hamcrest.Matchers.nullValue;
19+
import static org.junit.Assert.assertThrows;
1920

2021
/**
2122
* Test class for GHRepositoryRule.
2223
*/
23-
public class GHRepositoryRuleTest {
24+
public class GHRepositoryRuleTest extends AbstractGitHubWireMockTest {
2425

2526
/**
2627
* Create default GHRepositoryRuleTest instance
@@ -70,15 +71,24 @@ public void testParameterReturnsNullOnNullArg() throws Exception {
7071

7172
/**
7273
* Test to cover the constructor of the Parameters class.
74+
*
75+
* @throws Exception
76+
* if something goes wrong.
7377
*/
7478
@Test
75-
public void testParameters() {
76-
assertThat(Parameters.REQUIRED_DEPLOYMENT_ENVIRONMENTS.getType(), is(notNullValue()));
77-
assertThat(Parameters.REQUIRED_STATUS_CHECKS.getType(), is(notNullValue()));
78-
assertThat(Parameters.OPERATOR.getType(), is(notNullValue()));
79-
assertThat(Parameters.WORKFLOWS.getType(), is(notNullValue()));
80-
assertThat(Parameters.CODE_SCANNING_TOOLS.getType(), is(notNullValue()));
81-
assertThat(new StringParameter("any").getType(), is(notNullValue()));
79+
public void testParameters() throws Exception {
80+
assertThat(Parameters.REQUIRED_DEPLOYMENT_ENVIRONMENTS, is(notNullValue()));
81+
assertThat(Parameters.REQUIRED_STATUS_CHECKS, is(notNullValue()));
82+
assertThat(Parameters.OPERATOR, is(notNullValue()));
83+
assertThat(Parameters.WORKFLOWS, is(notNullValue()));
84+
assertThat(Parameters.CODE_SCANNING_TOOLS, is(notNullValue()));
85+
assertThat(Parameters.CODE_SCANNING_TOOLS.apply("[]", this.gitHub), is(notNullValue()));
86+
assertThat(new StringParameter("any"), is(notNullValue()));
87+
88+
assertThrows(GHException.class, () -> new GHRepositoryRule.ListParameter<Object>("") {
89+
});
90+
assertThrows(GHException.class, () -> new GHRepositoryRule.Parameter<Object>("") {
91+
});
8292
}
8393

8494
/**

0 commit comments

Comments
 (0)