Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions spring-cloud-contract-verifier/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,18 @@
<artifactId>spring-boot-resttestclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.12.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to a property

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed now 👌🏼

<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.12.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@

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

import org.springframework.util.ClassUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import tools.jackson.databind.json.JsonMapper;

/**
* Wrapper over {@link JsonMapper} that won't try to parse String but will directly return
* it.
*
* @author Marcin Grzejszczak
*/
public class ContractVerifierObjectMapper {

private final JsonMapper objectMapper;

public ContractVerifierObjectMapper(JsonMapper objectMapper) {
this.objectMapper = objectMapper;
public ContractVerifierObjectMapper() {
this(new JsonMapper());
}

public ContractVerifierObjectMapper() {
this.objectMapper = new JsonMapper();
public ContractVerifierObjectMapper(JsonMapper mapper) {
this.objectMapper = usesAvro() ? ignoreAvroFields(mapper) : mapper;
Copy link
Contributor

@marcingrzejszczak marcingrzejszczak Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about one thing though. Now we have Avro. What if in the future we will need to support more things? I don't like that we're leaking Avro in this class.

I think this code should be moved to NoOpContractVerifierAutoConfiguration and have a conditional on class there, where in case of avro being present would create a proper ContractVerifierObjectMapper.

WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcingrzejszczak yes, it makes sense. I've re-arranged things a bit, now.
Can you please take a look?

}

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

private static boolean usesAvro() {
return ClassUtils.isPresent("org.apache.avro.specific.SpecificRecordBase", null);
}

private static JsonMapper ignoreAvroFields(JsonMapper mapper) {
try {
return mapper.rebuild().addMixIn(
ClassUtils.forName("org.apache.avro.specific.SpecificRecordBase",
null), IgnoreAvroMixin.class).build();
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@JsonIgnoreProperties({ "schema", "specificData", "classSchema", "conversion" })
interface IgnoreAvroMixin {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class ContractVerifierObjectMapperSpec extends Specification {
result == '''{"foo":"bar"}'''
}

def "should convert an Avro-generated object into a json representation"() {
given:
FooAvro input = FooAvro.newBuilder().setFooAvro("barAvro").build()
when:
String result = mapper.writeValueAsString(input)
then:
result == '''{"fooAvro":"barAvro"}'''
}

def "should convert bytes into a json representation"() {
given:
String input = '''{"foo":"bar"}'''
Expand Down
Loading