Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response deserialization failure #143

Open
ilsid opened this issue Jan 31, 2024 · 2 comments
Open

Response deserialization failure #143

ilsid opened this issue Jan 31, 2024 · 2 comments

Comments

@ilsid
Copy link

ilsid commented Jan 31, 2024

Hello team,

Looks like the Content-Type header in a server response has been changed recently and is not supported by Java client
I'm getting now the following error when invoking Api.PaymentsApi.createPayment(CreatePaymentRequest). It used to work fine before.

Content type "application/hal+json,application/hal+json;charset=utf-8" is not supported for type: class Model.PtsV2PaymentsPost201Response

Please see
https://github.com/CyberSource/cybersource-rest-client-java/blob/master/src/main/java/Invokers/ApiClient.java#L1064
https://github.com/CyberSource/cybersource-rest-client-java/blob/master/src/main/java/Invokers/ApiClient.java#L953

Tried versions 0.0.58 and 0.0.60

Thanks

@ilsid
Copy link
Author

ilsid commented Feb 1, 2024

For a quick problem resolution, I made a small patch in my code using Javassist library

ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get("Invokers.ApiClient");
CtMethod ctMethod = ctClass.getDeclaredMethod("isJsonMime");

// This is a small change for the jsonMime regexp to accept "application/hal+json,application/hal+json;charset=utf-8"
// Content-Type response header. Actually, a server should return
// "application/hal+json;charset=utf-8" header value (no duplication). In this case, no fixes are needed.
ctMethod.setBody("{ " +
                    "String jsonMime = \"(?i)^(application/json|[^;/ \\t]+/[^;/ \\t]+[+]json)[ \\t]*(.*)?$\"; " +
                    "return $1 != null && ($1.matches(jsonMime) || $1.equalsIgnoreCase(\"application/json-patch+json\")); " +
                 "}");

ctClass.toClass();

@deepgahlot11
Copy link

I am facing similar issue with 0.0.73 and fixed it by creating a CustomApiClient in which i override isJsonMime(). Now In place of ApiClient use CustomApiClient.

public class CustomApiClient extends ApiClient {

    public CustomApiClient(MerchantConfig merchantConfig) {
        super(merchantConfig);
    }

    @Override
    public boolean isJsonMime(String mime) {
        if (mime == null) {
            return false;
        }

        // Enhanced pattern to handle various JSON content types
        String jsonMimePattern = "(?i)^(application/json|[^;/ \\t]+/[^;/ \\t]+[+]json)[ \\t]*(.*)?$";
        return mime.matches(jsonMimePattern) ||
                mime.equalsIgnoreCase("application/json-patch+json") ||
                mime.startsWith("application/json") ||
                mime.startsWith("application/hal+json");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants