remondis-it / pact-consumer-builder

Library for generating pact consumer expectations from java beans
Apache License 2.0
14 stars 6 forks source link

Problem with generic #18

Closed romainvv closed 4 years ago

romainvv commented 4 years ago

Hello,

Thanks for your work. It is very useful. Il have the following issue : I try to turn a class into a DslPart specification by using your library. This class is a generic : public class CustomObj<T> implements Serializable { private static final long serialVersionUID = -8881951915918526307L; private Integer number; private List<T> content; public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } public List<T> getContent() { return content; } public void setContent(List<T> content) { this.content = content; } }

Then I use the following code to generate a DslPart specification :

CustomObj<String> p = new CustomObj<>(); p.setNumber(0); LinkedList l = new LinkedList<String>(); l.add(new String("lala" )); l.add(new String("lulu")); p.setContent(l); DslPart array5 = ConsumerExpects.type(CustomObj.class).build(p);

This code generates the stacktrace : Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class at com.remondis.cdc.consumer.pactbuilder.ReflectionUtil.findGenericTypeFromMethod(ReflectionUtil.java:333) at com.remondis.cdc.consumer.pactbuilder.ReflectionUtil.getTypeOrListType(ReflectionUtil.java:308) at com.remondis.cdc.consumer.pactbuilder.ConsumerBuilderImpl.getModifier(ConsumerBuilderImpl.java:163) at com.remondis.cdc.consumer.pactbuilder.ConsumerBuilderImpl.lambda$expandPropertyMap$0(ConsumerBuilderImpl.java:102) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1553) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) at com.remondis.cdc.consumer.pactbuilder.ConsumerBuilderImpl.expandPropertyMap(ConsumerBuilderImpl.java:104) at com.remondis.cdc.consumer.pactbuilder.ConsumerBuilderImpl.build(ConsumerBuilderImpl.java:86) at com.remondis.cdc.consumer.pactbuilder.ConsumerBuilderImpl.build(ConsumerBuilderImpl.java:80) at lu.chamber.consultationdescourriers.cdc.ConsumeXXXCdcAndTest.createPact(ConsumeXXXCdcAndTest.java:80)

When I tired without generic in the CustomObj class, it works like a charm.

Can you do something to support generics ?

Thanks

schuettec commented 4 years ago

Hey, thanks for reporting! I'm looking at it right now.

schuettec commented 4 years ago

Okay there was a bug in the generic type analysis plus there were no runtime type checks in case of generic types. I build a possible solution for that and I also added a testcase, but I would appreciate if you could test the PR with your use-case before I release the changes.

See #20 for testing.

romainvv commented 4 years ago

Wow thanks for the quick fix. I have an issue while I am compiling : pact-consumer-builder\src\main\java\com\remondis\cdc\consumer\pactbuilder\ConsumerBuilder.java' has not been previously formatted. Please format file and commit before running validation! I skipped the formatting.

I have an issue with a generic class that have list in it like your GenericListDummy tests : JSONAssert.assertEquals("{\"list\":\"A\"}", actualJson, JSONCompareMode.NON_EXTENSIBLE); I think we must expect an array like this : JSONAssert.assertEquals("{\"list\":[\"A\"]}", actualJson, JSONCompareMode.NON_EXTENSIBLE);

schuettec commented 4 years ago

For formatting you can setup your IDE to run the eclipse formatter with the configuration you can find in the project. You can also run mvn formatter:format to apply the format before commiting.

And yes, you're right....the array format is still wrong T.T ....I'm on it.

schuettec commented 4 years ago

Wow that took a while. I did a simple mistake that has been confusing me for a week 😄

22 will be merged in a few minutes and the release will take a few hours to be available.

I would appreciate if you can double check the solution again :)

schuettec commented 4 years ago

FYI: Release 0.1.3 is on it's way to Maven Central.