zwaldeck / mollie

Java framework to consume the Mollie API
Other
39 stars 36 forks source link

optional payment attributes are null instead of empty Optionals when not set #41

Closed tcmicrosynetics closed 4 years ago

tcmicrosynetics commented 4 years ago

I found this, when I wanted to get the subscription ID of a payment that was not part of a subscription. Please have a look at the following example code:

public class MollieOptional {
    private static final String MOLLIE_TEST_KEY = "test_XXXXX";
    private static final String PAYMENT_WITHOUT_SUBSCRIPTION_ID = "tr_XXXXX";

    public static void main(String[] args) {
        Client mollieClient = new ClientBuilder()
                .withApiKey(MOLLIE_TEST_KEY)
                .build();

        try {
            PaymentResponse payment = mollieClient.payments().getPayment(PAYMENT_WITHOUT_SUBSCRIPTION_ID);

            Optional<String> subscriptionId = payment.getSubscriptionId();
            if(subscriptionId == null) {
                System.out.println("optional is null");
            }
            if(subscriptionId.isEmpty()) {
                System.out.println("optional is empty");
            }
        } catch (MollieException e) {
            e.printStackTrace();
        }
    }
}

I would expect an empty Optional. Calling isEmpty throws a NullPointerException.

paulvaneijden commented 4 years ago

Also experiencing this on all Optionals

paulvaneijden commented 4 years ago

Investigating this, it turns out initialisation of Optional properties is required to solve this.

private Optional<String> subscriptionId = Optional.empty(); Long-read: https://github.com/FasterXML/jackson-modules-java8/issues/36

zwaldeck commented 4 years ago

@paulvaneijden Thanks for the investigation!

I will initialize all Optionals

paulvaneijden commented 4 years ago

Thanks, sounds great! If you need help reviewing let me know.

zwaldeck commented 4 years ago

Deployed new version to bintray & synced with maven central

jepsar commented 3 years ago

Maybe it's an idea to use milestones. Anyway, this fix is available in version 3.0.1.