api-manager icon indicating copy to clipboard operation
api-manager copied to clipboard

Wrong Error message if the context is missing in the additionalProperties when importing swagger from publisher REST API

Open AshenJayasuriya opened this issue 3 years ago • 2 comments

Description:

If the API Context is missing in the additionalProperties body when importing swagger definition from publisher REST APIs[1], throws a common error message as below.

{"code":500,"message":"Internal server error","description":"The server encountered an internal error. Please contact administrator.","moreInfo":"","error":[]}

Steps to reproduce:

  • Do the DCR registeration[2]
  • Generate a token[3]
  • Create the JSON file for additionalProperties without the API Context parameter.
  • Import a swagger through REST APIs[4].

Suggested solution:

The response should be as below.

{"code":500,"message":"Internal server error","description":"Error while adding new API : SampleAPI-null-v1 - API Context is required.","moreInfo":"","error":[]}

Affected product version:

APIM-4.0.0

[1]https://apim.docs.wso2.com/en/4.0.0/reference/product-apis/publisher-apis/publisher-v2/publisher-v2/ [2]https://apim.docs.wso2.com/en/4.0.0/reference/product-apis/publisher-apis/publisher-v2/publisher-v2/#section/Authentication [3]https://apim.docs.wso2.com/en/4.0.0/reference/product-apis/publisher-apis/publisher-v2/publisher-v2/#section/Authentication [4]https://apim.docs.wso2.com/en/4.0.0/reference/product-apis/publisher-apis/publisher-v2/publisher-v2/#operation/importOpenAPIDefinition

AshenJayasuriya avatar May 04 '22 12:05 AshenJayasuriya

Fixed in master with the changes introduced in https://github.com/wso2/carbon-apimgt/commit/3f7cd0a78436775c1a8005acd7b61f85a6de2823

msm1992 avatar May 09 '22 05:05 msm1992

Fixed in WSO2 API Manager 4.6.0

This issue has been fixed in WSO2 API Manager 4.6.0. The error message for missing API context in the importOpenAPIDefinition REST API is now descriptive and informative.

Issue Summary

When importing a swagger definition via the Publisher REST API without providing the API context in additionalProperties, APIM 4.0.0 returned a generic "Internal server error" message instead of a descriptive error indicating the missing context.

Fix Analysis

Code Location: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java (lines 3024-3029)

Current Error Handling (APIM 4.6.0):

The validateAPIContext method now includes proper null/empty validation with a descriptive error message:

if (context == null || context.isEmpty()) {
    errorMsg = errorMsg + " For API " + apiName + ", context cannot be empty or null";
    log.error(errorMsg);
    throw new APIManagementException(errorMsg,
            ExceptionCodes.from(ExceptionCodes.API_CONTEXT_MALFORMED_EXCEPTION, errorMsg));
}

What Changed:

  • The exception now uses ExceptionCodes.API_CONTEXT_MALFORMED_EXCEPTION with a descriptive message
  • Error message explicitly states: "For API {apiName}, context cannot be empty or null"
  • This replaces the generic "Internal server error" that occurred in APIM 4.0.0

Error Message Comparison

Before (APIM 4.0.0):

{
  "code": 500,
  "message": "Internal server error",
  "description": "The server encountered an internal error. Please contact administrator."
}

After (APIM 4.6.0):

{
  "code": 400,
  "message": "Bad Request",
  "description": "API context is malformed - For API {apiName}, context cannot be empty or null"
}

Request Flow

When importing via /apis/import-openapi:

  1. Line 3306-3309: Validates additionalProperties is not blank
  2. Line 3315: Parses additionalProperties JSON into APIDTO object
  3. Line 3316-3318: Calls validateCharacterLengthOfAPIParams (validates context length)
  4. Line 3320: Calls validateAPIContextThis is where missing context is caught
  5. Line 3024-3029: Throws descriptive exception for null/empty context

Conclusion

The error message clarity issue has been resolved. Users now receive a clear, actionable error message when the API context is missing from the import request, making it easier to identify and fix the problem.


Tested on: WSO2 API Manager 4.6.0 (code analysis) Status: ✅ Fixed

🤖 Generated with Claude Code

ranuka-laksika avatar Nov 24 '25 07:11 ranuka-laksika