sylvainlaurent / swagger-validator-maven-plugin

A maven plugin that validates swagger files in yaml and json formats
Apache License 2.0
11 stars 6 forks source link

ClassCastException ArrayModelWrapper #13

Closed FizzGirl closed 6 years ago

FizzGirl commented 6 years ago

Hi,

thanks for your work on this plugin, comes in really handy :-)

I'm experiencing however the following issue when validating one of my Swaggers:

java.lang.ClassCastException: com.github.sylvainlaurent.maven.swaggervalidator.semantic.node.model.ArrayModelWrapper cannot be cast to com.github.sylvainlaurent.maven.swaggervalidator.semantic.ModelVisitor
    at com.github.sylvainlaurent.maven.swaggervalidator.semantic.node.property.RefPropertyWrapper.accept(RefPropertyWrapper.java:1)
    at com.github.sylvainlaurent.maven.swaggervalidator.semantic.node.model.ArrayModelWrapper.accept(ArrayModelWrapper.java:21)
    at com.github.sylvainlaurent.maven.swaggervalidator.semantic.node.model.AbstractModelWrapper.accept(AbstractModelWrapper.java:1)
    at com.github.sylvainlaurent.maven.swaggervalidator.semantic.validator.definition.ModelValidatorTemplate.validate(ModelValidatorTemplate.java:39)
    at com.github.sylvainlaurent.maven.swaggervalidator.service.SemanticValidationService.validate(SemanticValidationService.java:76)
    at com.github.sylvainlaurent.maven.swaggervalidator.service.ValidationService.validateSwagger(ValidationService.java:98)
    at com.github.sylvainlaurent.maven.swaggervalidator.service.ValidationService.validate(ValidationService.java:68)
    at com.github.sylvainlaurent.maven.swaggervalidator.ValidationServiceTest.test_consent_json(ValidationServiceTest.java:103)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at com.github.sylvainlaurent.maven.swaggervalidator.ValidatorJunitRunner.run(ValidatorJunitRunner.java:17)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Due to company restrictions I cannot post my Swagger JSON file here, but I did some digging in the code and saw the following which seems like a minor bug: https://github.com/sylvainlaurent/swagger-validator-maven-plugin/blob/e318f435e2c093392f7cc0f22b7e634249ea754a/src/main/java/com/github/sylvainlaurent/maven/swaggervalidator/semantic/node/model/ArrayModelWrapper.java#L18-L22

The CCE occurs on the items.accept(this) line . items being of type VisitableProperty which implements the generic interface VisitableObject<T> with a specific generic type ModelVisitor: public interface VisitableProperty<T extends Property> extends VisitableObject<ModelVisitor>, PathObject { Therefore the method accept (T visitor) through the interface VisitableProperty may only receive a ModelVisitor as input. In the above code, as we are passing in this which is a ArrayModelWrapper and is not of type ModelVisitor this causes the CCE behaviour.

I noticed that not any of the tests pass through this code yet, but when adding my swagger to the test set, it goes into this code and makes my 'happy flow' test fail. My swagger is OK according to Swagger UI.

Proposed change is thus replacing items.accept(this) by items.accept(modelvisitor).

sylvainlaurent commented 6 years ago

good catch, and thanks for the detailed explanation. I'm not very comfortable with this part of the code as this is a community contribution, but I'll try to add a test. And BTW, the ìtems`field is using a raw type instead of a generic, and when I add generic types, the compiler immediately raises an error. I'll try to add all missing generics...

sylvainlaurent commented 6 years ago

fixed in 1.2.4

FizzGirl commented 6 years ago

Works like a charm now, thanks for your reactivity :+1: