-
Notifications
You must be signed in to change notification settings - Fork 434
ContractVerifierObjectMapper - special support for Avro #2405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
||
| } | ||
|
|
||
| public String writeValueAsString(Object payload) { | ||
|
|
@@ -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 { | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed now 👌🏼