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

JsonMappingException exception is throwing when retrieving a customer #496

Closed
nnurmano opened this issue Apr 6, 2018 · 4 comments
Closed

Comments

@nnurmano
Copy link

nnurmano commented Apr 6, 2018

The code was working with an older stripe version 3.5.0 library, but with the newest library it throws:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.stripe.net.StripeResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.stripe.model.Customer["lastResponse"])

Here is the code pieces:
In the serviceImpl class I retrieve Customer object via
customer = Customer.retrieve(currentAccount.getCustomerStripeId());

In the controller class:

         Customer currentCustomer = stripeService.findCustomerByUserId(id);

        //StripeResponse response = currentCustomer.getLastResponse();
        //System.out.println("the customer is " + response.body());
        //Customer card = APIResource.GSON.fromJson(response.body(), Customer.class);
        if (currentCustomer == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(currentCustomer, HttpStatus.OK);

This piece of code throws above error. When I re-write the method as below:

        Customer currentCustomer = stripeService.findCustomerByUserId(id);
        StripeResponse response = currentCustomer.getLastResponse();
        System.out.println("the customer is " + response.body());
        Customer customer = APIResource.GSON.fromJson(response.body(), Customer.class);
        if (currentCustomer == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(customer, HttpStatus.OK);

The customer is returned with no issues. What have changed in the library? Do I have to re-write my code base as above?

@ob-stripe
Copy link
Contributor

Hi @nomadus. I think this issue is caused by the addition of the lastResponse attribute on StripeObjects in version 5.24.0 (#421).

The library uses Gson as its JSON library, and using APIResource.GSON.fromJson() is indeed the canonical way of deserializing JSON payloads into Stripe objects.

You can go with your rewritten method, or alternatively you can configure Jackson to ignore transient fields with PROPAGATE_TRANSIENT_MARKER.

SudharakaP added a commit to OpenArchitex/OpenLearnr that referenced this issue May 31, 2019
SudharakaP added a commit to OpenArchitex/OpenLearnr that referenced this issue Jun 2, 2019
@dagetachew
Copy link

dagetachew commented Dec 23, 2020

is this issue resolved yet ? I am getting the same exception just for passing ChargeCreateParams object to the charge.create() method. it works fine if I put spring.jackson.mapper.propagate-transient-marker=true in the properites file.
I am not implementing any of stripe interfaces and the below exception is thrown
"Type definition error: [simple type, class com.stripe.net.StripeResponse]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.stripe.net.StripeResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.stripe.model.Charge["lastResponse"])"

@ob-stripe
Copy link
Contributor

Hi @dagetachew. I don't think this issue can be solved from the stripe-java library. stripe-java uses Gson as its JSON library, not Jackson, so we cannot ensure that Stripe objects will play nicely with Jackson.

The proper way to fix this would be to never use Jackson to serialize or deserialize Stripe objects from/to JSON. You can use the following methods provided by the library instead:

// deserialization
Customer customer = APIResource.GSON.fromJson(json, Customer.class);

// serialization
String serialized = customer.toJson();

If you absolutely have to use Jackson, then setting PROPAGATE_TRANSIENT_MARKER to true is a simple workaround to avoid triggering an exception when serializing an object. You could also possibly write a custom Jackson serializer for StripeObject that simply delegates to stripeObject.toJson().

@furnace915
Copy link

furnace915 commented Sep 30, 2021

Thanks for this. My team is spiking out a Stripe solution with Spring and this info was very helpful.

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

No branches or pull requests

4 participants