Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions spring-cloud-contract-verifier/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<properties>
<javax.ws.rs-api.version>2.1.1</javax.ws.rs-api.version>
<avro.version>1.12.1</avro.version>
</properties>
<parent>
<groupId>org.springframework.cloud</groupId>
Expand Down Expand Up @@ -275,11 +276,28 @@
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-resttestclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>${avro.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>${avro.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jspecify.annotations.Nullable;
import tools.jackson.databind.json.JsonMapper;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.cloud.contract.verifier.converter.YamlContract;
import org.springframework.cloud.contract.verifier.messaging.MessageVerifierReceiver;
import org.springframework.cloud.contract.verifier.messaging.MessageVerifierSender;
Expand Down Expand Up @@ -89,12 +92,32 @@ public ContractVerifierMessaging<Object> contractVerifierMessaging() {

@Bean
@ConditionalOnMissingBean
public ContractVerifierObjectMapper contractVerifierObjectMapper(ObjectProvider<JsonMapper> jsonMapper) {
JsonMapper mapper = jsonMapper.getIfAvailable();
if (mapper != null) {
@ConditionalOnMissingClass("org.apache.avro.specific.SpecificRecordBase")
public ContractVerifierObjectMapper contractVerifierObjectMapper(
ObjectProvider<JsonMapper> jsonMapper) {
JsonMapper mapper = jsonMapper.getIfAvailable(JsonMapper::new);
return new ContractVerifierObjectMapper(mapper);
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.apache.avro.specific.SpecificRecordBase")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it's a cass you can add this as an optional dependency and then normally reference it. Then same below - you could use the class reference instead of doing Class.forName()

Copy link
Author

Choose a reason for hiding this comment

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

I've changed it now, it looks much better 👍🏼
It won't fail because of the SpecificRecordBase import if we don't load the nested config class, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly, since we have textual class on conditional on class here the class will not be loaded. If the class is missing the rest won't be read.

public static class AvroContractVerifierObjectMapperConfiguration {

@Bean
@ConditionalOnMissingBean
public ContractVerifierObjectMapper avroContractVerifierObjectMapper(
ObjectProvider<JsonMapper> jsonMapper) throws ClassNotFoundException {
JsonMapper mapper = jsonMapper.getIfAvailable(JsonMapper::new).rebuild()
.addMixIn(
Class.forName("org.apache.avro.specific.SpecificRecordBase"),
IgnoreAvroMixin.class).build();
return new ContractVerifierObjectMapper(mapper);
}
return new ContractVerifierObjectMapper();

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

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2013-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.contract.verifier.messaging.noop.NoOpContractVerifierAutoConfiguration
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

@ContextConfiguration(classes = [NoOpContractVerifierAutoConfiguration])
class ContractVerifierObjectMapperAvroSpec extends Specification {

@Autowired
ContractVerifierObjectMapper mapper

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"}'
}
}
Loading