Skip to content

Commit bfd2e00

Browse files
committed
Fix a few issues on the JSON file generator
1 parent 087e99d commit bfd2e00

File tree

4 files changed

+246
-125
lines changed

4 files changed

+246
-125
lines changed

slack-api-client/src/test/java/test_with_remote_apis/methods/chat_Test.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ public void postMessage_bot() throws Exception {
157157
loadRandomChannelId();
158158
ChatPostMessageResponse response = slack.methods(botToken).chatPostMessage(req -> req
159159
.channel(randomChannelId)
160-
.text("You can also do slack.methods(token)"));
160+
.text("You can also do slack.methods(token)")
161+
.blocks(asBlocks(section(s -> s.text(plainText("You can also do slack.methods(token)")))))
162+
);
161163
assertThat(response.getError(), is(nullValue()));
162164
assertThat(response.getMessage().getText(), is("You can also do slack.methods(token)"));
163165

slack-api-client/src/test/java/util/ObjectInitializer.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package util;
22

3+
import com.slack.api.model.block.composition.OptionGroupObject;
4+
import com.slack.api.model.block.composition.OptionObject;
35
import lombok.extern.slf4j.Slf4j;
46

57
import java.lang.reflect.Constructor;
68
import java.lang.reflect.Field;
79
import java.lang.reflect.Modifier;
10+
import java.util.ArrayList;
811
import java.util.List;
912

13+
import static com.slack.api.model.block.composition.BlockCompositions.asOptions;
14+
import static com.slack.api.model.block.composition.BlockCompositions.plainText;
15+
1016
@Slf4j
1117
public class ObjectInitializer {
1218

1319
private ObjectInitializer() {
1420
}
1521

1622
public static <T> T initProperties(T obj) {
23+
return initProperties(obj, false);
24+
}
25+
26+
public static <T> T initProperties(T obj, boolean attachments) {
1727
Field currentField = null;
1828
try {
1929
for (Field field : obj.getClass().getDeclaredFields()) {
@@ -38,12 +48,38 @@ public static <T> T initProperties(T obj) {
3848
field.set(obj, false);
3949
} else if (type.equals(List.class)) {
4050
List listObj = (List) field.get(obj);
41-
if (listObj != null) {
51+
if (listObj != null && listObj.size() > 0) {
4252
for (Object o : listObj) {
4353
initProperties(o);
4454
}
4555
} else {
46-
log.info("null List object detected {} in {}", field.getName(), obj.getClass().getSimpleName());
56+
if (listObj == null) {
57+
listObj = new ArrayList();
58+
field.set(obj, listObj);
59+
}
60+
if (attachments) {
61+
log.info("null List object detected {} in {}", field.getName(), obj.getClass().getSimpleName());
62+
} else {
63+
if (field.getName().equals("options")) {
64+
listObj.add(initProperties(OptionObject.builder()
65+
.text(plainText(""))
66+
.description(plainText(""))
67+
.build()
68+
));
69+
} else if (field.getName().equals("optionGroups")) {
70+
listObj.add(initProperties(OptionGroupObject.builder()
71+
.label(plainText(""))
72+
.options(asOptions(initProperties(OptionObject.builder()
73+
.text(plainText(""))
74+
.description(plainText(""))
75+
.build()
76+
)))
77+
.build()
78+
));
79+
} else {
80+
log.info("null List object detected {} in {}", field.getName(), obj.getClass().getSimpleName());
81+
}
82+
}
4783
}
4884
} else {
4985
if (field.get(obj) != null) {

slack-api-client/src/test/java/util/sample_json_generation/JsonDataRecorder.java

Lines changed: 96 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,22 @@ public JsonDataRecorder(SlackConfig config, String outputDirectory) {
4242
}
4343

4444
public void writeMergedResponse(Response response, String body) throws IOException {
45-
String path = response.request().url().url().getPath();
46-
String httpMethod = response.request().method();
47-
if (path.startsWith("/scim")) {
48-
if (httpMethod.toUpperCase(Locale.ENGLISH).equals("GET")) {
45+
try {
46+
String path = response.request().url().url().getPath();
47+
String httpMethod = response.request().method();
48+
if (path.startsWith("/scim")) {
49+
if (httpMethod.toUpperCase(Locale.ENGLISH).equals("GET")) {
50+
writeMergedJsonData(path, body);
51+
}
52+
} else if (path.equals("/audit/v1/logs")) {
53+
// As generating logs.json is not so easy,
54+
// test_with_remote_apis.audit.ApiTest generates the file
55+
} else {
4956
writeMergedJsonData(path, body);
5057
}
51-
} else if (path.equals("/audit/v1/logs")) {
52-
// As generating logs.json is not so easy,
53-
// test_with_remote_apis.audit.ApiTest generates the file
54-
} else {
55-
writeMergedJsonData(path, body);
58+
} catch (Throwable e) {
59+
log.error("Failed to write merged response data: {}", body, e);
60+
throw e;
5661
}
5762
}
5863

@@ -101,86 +106,92 @@ public void writeMergedJsonData(String path, String body) throws IOException {
101106
// Update the existing raw data
102107
Path rawFilePath = new File(toRawFilePath(path)).toPath();
103108
Files.createDirectories(rawFilePath.getParent());
104-
Files.write(rawFilePath, gson().toJson(existingRawJsonElem).getBytes(UTF_8));
109+
if (rawJsonObj != null) {
110+
Files.write(rawFilePath, gson().toJson(rawJsonObj).getBytes(UTF_8));
111+
} else {
112+
Files.write(rawFilePath, gson().toJson(existingRawJsonElem).getBytes(UTF_8));
113+
}
105114

106-
if (existingRawJsonElem != null
107-
&& existingRawJsonElem.isJsonObject()
108-
&& rawJsonObj != null) {
109-
// Normalize the property values in the JSON data
110-
for (Map.Entry<String, JsonElement> entry : rawJsonObj.entrySet()) {
111-
scanToNormalizeValues(path, rawJsonObj, entry.getKey(), entry.getValue());
112-
}
113-
// Merge the raw data into the existing masked (sample) data
114-
String existingSampleJson = null;
115-
try {
116-
Path jsonFilePath = new File(toMaskedFilePath(path)).toPath();
117-
existingSampleJson = Files.readAllLines(jsonFilePath, UTF_8).stream().collect(Collectors.joining());
118-
} catch (NoSuchFileException e) {
119-
}
120-
if (existingSampleJson == null) {
121-
existingSampleJson = "{}";
122-
}
123-
JsonObject mergedJsonObj = rawJsonObj;
124-
if (existingSampleJson.trim().isEmpty()) {
115+
if (existingRawJsonElem != null) {
116+
if (existingRawJsonElem.isJsonObject() && rawJsonObj != null) {
117+
// Normalize the property values in the JSON data
118+
for (Map.Entry<String, JsonElement> entry : rawJsonObj.entrySet()) {
119+
scanToNormalizeValues(path, rawJsonObj, entry.getKey(), entry.getValue());
120+
}
121+
// Merge the raw data into the existing masked (sample) data
122+
String existingSampleJson = buildSampleJSONString(path);
125123
JsonElement existingSampleJsonElem = JsonParser.parseString(existingSampleJson);
126-
JsonObject sampleJsonObj = existingSampleJsonElem.isJsonObject() ? existingSampleJsonElem.getAsJsonObject() : null;
127-
if (sampleJsonObj != null && sampleJsonObj.isJsonObject()) {
128-
mergedJsonObj = sampleJsonObj;
129-
try {
130-
MergeJsonBuilder.mergeJsonObjects(mergedJsonObj, MergeJsonBuilder.ConflictStrategy.PREFER_FIRST_OBJ, rawJsonObj);
131-
} catch (MergeJsonBuilder.JsonConflictException e) {
132-
log.warn("Failed to merge JSON objects because {}", e.getMessage(), e);
133-
}
124+
JsonObject mergedJsonObj = existingSampleJsonElem.isJsonObject() ?
125+
existingSampleJsonElem.getAsJsonObject() : new JsonObject();
126+
try {
127+
MergeJsonBuilder.mergeJsonObjects(mergedJsonObj, MergeJsonBuilder.ConflictStrategy.PREFER_FIRST_OBJ, rawJsonObj);
128+
} catch (MergeJsonBuilder.JsonConflictException e) {
129+
log.warn("Failed to merge JSON objects because {}", e.getMessage(), e);
130+
}
131+
// Normalize the merged data (especially for cleaning up array data in nested JSON data)
132+
for (Map.Entry<String, JsonElement> entry : mergedJsonObj.entrySet()) {
133+
scanToNormalizeValues(path, mergedJsonObj, entry.getKey(), entry.getValue());
134134
}
135-
}
136-
// Normalize the merged data (especially for cleaning up array data in nested JSON data)
137-
for (Map.Entry<String, JsonElement> entry : mergedJsonObj.entrySet()) {
138-
scanToNormalizeValues(path, mergedJsonObj, entry.getKey(), entry.getValue());
139-
}
140135

141-
if (path.startsWith("/scim")) {
142-
// Manually build complete objects for SCIM API response
143-
if (mergedJsonObj.get("Resources") != null) {
144-
for (JsonElement resource : mergedJsonObj.get("Resources").getAsJsonArray()) {
145-
JsonObject resourceObj = resource.getAsJsonObject();
146-
if (resourceObj.get("userName") != null) {
147-
initializeSCIMUser(resourceObj);
148-
}
149-
if (resourceObj.get("members") != null) {
150-
initializeSCIMGroup(resourceObj);
151-
}
152-
}
136+
if (path.startsWith("/scim")) {
137+
writeSCIMResponseData(path, mergedJsonObj);
153138
} else {
154-
if (mergedJsonObj.get("userName") != null) {
155-
initializeSCIMUser(mergedJsonObj);
156-
}
157-
if (mergedJsonObj.get("members") != null) {
158-
initializeSCIMGroup(mergedJsonObj);
139+
if (!path.startsWith("/status")) {
140+
// ok, error etc. do not exist in the Status (Current) API response
141+
addCommonPropertiesAtTopLevel(mergedJsonObj);
159142
}
143+
// Write the masked (sample) JSON data
144+
Path filePath = new File(toMaskedFilePath(path)).toPath();
145+
Files.createDirectories(filePath.getParent());
146+
Files.write(filePath, gson().toJson(mergedJsonObj).getBytes(UTF_8));
160147
}
161-
Path filePath = new File(toMaskedFilePath(path).replaceFirst("/\\w{9}.json$", "/000000000.json")).toPath();
148+
} else if (existingRawJsonElem.isJsonArray()) {
149+
// The Status History API
150+
JsonArray jsonArray = existingRawJsonElem.getAsJsonArray();
151+
scanToNormalizeValues(path, null, null, jsonArray);
152+
Path filePath = new File(toMaskedFilePath(path)).toPath();
162153
Files.createDirectories(filePath.getParent());
163-
Files.write(filePath, gson().toJson(mergedJsonObj).getBytes(UTF_8));
154+
Files.write(filePath, gson().toJson(jsonArray).getBytes(UTF_8));
155+
}
156+
}
157+
}
164158

165-
} else {
166-
if (!path.startsWith("/status")) {
167-
// ok, error etc. do not exist in the Status (Current) API response
168-
addCommonPropertiesAtTopLevel(mergedJsonObj);
169-
}
159+
private String buildSampleJSONString(String path) throws IOException {
160+
String existingSampleJson = null;
161+
try {
162+
Path jsonFilePath = new File(toMaskedFilePath(path)).toPath();
163+
existingSampleJson = Files.readAllLines(jsonFilePath, UTF_8).stream().collect(Collectors.joining());
164+
} catch (NoSuchFileException e) {
165+
}
166+
if (existingSampleJson == null) {
167+
existingSampleJson = "{}";
168+
}
169+
return existingSampleJson;
170+
}
170171

171-
// Write the masked (sample) JSON data
172-
Path filePath = new File(toMaskedFilePath(path)).toPath();
173-
Files.createDirectories(filePath.getParent());
174-
Files.write(filePath, gson().toJson(mergedJsonObj).getBytes(UTF_8));
172+
private void writeSCIMResponseData(String path, JsonObject mergedJsonObj) throws IOException {
173+
// Manually build complete objects for SCIM API response
174+
if (mergedJsonObj.get("Resources") != null) {
175+
for (JsonElement resource : mergedJsonObj.get("Resources").getAsJsonArray()) {
176+
JsonObject resourceObj = resource.getAsJsonObject();
177+
if (resourceObj.get("userName") != null) {
178+
initializeSCIMUser(resourceObj);
179+
}
180+
if (resourceObj.get("members") != null) {
181+
initializeSCIMGroup(resourceObj);
182+
}
183+
}
184+
} else {
185+
if (mergedJsonObj.get("userName") != null) {
186+
initializeSCIMUser(mergedJsonObj);
187+
}
188+
if (mergedJsonObj.get("members") != null) {
189+
initializeSCIMGroup(mergedJsonObj);
175190
}
176-
} else if (existingRawJsonElem.isJsonArray()) {
177-
// The Status History API
178-
JsonArray jsonArray = existingRawJsonElem.getAsJsonArray();
179-
scanToNormalizeValues(path, null, null, jsonArray);
180-
Path filePath = new File(toMaskedFilePath(path)).toPath();
181-
Files.createDirectories(filePath.getParent());
182-
Files.write(filePath, gson().toJson(jsonArray).getBytes(UTF_8));
183191
}
192+
Path filePath = new File(toMaskedFilePath(path).replaceFirst("/\\w{9}.json$", "/000000000.json")).toPath();
193+
Files.createDirectories(filePath.getParent());
194+
Files.write(filePath, gson().toJson(mergedJsonObj).getBytes(UTF_8));
184195
}
185196

186197
private void initializeSCIMGroup(JsonObject resourceObj) {
@@ -315,8 +326,8 @@ private void scanToNormalizeValues(String path, JsonElement parent, String name,
315326
for (int idx = 0; idx < array.size(); idx++) {
316327
array.remove(idx);
317328
}
318-
com.slack.api.model.File f = ObjectInitializer.initProperties(new com.slack.api.model.File());
319-
f.setHeaders(ObjectInitializer.initProperties(new com.slack.api.model.File.Headers()));
329+
com.slack.api.model.File f = SampleObjects.initFileObject();
330+
f.setAttachments(null); // Trying to load data for this field can result in StackOverFlowError
320331
array.add(gson.toJsonTree(f));
321332
} else if (name != null && name.equals("status_emoji_display_info")) {
322333
for (int idx = 0; idx < array.size(); idx++) {
@@ -403,16 +414,19 @@ private void scanToNormalizeValues(String path, JsonElement parent, String name,
403414
}
404415
} else if (element.isJsonObject()) {
405416
if (name != null && name.equals("file")) {
417+
if (path.startsWith("/audit/v1/schemas") || path.startsWith("/audit/v1/actions")) {
418+
return;
419+
}
406420
try {
407421
JsonObject file = element.getAsJsonObject();
408422
// To avoid concurrent modification of the underlying objects
409423
List<String> oldKeys = new ArrayList<>(file.keySet());
410424
for (String key : oldKeys) {
411425
file.remove(key);
412426
}
413-
com.slack.api.model.File f = ObjectInitializer.initProperties(new com.slack.api.model.File());
414-
f.setHeaders(ObjectInitializer.initProperties(new com.slack.api.model.File.Headers()));
415-
JsonObject fullFile = GsonFactory.createSnakeCase().toJsonTree(f).getAsJsonObject();
427+
JsonObject fullFile = GsonFactory.createSnakeCase()
428+
.toJsonTree(SampleObjects.FileObject)
429+
.getAsJsonObject();
416430
for (String newKey : fullFile.keySet()) {
417431
file.add(newKey, fullFile.get(newKey));
418432
}

0 commit comments

Comments
 (0)