Skip to content

Commit 21172bd

Browse files
author
ci.datadog-api-spec
committed
pre-commit fixes
1 parent 14f8bae commit 21172bd

File tree

8 files changed

+527
-503
lines changed

8 files changed

+527
-503
lines changed

src/main/java/com/datadog/api/client/v1/model/DistributionPointItem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public DistributionPointItem deserialize(JsonParser jp, DeserializationContext c
147147
}
148148
}
149149
if (attemptParsing) {
150-
tmp = tree.traverse(jp.getCodec()).readValueAs(List.class);
150+
// Using TypeReference for parameterized type List<Double> (Java 8 compatible)
151+
tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<List<Double>>() {});
151152
// TODO: there is no validation against JSON schema constraints
152153
// (min, max, enum, pattern...), this does not perform a strict JSON
153154
// validation, which means the 'match' count may be higher than it should be.

src/main/java/com/datadog/api/client/v1/model/SharedDashboardInvitesData.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ public SharedDashboardInvitesData deserialize(JsonParser jp, DeserializationCont
153153
}
154154
}
155155
if (attemptParsing) {
156-
tmp = tree.traverse(jp.getCodec()).readValueAs(List.class);
156+
// Using TypeReference for parameterized type List<SharedDashboardInvitesDataObject> (Java
157+
// 8 compatible)
158+
tmp =
159+
tree.traverse(jp.getCodec())
160+
.readValueAs(new TypeReference<List<SharedDashboardInvitesDataObject>>() {});
157161
// TODO: there is no validation against JSON schema constraints
158162
// (min, max, enum, pattern...), this does not perform a strict JSON
159163
// validation, which means the 'match' count may be higher than it should be.

src/main/java/com/datadog/api/client/v2/model/ActionQuerySpecInputs.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ public ActionQuerySpecInputs deserialize(JsonParser jp, DeserializationContext c
146146
}
147147
}
148148
if (attemptParsing) {
149-
tmp = tree.traverse(jp.getCodec()).readValueAs(Map.class);
149+
// Using TypeReference for parameterized type Map<String, Object> (Java 8 compatible)
150+
tmp =
151+
tree.traverse(jp.getCodec()).readValueAs(new TypeReference<Map<String, Object>>() {});
150152
// TODO: there is no validation against JSON schema constraints
151153
// (min, max, enum, pattern...), this does not perform a strict JSON
152154
// validation, which means the 'match' count may be higher than it should be.

src/main/java/com/datadog/api/client/v2/model/CIAppCreatePipelineEventRequestDataSingleOrArray.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ public CIAppCreatePipelineEventRequestDataSingleOrArray deserialize(
165165
}
166166
}
167167
if (attemptParsing) {
168-
tmp = tree.traverse(jp.getCodec()).readValueAs(List.class);
168+
// Using TypeReference for parameterized type List<CIAppCreatePipelineEventRequestData>
169+
// (Java 8 compatible)
170+
tmp =
171+
tree.traverse(jp.getCodec())
172+
.readValueAs(new TypeReference<List<CIAppCreatePipelineEventRequestData>>() {});
169173
// TODO: there is no validation against JSON schema constraints
170174
// (min, max, enum, pattern...), this does not perform a strict JSON
171175
// validation, which means the 'match' count may be higher than it should be.
Lines changed: 123 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,170 @@
11
package com.datadog.api.compatibility;
22

3+
import static org.junit.Assert.*;
4+
35
import com.fasterxml.jackson.core.type.TypeReference;
46
import com.fasterxml.jackson.databind.JsonNode;
57
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import org.junit.Test;
7-
88
import java.io.IOException;
99
import java.util.List;
10-
11-
import static org.junit.Assert.*;
10+
import org.junit.Test;
1211

1312
/**
14-
* CRITICAL TEST: This demonstrates the exact Java 8 compilation issue that PR #4191 would have caused.
13+
* CRITICAL TEST: This demonstrates the exact Java 8 compilation issue that PR #4191 would have
14+
* caused.
1515
*
16-
* This test contains BOTH the problematic pattern AND the fixed pattern to prove our solution works.
16+
* <p>This test contains BOTH the problematic pattern AND the fixed pattern to prove our solution
17+
* works.
1718
*/
1819
public class DiamondOperatorCompilationTest {
1920

20-
private final ObjectMapper objectMapper = new ObjectMapper();
21+
private final ObjectMapper objectMapper = new ObjectMapper();
22+
23+
@Test
24+
public void testJava8DiamondOperatorIssueReproduction() throws IOException {
25+
// This test reproduces the EXACT issue from PR #4191
26+
27+
String jsonArray = "[{\"email\": \"test@example.com\"}, {\"role\": \"viewer\"}]";
28+
JsonNode tree = objectMapper.readTree(jsonArray);
2129

22-
@Test
23-
public void testJava8DiamondOperatorIssueReproduction() throws IOException {
24-
// This test reproduces the EXACT issue from PR #4191
30+
// ❌ PROBLEMATIC PATTERN (would fail compilation in Java 8):
31+
// The following line would cause this compilation error:
32+
// "cannot infer type arguments for com.fasterxml.jackson.core.type.TypeReference<T>
33+
// reason: '<>' with anonymous inner classes is not supported in -source 8"
34+
//
35+
// Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
36+
// ^^
37+
// Diamond operator fails!
2538

26-
String jsonArray = "[{\"email\": \"test@example.com\"}, {\"role\": \"viewer\"}]";
27-
JsonNode tree = objectMapper.readTree(jsonArray);
39+
// ✅ FIXED PATTERN (works in Java 8):
40+
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Object>>() {});
2841

29-
// ❌ PROBLEMATIC PATTERN (would fail compilation in Java 8):
30-
// The following line would cause this compilation error:
31-
// "cannot infer type arguments for com.fasterxml.jackson.core.type.TypeReference<T>
32-
// reason: '<>' with anonymous inner classes is not supported in -source 8"
33-
//
34-
// Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
35-
// ^^
36-
// Diamond operator fails!
42+
assertNotNull(tmp);
43+
assertTrue("Should deserialize successfully with explicit type", tmp instanceof List);
3744

38-
// ✅ FIXED PATTERN (works in Java 8):
39-
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Object>>() {});
45+
@SuppressWarnings("unchecked")
46+
List<Object> result = (List<Object>) tmp;
47+
assertEquals("Should have 2 elements", 2, result.size());
48+
}
4049

41-
assertNotNull(tmp);
42-
assertTrue("Should deserialize successfully with explicit type", tmp instanceof List);
50+
@Test
51+
public void testTemplateFixSimulation() throws IOException {
52+
// This simulates what our template fix does:
53+
// Detects parameterized types and uses explicit TypeReference
4354

44-
@SuppressWarnings("unchecked")
45-
List<Object> result = (List<Object>) tmp;
46-
assertEquals("Should have 2 elements", 2, result.size());
47-
}
55+
String jsonData = "[1.5, 2.0, 3.7]"; // Numbers (like DistributionPointItem)
56+
JsonNode tree = objectMapper.readTree(jsonData);
4857

49-
@Test
50-
public void testTemplateFixSimulation() throws IOException {
51-
// This simulates what our template fix does:
52-
// Detects parameterized types and uses explicit TypeReference
58+
// Template logic simulation:
59+
String dataType = "List<Double>";
60+
String unParameterizedDataType = "Double"; // This would be different, so use TypeReference
5361

54-
String jsonData = "[1.5, 2.0, 3.7]"; // Numbers (like DistributionPointItem)
55-
JsonNode tree = objectMapper.readTree(jsonData);
62+
boolean isParameterized = !dataType.equals(unParameterizedDataType);
63+
assertTrue("Template should detect this as parameterized", isParameterized);
5664

57-
// Template logic simulation:
58-
String dataType = "List<Double>";
59-
String unParameterizedDataType = "Double"; // This would be different, so use TypeReference
65+
// Because it's parameterized, template generates this (FIXED pattern):
66+
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Double>>() {});
67+
// Instead of this (BROKEN pattern that would fail):
68+
// Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
6069

61-
boolean isParameterized = !dataType.equals(unParameterizedDataType);
62-
assertTrue("Template should detect this as parameterized", isParameterized);
70+
assertNotNull(tmp);
71+
assertTrue(tmp instanceof List);
6372

64-
// Because it's parameterized, template generates this (FIXED pattern):
65-
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Double>>() {});
66-
// Instead of this (BROKEN pattern that would fail):
67-
// Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<>() {});
73+
@SuppressWarnings("unchecked")
74+
List<Double> result = (List<Double>) tmp;
75+
assertEquals(3, result.size());
76+
assertEquals(1.5, result.get(0), 0.001);
77+
}
6878

69-
assertNotNull(tmp);
70-
assertTrue(tmp instanceof List);
79+
@Test
80+
public void testOriginalSharedDashboardInvitesDataScenario() throws IOException {
81+
// This recreates the exact failing scenario from SharedDashboardInvitesData.java:138
7182

72-
@SuppressWarnings("unchecked")
73-
List<Double> result = (List<Double>) tmp;
74-
assertEquals(3, result.size());
75-
assertEquals(1.5, result.get(0), 0.001);
76-
}
83+
String jsonInvites = "[{\"email\":\"user@example.com\",\"role\":\"viewer\"}]";
84+
JsonNode tree = objectMapper.readTree(jsonInvites);
7785

78-
@Test
79-
public void testOriginalSharedDashboardInvitesDataScenario() throws IOException {
80-
// This recreates the exact failing scenario from SharedDashboardInvitesData.java:138
86+
// BEFORE our fix (would cause Java 8 compilation failure):
87+
// tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
8188

82-
String jsonInvites = "[{\"email\":\"user@example.com\",\"role\":\"viewer\"}]";
83-
JsonNode tree = objectMapper.readTree(jsonInvites);
89+
// AFTER our fix (Java 8 compatible):
90+
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Object>>() {});
8491

85-
// BEFORE our fix (would cause Java 8 compilation failure):
86-
// tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
92+
assertNotNull("Should deserialize invite data", tmp);
93+
assertTrue("Should be a List", tmp instanceof List);
8794

88-
// AFTER our fix (Java 8 compatible):
89-
Object tmp = tree.traverse(objectMapper)
90-
.readValueAs(new TypeReference<List<Object>>() {});
95+
@SuppressWarnings("unchecked")
96+
List<Object> invites = (List<Object>) tmp;
97+
assertEquals("Should have one invite", 1, invites.size());
98+
}
9199

92-
assertNotNull("Should deserialize invite data", tmp);
93-
assertTrue("Should be a List", tmp instanceof List);
100+
@Test
101+
public void testOriginalDistributionPointItemScenario() throws IOException {
102+
// This recreates the exact failing scenario from DistributionPointItem.java:132
94103

95-
@SuppressWarnings("unchecked")
96-
List<Object> invites = (List<Object>) tmp;
97-
assertEquals("Should have one invite", 1, invites.size());
98-
}
104+
String jsonPoints = "[1.0, 2.5, 3.14]"; // Distribution points as numbers
105+
JsonNode tree = objectMapper.readTree(jsonPoints);
99106

100-
@Test
101-
public void testOriginalDistributionPointItemScenario() throws IOException {
102-
// This recreates the exact failing scenario from DistributionPointItem.java:132
107+
// BEFORE our fix (would cause Java 8 compilation failure):
108+
// tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
103109

104-
String jsonPoints = "[1.0, 2.5, 3.14]"; // Distribution points as numbers
105-
JsonNode tree = objectMapper.readTree(jsonPoints);
110+
// AFTER our fix (Java 8 compatible):
111+
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Double>>() {});
106112

107-
// BEFORE our fix (would cause Java 8 compilation failure):
108-
// tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
113+
assertNotNull("Should deserialize distribution points", tmp);
114+
assertTrue("Should be a List", tmp instanceof List);
109115

110-
// AFTER our fix (Java 8 compatible):
111-
Object tmp = tree.traverse(objectMapper)
112-
.readValueAs(new TypeReference<List<Double>>() {});
116+
@SuppressWarnings("unchecked")
117+
List<Double> points = (List<Double>) tmp;
118+
assertEquals("Should have 3 points", 3, points.size());
119+
assertEquals("First point should be 1.0", 1.0, points.get(0), 0.001);
120+
}
113121

114-
assertNotNull("Should deserialize distribution points", tmp);
115-
assertTrue("Should be a List", tmp instanceof List);
122+
@Test
123+
public void testCustomAttributeValuesUnionScenario() throws IOException {
124+
// This recreates what CustomAttributeValuesUnion.java:133 would have been
116125

117-
@SuppressWarnings("unchecked")
118-
List<Double> points = (List<Double>) tmp;
119-
assertEquals("Should have 3 points", 3, points.size());
120-
assertEquals("First point should be 1.0", 1.0, points.get(0), 0.001);
121-
}
122-
123-
@Test
124-
public void testCustomAttributeValuesUnionScenario() throws IOException {
125-
// This recreates what CustomAttributeValuesUnion.java:133 would have been
126-
127-
String jsonValues = "[\"string_value\", 42, {\"object\": \"value\"}]"; // Mixed values
128-
JsonNode tree = objectMapper.readTree(jsonValues);
129-
130-
// BEFORE our fix (would cause Java 8 compilation failure):
131-
// tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
126+
String jsonValues = "[\"string_value\", 42, {\"object\": \"value\"}]"; // Mixed values
127+
JsonNode tree = objectMapper.readTree(jsonValues);
132128

133-
// AFTER our fix (Java 8 compatible):
134-
Object tmp = tree.traverse(objectMapper)
135-
.readValueAs(new TypeReference<List<Object>>() {});
129+
// BEFORE our fix (would cause Java 8 compilation failure):
130+
// tmp = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<>() {});
131+
132+
// AFTER our fix (Java 8 compatible):
133+
Object tmp = tree.traverse(objectMapper).readValueAs(new TypeReference<List<Object>>() {});
136134

137-
assertNotNull("Should deserialize custom attribute values", tmp);
138-
assertTrue("Should be a List", tmp instanceof List);
135+
assertNotNull("Should deserialize custom attribute values", tmp);
136+
assertTrue("Should be a List", tmp instanceof List);
139137

140-
@SuppressWarnings("unchecked")
141-
List<Object> values = (List<Object>) tmp;
142-
assertEquals("Should have 3 values", 3, values.size());
143-
}
138+
@SuppressWarnings("unchecked")
139+
List<Object> values = (List<Object>) tmp;
140+
assertEquals("Should have 3 values", 3, values.size());
141+
}
144142

145-
@Test
146-
public void testCompilationDifferenceDocumentation() {
147-
// This test documents the exact difference between failing and working code
143+
@Test
144+
public void testCompilationDifferenceDocumentation() {
145+
// This test documents the exact difference between failing and working code
148146

149-
// ❌ FAILS in Java 8 (diamond operator with anonymous inner class):
150-
String failingPattern = "new TypeReference<>() {}";
147+
// ❌ FAILS in Java 8 (diamond operator with anonymous inner class):
148+
String failingPattern = "new TypeReference<>() {}";
151149

152-
// ✅ WORKS in Java 8 (explicit type parameters):
153-
String workingPattern1 = "new TypeReference<List<String>>() {}";
154-
String workingPattern2 = "new TypeReference<List<Object>>() {}";
155-
String workingPattern3 = "new TypeReference<List<Double>>() {}";
150+
// ✅ WORKS in Java 8 (explicit type parameters):
151+
String workingPattern1 = "new TypeReference<List<String>>() {}";
152+
String workingPattern2 = "new TypeReference<List<Object>>() {}";
153+
String workingPattern3 = "new TypeReference<List<Double>>() {}";
156154

157-
// Verify the patterns
158-
assertTrue("Failing pattern uses diamond operator", failingPattern.contains("<>"));
155+
// Verify the patterns
156+
assertTrue("Failing pattern uses diamond operator", failingPattern.contains("<>"));
159157

160-
assertFalse("Working pattern 1 should not use diamond operator", workingPattern1.contains("<>"));
161-
assertFalse("Working pattern 2 should not use diamond operator", workingPattern2.contains("<>"));
162-
assertFalse("Working pattern 3 should not use diamond operator", workingPattern3.contains("<>"));
158+
assertFalse(
159+
"Working pattern 1 should not use diamond operator", workingPattern1.contains("<>"));
160+
assertFalse(
161+
"Working pattern 2 should not use diamond operator", workingPattern2.contains("<>"));
162+
assertFalse(
163+
"Working pattern 3 should not use diamond operator", workingPattern3.contains("<>"));
163164

164-
// All working patterns have explicit types
165-
assertTrue("Pattern 1 has explicit type", workingPattern1.contains("List<String>"));
166-
assertTrue("Pattern 2 has explicit type", workingPattern2.contains("List<Object>"));
167-
assertTrue("Pattern 3 has explicit type", workingPattern3.contains("List<Double>"));
168-
}
169-
}
165+
// All working patterns have explicit types
166+
assertTrue("Pattern 1 has explicit type", workingPattern1.contains("List<String>"));
167+
assertTrue("Pattern 2 has explicit type", workingPattern2.contains("List<Object>"));
168+
assertTrue("Pattern 3 has explicit type", workingPattern3.contains("List<Double>"));
169+
}
170+
}

0 commit comments

Comments
 (0)