commercetools / commercetools-sdk-java-v2

The e-commerce SDK from commercetools for Java.
https://commercetools.github.io/commercetools-sdk-java-v2/javadoc/index.html
Apache License 2.0
35 stars 15 forks source link

Conflicting setter definitions for property "target" #372

Open milindsingh opened 1 year ago

milindsingh commented 1 year ago

Describe the bug Springdoc open api spec fails

To Reproduce Setup spring app with springdoc

Expected behavior no error

Screenshots/Code snippet

java.lang.IllegalArgumentException: Conflicting setter definitions for property "target": com.commercetools.api.models.review.Review#setTarget(com.commercetools.api.models.channel.ChannelReference) vs com.commercetools.api.models.review.Review#setTarget(java.lang.Object) vs com.commercetools.api.models.review.Review#setTarget(com.commercetools.api.models.product.ProductReference)
    at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder._selectSetterFromMultiple(POJOPropertyBuilder.java:561) ~[jackson-databind-2.13.4.2.jar:2.13.4.2]
    at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:492) ~[jackson-databind-2.13.4.2.jar:2.13.4.2]

Stack information (please complete the following information):

Additional context remove void setTarget(Object var1) in com.commercetools.api.models.review.Review ?

jenschude commented 1 year ago

This is an issue of the ObjectMapper used by springdoc.

The SDK adds the ApiModule to the ObjectMapper used for De-/Serialization. This includes a ReviewMixin class which helps correctly deserializing the review object.

You may have to investigate how to customize the modelconverters or objectmapper used by springdoc. One option could be to use the replaceWithSchema or replaceWithClass method as described here https://springdoc.org/#how-can-i-use-enable-springdoc-openapi-monetaryamount-support (use only of the config methods mentioned below)

    interface CustomReview {
       // ... add necessary properties and jackson annotations
    }

    static {
        SpringDocUtils.getConfig()
                .replaceWithClass(
                        com.commercetools.api.models.review.Review.class,
                        CustomReview.class
                )
            .replaceWithSchema(
                com.commercetools.api.models.review.Review.class,
                new ObjectSchema()
                    .addProperty(
                        "target",
                        new ComposedSchema()
                            .addOneOfItem(new ObjectSchema().$ref("CategoryReview"))
                            .addOneOfItem(new ObjectSchema().$ref("ProductReview"))
                    )
                    // ... add the other properties for review class
            );
    }

I also saw other code snippets which were more sophisticated but may be harder to adapt to your needs.