Skip to content

Commit 558624b

Browse files
committed
ignore avro-specific fields if avro is in the classpath
closes gh-2402 Signed-off-by: Emanuel Trandafir <emanueltrandafir1993@gmail.com>
1 parent a66f03e commit 558624b

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

spring-cloud-contract-verifier/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@
286286
<version>1.12.1</version>
287287
<scope>test</scope>
288288
</dependency>
289+
<dependency>
290+
<groupId>org.apache.avro</groupId>
291+
<artifactId>avro</artifactId>
292+
<version>1.12.1</version>
293+
<scope>provided</scope>
294+
</dependency>
289295
</dependencies>
290296
<build>
291297
<plugins>

spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/messaging/internal/ContractVerifierObjectMapper.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,27 @@
1616

1717
package org.springframework.cloud.contract.verifier.messaging.internal;
1818

19+
import org.springframework.util.ClassUtils;
20+
21+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22+
1923
import tools.jackson.databind.json.JsonMapper;
2024

2125
/**
2226
* Wrapper over {@link JsonMapper} that won't try to parse String but will directly return
2327
* it.
24-
*
2528
* @author Marcin Grzejszczak
2629
*/
2730
public class ContractVerifierObjectMapper {
2831

2932
private final JsonMapper objectMapper;
3033

31-
public ContractVerifierObjectMapper(JsonMapper objectMapper) {
32-
this.objectMapper = objectMapper;
34+
public ContractVerifierObjectMapper() {
35+
this(new JsonMapper());
3336
}
3437

35-
public ContractVerifierObjectMapper() {
36-
this.objectMapper = new JsonMapper();
38+
public ContractVerifierObjectMapper(JsonMapper mapper) {
39+
this.objectMapper = usesAvro() ? ignoreAvroFields(mapper) : mapper;
3740
}
3841

3942
public String writeValueAsString(Object payload) {
@@ -56,4 +59,23 @@ else if (payload instanceof byte[]) {
5659
return this.objectMapper.writeValueAsBytes(payload);
5760
}
5861

62+
private static boolean usesAvro() {
63+
return ClassUtils.isPresent("org.apache.avro.specific.SpecificRecordBase", null);
64+
}
65+
66+
private static JsonMapper ignoreAvroFields(JsonMapper mapper) {
67+
try {
68+
return mapper.rebuild().addMixIn(
69+
ClassUtils.forName("org.apache.avro.specific.SpecificRecordBase",
70+
null), IgnoreAvroMixin.class).build();
71+
}
72+
catch (ClassNotFoundException e) {
73+
throw new RuntimeException(e);
74+
}
75+
}
76+
77+
@JsonIgnoreProperties({ "schema", "specificData", "classSchema", "conversion" })
78+
interface IgnoreAvroMixin {
79+
}
80+
5981
}

0 commit comments

Comments
 (0)