Skip to content

TypeCastError: 'error' cannot be cast to 'int' #3722

@Sxmindu

Description

@Sxmindu

Description

When invoking the https://localhost:9443/internal/data/v1/apis?context=<context>&version=<version> call with a non-existing context or mismatching context from the Microgateway, the APIM response with {"count":1,"list":[null],"pagination":null}.

And according to the implementation, after getting the response from APIM it try to iterate through the list array in the response, if the list contains any elements (if (list.length() > 0 )). And since there is null inside the list, the if condition becomes true as list array contains null and try to type cast the null into a int in the line id: <int>list[0].apiId resulting the error.

if (list.length() > 0 ) {
  Api api = {
      id: <int>list[0].apiId,
      provider: list[0].provider.toString(),
      name: list[0].name.toString(),
      apiVersion: list[0].'version.toString(),
      context: list[0].context.toString(),
      policyId: list[0].policy.toString()
  };
  string apiKey = api.name + ":" + api.apiVersion;
  self.apis[apiKey] = <@untainted>api;
  printDebug(KEY_API_STORE, "Returned API from service is : " + api.toString());
  return <@untainted>api;
}

Steps to Reproduce

Can reproduce the behaviour by calling https://localhost:9443/internal/data/v1/apis?context=<context>&version=<version> with non-existing context and version values, so that the response simulates the empty result scenario.

Version

3.2.9

Environment Details (with versions)

No response

Relevant Log Output

Error:

2025-09-01 12:32:14,806 DEBUG [wso2/gateway/src/gateway/utils] - [ApiDataStore] [4b428c69-d374-452b-af36-e0638b9a5c37] API list is : {"count":1, "list":[null], "pagination":null}
error: {ballerina}TypeCastError message=incompatible types: 'error' cannot be cast to 'int'
	at wso2.gateway.ApiDataStore:loadApiFromService(/src/gateway/apim/api_data_store.bal:80)
	   wso2.gateway.PilotDataProvider:loadApiFromService(/src/gateway/apim/pilot_data_provider.bal:72)
	   wso2.gateway.src.gateway.apim.pilot_utils:validateSubscriptionFromDataStores(/src/gateway/apim/pilot_utils.bal:106)
	   wso2.gateway.src.gateway.utils.jwt_util:isAllowedKey(/src/gateway/utils/jwt_util.bal:69)
	   wso2.gateway.src.gateway.handler_providers.jwt_auth_provider:validateSubscriptions(/src/gateway/handler_providers/jwt_auth_provider.bal:176)
	   wso2.gateway.JwtAuthProvider:authenticate(/src/gateway/handler_providers/jwt_auth_provider.bal:138)
	   wso2.gateway.JWTAuthHandler:process(/src/gateway/handlers/jwt_auth_handler.bal:86)
	   ballerina.http.auth.authn_filter:checkForAuthHandlers(auth/authn_filter.bal:80)
	   ballerina.http.auth.authn_filter:handleAuthRequest(auth/authn_filter.bal:62)
	   ballerina.http.AuthnFilter:filterRequest(auth/authn_filter.bal:43)
	   wso2.test__service_0:get686afa485f00470f84462fb454ea221c(Customer_hyphen_Service_hyphen_API_hyphen_V1_dot_1__1_1_0.bal:347)

Suggested Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions