swagger-coverage
swagger-coverage copied to clipboard
Serialization error when using gson library and swagger-codegen-maven-plugin
In my project I use swagger-codegen-maven-plugin 2.4.9 + rest assured 4.1.1 and gson library for serialization. When I added the swagger-coverage in the pom.xml, jackson library was included into the classpath too. When i started to build my project the class model was generated using gson library.
The following error occurred while running the tests:
[ERROR] checkDeposit(com.test.SimpleTest) Time elapsed: 1.859 s <<< ERROR!
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "Result" (class com.test.model.ApiResultIResponseAuth), not marked as ignorable (2 known properties: "error", "result"])
at [Source: (String)"{"Result":{"AccessToken":"token","Expires"[truncated 296 chars]; line: 1, column: 12] (through reference chain: com.test.model.ApiResultIResponseAuth["Result"])
Workaround: One of simple workarounds is maven exclusions. You may just exclude jackson library from classpath for swagger-coverage-rest-assured:
<dependency>
<groupId>com.github.viclovsky.swagger.coverage</groupId>
<artifactId>swagger-coverage-rest-assured</artifactId>
<version>1.1.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
Or you may configure RestAssured to use GSON serialization library directly.
This can be done using ObjectMapperConfig.java
https://github.com/rest-assured/rest-assured/blob/master/rest-assured/src/main/java/io/restassured/config/ObjectMapperConfig.java