2828import java .util .*;
2929
3030public final class DetectController extends VaultClient {
31- private static final Gson gson = new GsonBuilder ().serializeNulls ().create ();
31+ Gson gson = new GsonBuilder ()
32+ .registerTypeAdapter (Optional .class , (JsonSerializer <Optional <?>>) (src , typeOfSrc , context ) ->
33+ src .map (context ::serialize ).orElse (null ))
34+ .serializeNulls ()
35+ .create ();
3236
3337 public DetectController (VaultConfig vaultConfig , Credentials credentials ) {
3438 super (vaultConfig , credentials );
@@ -55,7 +59,7 @@ public DeidentifyTextResponse deidentifyText(DeidentifyTextRequest deidentifyTex
5559 deidentifyTextResponse = getDeIdentifyTextResponse (deidentifyStringResponse );
5660 LogUtil .printInfoLog (InfoLogs .DEIDENTIFY_TEXT_REQUEST_RESOLVED .getLog ());
5761 } catch (ApiClientApiException ex ) {
58- String bodyString = gson . toJson (ex .body ());
62+ String bodyString = extractBodyAsString (ex .body ());
5963 LogUtil .printErrorLog (ErrorLogs .DEIDENTIFY_TEXT_REQUEST_REJECTED .getLog ());
6064 throw new SkyflowException (ex .statusCode (), ex , ex .headers (), bodyString );
6165 }
@@ -82,7 +86,7 @@ public ReidentifyTextResponse reidentifyText(ReidentifyTextRequest reidentifyTex
8286 reidentifyTextResponse = new ReidentifyTextResponse (reidentifyStringResponse .getText ().orElse (null ));
8387 LogUtil .printInfoLog (InfoLogs .REIDENTIFY_TEXT_REQUEST_RESOLVED .getLog ());
8488 } catch (ApiClientApiException ex ) {
85- String bodyString = gson . toJson (ex .body ());
89+ String bodyString = extractBodyAsString (ex .body ());
8690 LogUtil .printErrorLog (ErrorLogs .REIDENTIFY_TEXT_REQUEST_REJECTED .getLog ());
8791 throw new SkyflowException (ex .statusCode (), ex , ex .headers (), bodyString );
8892 }
@@ -145,7 +149,7 @@ public DeidentifyFileResponse deidentifyFile(DeidentifyFileRequest request) thro
145149 }
146150 }
147151 } catch (ApiClientApiException e ) {
148- String bodyString = gson . toJson (e .body ());
152+ String bodyString = extractBodyAsString (e .body ());
149153 LogUtil .printErrorLog (ErrorLogs .DEIDENTIFY_FILE_REQUEST_REJECTED .getLog ());
150154 throw new SkyflowException (e .statusCode (), e , e .headers (), bodyString );
151155 }
@@ -287,6 +291,14 @@ private static synchronized List<FileEntityInfo> getEntities(DeidentifyStatusRes
287291 return entities ;
288292 }
289293
294+ private String extractBodyAsString (Object body ) {
295+ if (body instanceof String ) {
296+ return (String ) body ;
297+ } else {
298+ return gson .toJson (body );
299+ }
300+ }
301+
290302 private com .skyflow .generated .rest .types .DeidentifyFileResponse processFileByType (String fileExtension , String base64Content , DeidentifyFileRequest request , String vaultId ) throws SkyflowException {
291303 switch (fileExtension .toLowerCase ()) {
292304 case "txt" :
@@ -367,7 +379,7 @@ public DeidentifyFileResponse getDetectRun(GetDetectRunRequest request) throws S
367379
368380 return parseDeidentifyFileResponse (apiResponse , runId , apiResponse .getStatus ().toString ());
369381 } catch (ApiClientApiException e ) {
370- String bodyString = gson . toJson (e .body ());
382+ String bodyString = extractBodyAsString (e .body ());
371383 LogUtil .printErrorLog (ErrorLogs .GET_DETECT_RUN_REQUEST_REJECTED .getLog ());
372384 throw new SkyflowException (e .statusCode (), e , e .headers (), bodyString );
373385 }
0 commit comments