@@ -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