@@ -266,8 +266,8 @@ public MetadataAPISchemaResponse parseSchemaResponse(InputStream responseStream)
266266 * @throws ServiceNowAPIException
267267 */
268268 public Schema fetchTableSchema (String tableName , SourceValueType valueType )
269- throws ServiceNowAPIException {
270- return fetchTableSchema (tableName , getAccessToken (), valueType , schemaType , false );
269+ throws ServiceNowAPIException {
270+ return fetchTableSchema (tableName , getAccessToken (), valueType , schemaType , true );
271271 }
272272
273273 private SchemaType getSchemaTypeBasedOnUseConnection (Boolean useConnection ) {
@@ -291,7 +291,7 @@ private SchemaType getSchemaTypeBasedOnUseConnection(Boolean useConnection) {
291291 */
292292 public Schema fetchTableSchema (String tableName , String accessToken , SourceValueType valueType ,
293293 SchemaType schemaType , Boolean legacyMapping )
294- throws ServiceNowAPIException {
294+ throws ServiceNowAPIException {
295295 ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder (
296296 this .conf .getRestApiEndpoint (), tableName , true , schemaType )
297297 .setExcludeReferenceLink (true );
@@ -301,12 +301,16 @@ public Schema fetchTableSchema(String tableName, String accessToken, SourceValue
301301 restAPIResponse = executeGetWithRetries (requestBuilder .build ());
302302 List <ServiceNowColumn > columns = new ArrayList <>();
303303
304- if (schemaType == SchemaType .METADATA_API_BASED ) {
305- return prepareSchemaWithMetadataAPI (restAPIResponse , columns , tableName , valueType , legacyMapping );
306- } else if (schemaType == SchemaType .SCHEMA_API_BASED ) {
307- return prepareSchemaWithSchemaAPI (restAPIResponse , columns , tableName );
308- } else {
309- return prepareStringBasedSchema (restAPIResponse , columns , tableName );
304+ try {
305+ if (schemaType == SchemaType .METADATA_API_BASED ) {
306+ return prepareSchemaWithMetadataAPI (restAPIResponse , columns , tableName , valueType , legacyMapping );
307+ } else if (schemaType == SchemaType .SCHEMA_API_BASED ) {
308+ return prepareSchemaWithSchemaAPI (restAPIResponse , columns , tableName , legacyMapping );
309+ } else {
310+ return prepareStringBasedSchema (restAPIResponse , columns , tableName , legacyMapping );
311+ }
312+ } catch (IOException exception ) {
313+ throw new RuntimeException ("Error in fetching schema for table " + tableName , exception );
310314 }
311315 }
312316
@@ -327,7 +331,7 @@ public Schema fetchTableSchema(String tableName, String accessToken, SourceValue
327331 * @throws RuntimeException if the schema response is null or contains no result.
328332 */
329333 private Schema prepareSchemaWithSchemaAPI (RestAPIResponse restAPIResponse , List <ServiceNowColumn > columns ,
330- String tableName ) throws ServiceNowAPIException {
334+ String tableName , Boolean legacyMapping ) throws ServiceNowAPIException , IOException {
331335 SchemaAPISchemaResponse schemaAPISchemaResponse =
332336 GSON .fromJson (createJsonReader (restAPIResponse .getResponseStream ()), SchemaAPISchemaResponse .class );
333337
@@ -339,7 +343,7 @@ private Schema prepareSchemaWithSchemaAPI(RestAPIResponse restAPIResponse, List<
339343 for (SchemaAPISchemaField field : schemaAPISchemaResponse .getResult ()) {
340344 columns .add (new ServiceNowColumn (field .getName (), field .getInternalType ()));
341345 }
342- return SchemaBuilder .constructSchema (tableName , columns , false );
346+ return SchemaBuilder .constructSchema (tableName , columns , legacyMapping );
343347 }
344348
345349 /**
@@ -361,7 +365,7 @@ private Schema prepareSchemaWithSchemaAPI(RestAPIResponse restAPIResponse, List<
361365 * @throws RuntimeException if the response does not contain valid column information.
362366 */
363367 private Schema prepareSchemaWithMetadataAPI (RestAPIResponse restAPIResponse , List <ServiceNowColumn > columns ,
364- String tableName , SourceValueType valueType , Boolean legacyMapping ) throws ServiceNowAPIException {
368+ String tableName , SourceValueType valueType , Boolean legacyMapping ) throws ServiceNowAPIException , IOException {
365369 MetadataAPISchemaResponse metadataAPISchemaResponse = parseSchemaResponse (restAPIResponse .getResponseStream ());
366370
367371 if (metadataAPISchemaResponse .getResult () == null || metadataAPISchemaResponse .getResult ().getColumns () == null ||
@@ -492,7 +496,7 @@ public String createRecordInDisplayMode(String tableName, HttpEntity entity) thr
492496 return systemID ;
493497 }
494498
495- private String getSystemId (RestAPIResponse restAPIResponse ) {
499+ private String getSystemId (RestAPIResponse restAPIResponse ) throws IOException {
496500 CreateRecordAPIResponse apiResponse = GSON .fromJson (
497501 new InputStreamReader (restAPIResponse .getResponseStream (), StandardCharsets .UTF_8 ),
498502 CreateRecordAPIResponse .class );
@@ -507,7 +511,7 @@ private String getSystemId(RestAPIResponse restAPIResponse) {
507511 * @param query The query
508512 */
509513 public JsonObject getRecordFromServiceNowTable (String tableName , String query )
510- throws ServiceNowAPIException {
514+ throws ServiceNowAPIException , IOException {
511515
512516 ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder (
513517 this .conf .getRestApiEndpoint (), tableName , false , schemaType )
@@ -537,7 +541,7 @@ public JsonObject getRecordFromServiceNowTable(String tableName, String query)
537541 * @throws RuntimeException if the schema response is null or contains no result.
538542 */
539543 private Schema prepareStringBasedSchema (RestAPIResponse restAPIResponse , List <ServiceNowColumn > columns ,
540- String tableName ) {
544+ String tableName , Boolean legacyMapping ) throws IOException {
541545 InputStream in = restAPIResponse .getResponseStream ();
542546 JsonReader reader = new JsonReader (new InputStreamReader (in , StandardCharsets .UTF_8 ));
543547 JsonObject firstRecord ;
@@ -551,7 +555,7 @@ private Schema prepareStringBasedSchema(RestAPIResponse restAPIResponse, List<Se
551555 firstRecord .entrySet ().forEach (entry ->
552556 columns .add (new ServiceNowColumn (entry .getKey (), "string" ))
553557 );
554- return SchemaBuilder .constructSchema (tableName , columns , false );
558+ return SchemaBuilder .constructSchema (tableName , columns , legacyMapping );
555559 }
556560 return null ;
557561 }
0 commit comments