-
Notifications
You must be signed in to change notification settings - Fork 434
Avro support #2468
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
Draft
etrandafir93
wants to merge
5
commits into
spring-cloud:main
Choose a base branch
from
etrandafir93:avro_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Avro support #2468
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9fd6e74
add avro support, fix ContractVerifierObjectMapper
etrandafir93 9dfe0be
propagate record headers
etrandafir93 f5fd290
avro support for stub runner executor
etrandafir93 2e4c925
code review
etrandafir93 fb96d44
checkstyle
etrandafir93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...st/groovy/org/springframework/cloud/contract/stubrunner/StubRunnerExecutorAvroSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * 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.stubrunner | ||
|
|
||
| import org.apache.avro.generic.GenericRecord | ||
| import spock.lang.Specification | ||
|
|
||
| import org.springframework.cloud.contract.verifier.messaging.avro.KafkaAvroMessageVerifierSender | ||
| import org.springframework.kafka.core.KafkaTemplate | ||
|
|
||
| class StubRunnerExecutorAvroSpec extends Specification { | ||
|
|
||
| private KafkaTemplate<String, Object> kafkaTemplate = Mock() | ||
| private KafkaAvroMessageVerifierSender sender = new KafkaAvroMessageVerifierSender(kafkaTemplate) | ||
|
|
||
| def 'should send Avro-serialized GenericRecord to Kafka for Avro contracts (bug #2404)'() { | ||
| given: | ||
| def tmpContractDir = saveTmpContract(""" | ||
| label: book_returned | ||
| input: | ||
| triggeredBy: publishBookReturned() | ||
| outputMessage: | ||
| sentTo: book.returned | ||
| headers: | ||
| X-Correlation-Id: abc-123-def | ||
| body: | ||
| isbn: "978-1234567890" | ||
| title: "Contract Testing for Dummies" | ||
| metadata: | ||
| kafka: | ||
| avro: | ||
| schema: > | ||
| { | ||
| "type": "record", | ||
| "name": "Book", | ||
| "fields": [ | ||
| {"name": "isbn", "type": "string"}, | ||
| {"name": "title", "type": "string"} | ||
| ] | ||
| } | ||
| """) | ||
| StubRunnerExecutor executor = new StubRunnerExecutor(new AvailablePortScanner(18000, 18999), sender, []) | ||
| executor.runStubs( | ||
| new StubRunnerOptionsBuilder().build(), | ||
| new StubRepository(tmpContractDir, [], new StubRunnerOptionsBuilder().build(), null), | ||
| new StubConfiguration('avro', 'avro', 'avro', '')) | ||
| when: | ||
| executor.trigger('book_returned') | ||
| then: | ||
| 1 * kafkaTemplate.send({ | ||
| it.topic() == "book.returned" && | ||
| it.value() instanceof GenericRecord && | ||
| it.value()["schema"] != null && | ||
| it.value()["isbn"] == "978-1234567890" && | ||
| it.value()["title"] == "Contract Testing for Dummies" && | ||
| header(it, "X-Correlation-Id") == "abc-123-def" | ||
| }) | ||
| cleanup: | ||
| executor.shutdown() | ||
| tmpContractDir.deleteDir() | ||
| } | ||
|
|
||
| private File saveTmpContract(String contractYaml) { | ||
| File contractDir = File.createTempDir() | ||
| new File(contractDir, "book_returned.yml").text = contractYaml | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a return
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| return contractDir | ||
| } | ||
|
|
||
| private String header(it, String key) { | ||
| new String(it.headers().lastHeader(key).value()) | ||
| } | ||
|
|
||
| } | ||
22 changes: 22 additions & 0 deletions
22
spring-cloud-contract-stub-runner/src/test/resources/avro-repo/book_returned.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| label: book_returned | ||
| input: | ||
| triggeredBy: publishBookReturned() | ||
| outputMessage: | ||
| sentTo: book.returned | ||
| headers: | ||
| X-Correlation-Id: abc-123-def | ||
| body: | ||
| isbn: "978-1234567890" | ||
| title: "Contract Testing for Dummies" | ||
| metadata: | ||
| kafka: | ||
| avro: | ||
| schema: > | ||
| { | ||
| "type": "record", | ||
| "name": "Book", | ||
| "fields": [ | ||
| {"name": "isbn", "type": "string"}, | ||
| {"name": "title", "type": "string"} | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...rc/main/java/org/springframework/cloud/contract/verifier/messaging/avro/AvroMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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.avro; | ||
|
|
||
| /** | ||
| * Avro serialization metadata for a Kafka contract message. | ||
| * | ||
| * <p> | ||
| * Example contract YAML: <pre> | ||
| * metadata: | ||
| * kafka: | ||
| * avro: | ||
| * schema: classpath:avro/Book.avsc | ||
| * </pre> | ||
| * | ||
| * <p> | ||
| * The Schema Registry URL is configured globally via | ||
| * {@code spring.kafka.properties.schema.registry.url}. | ||
| * | ||
| * @author Emanuel Trandafir | ||
| * @since 4.2.0 | ||
| */ | ||
| public final class AvroMetadata { | ||
|
|
||
| /** | ||
| * Classpath or filesystem path to the Avro schema file | ||
| * ({@code .avsc}), e.g. {@code classpath:avro/Book.avsc}. | ||
| * May also be an inline JSON schema string. | ||
| */ | ||
| private String schema; | ||
|
|
||
| /** | ||
| * Returns the Avro schema path or inline schema JSON. | ||
| * | ||
| * @return the schema | ||
| */ | ||
| public String getSchema() { | ||
| return this.schema; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the Avro schema path or inline schema JSON. | ||
| * | ||
| * @param value the schema | ||
| */ | ||
| public void setSchema(final String value) { | ||
| this.schema = value; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What headers are being sent in case of avro messages? Is there any content type?
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.
afaik, there are no headers strictly required by the kafka broker or the Avro/confluent deserializer — the schema info is embedded in the payload.
contentTypeis sometimes added by convention but isn't enforced by anything.This
X-Correlation-Idheader in the test is just a custom header used to verify that user-defined headers are propagated correctly. Would you prefer to rename it to something more obviously test-scoped (e.g.X-Dummy-Custom-Header), or add acontentType: application/avro?