Skip to content

Commit

Permalink
W-15425021: add binding version validation across all amqp bindings (…
Browse files Browse the repository at this point in the history
…as warning)
  • Loading branch information
arielmirra committed Apr 17, 2024
1 parent aeae444 commit 52b7e4c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ trait BindingParser[+Binding <: DomainElement] extends SpecParserOps {

protected def getDefaultBindingVersion(binding: String, spec: Spec): String = {
(binding, spec) match {
case ("Amqp091ChannelBinding", ASYNC20 | ASYNC21 | ASYNC22 | ASYNC23 | ASYNC24 | ASYNC25 | ASYNC26) => "0.1.0"
case ("Amqp091ChannelBinding", ASYNC20 | ASYNC21 | ASYNC22 | ASYNC23 | ASYNC24 | ASYNC25 | ASYNC26) => "0.1.0"
case ("Amqp091OperationBinding", ASYNC20 | ASYNC21 | ASYNC22 | ASYNC23 | ASYNC24 | ASYNC25 | ASYNC26) => "0.1.0"
case ("Amqp091MessageBinding", ASYNC20 | ASYNC21 | ASYNC22 | ASYNC23 | ASYNC24 | ASYNC25 | ASYNC26) => "0.1.0"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ object AmqpChannelBindingParser extends BindingParser[Amqp091ChannelBinding] {

// bindingVersion is either well defined or defaults to 0.1.0
val binding: Amqp091ChannelBinding = bindingVersion match {
case "0.2.0" | "latest" => Amqp091ChannelBinding020(Annotations(entry))
case "0.1.0" => Amqp091ChannelBinding010(Annotations(entry))
case "0.3.0" | "latest" => // 0.3.0 only changes operation binding, channel binding is the same as 0.2.0
Amqp091ChannelBinding020(Annotations(entry))
case "0.2.0" => Amqp091ChannelBinding020(Annotations(entry))
case "0.1.0" => Amqp091ChannelBinding010(Annotations(entry))
case invalidVersion =>
ctx.eh.warning(
UnsupportedBindingVersionWarning,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ import amf.apicontract.client.scala.model.domain.bindings.amqp.Amqp091MessageBin
import amf.apicontract.internal.metamodel.domain.bindings.Amqp091MessageBindingModel
import amf.apicontract.internal.spec.async.parser.bindings.BindingParser
import amf.apicontract.internal.spec.async.parser.context.AsyncWebApiContext
import amf.apicontract.internal.validation.definitions.ParserSideValidations.UnsupportedBindingVersionWarning
import amf.core.internal.parser.YMapOps
import amf.core.internal.parser.domain.Annotations
import org.yaml.model.{YMap, YMapEntry}

object Amqp091MessageBindingParser extends BindingParser[Amqp091MessageBinding] {
override def parse(entry: YMapEntry, parent: String)(implicit ctx: AsyncWebApiContext): Amqp091MessageBinding = {
val binding = Amqp091MessageBinding(Annotations(entry))
val map = entry.value.as[YMap]
val bindingVersion = getBindingVersion(entry.value.as[YMap], "Amqp091MessageBinding", ctx.specSettings.spec)

// bindingVersion is either well defined or defaults to 0.1.0
val binding: Amqp091MessageBinding = bindingVersion match {
case "0.1.0" | "0.2.0" | "0.3.0" | "latest" => Amqp091MessageBinding(Annotations(entry))
case invalidVersion =>
ctx.eh.warning(
UnsupportedBindingVersionWarning,
Amqp091MessageBinding(Annotations(entry)),
Some("bindingVersion"),
s"Version $invalidVersion is not supported in an Amqp091ChannelBinding",
entry.value.location
)
Amqp091MessageBinding(Annotations(entry))
}

val map = entry.value.as[YMap]

map.key("contentEncoding", Amqp091MessageBindingModel.ContentEncoding in binding)
map.key("messageType", Amqp091MessageBindingModel.MessageType in binding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.yaml.model.{YMap, YMapEntry}

object Amqp091OperationBindingParser extends BindingParser[Amqp091OperationBinding] {
override def parse(entry: YMapEntry, parent: String)(implicit ctx: AsyncWebApiContext): Amqp091OperationBinding = {
val bindingVersion = getBindingVersion(entry.value.as[YMap], "Amqp091ChannelBinding", ctx.specSettings.spec)
val bindingVersion = getBindingVersion(entry.value.as[YMap], "Amqp091OperationBinding", ctx.specSettings.spec)

// bindingVersion is either well defined or defaults to 0.1.0
val binding: Amqp091OperationBinding = bindingVersion match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class BindingsTest extends AnyFunSuite with Matchers with BeforeAndAfterAll {
messageBinding.messageType.value() shouldBe s
}

test("test Amqp091OperationBinding") {
val operationBinding = new Amqp091OperationBinding()
test("test Amqp091OperationBinding010") {
val operationBinding = new Amqp091OperationBinding010()
.withExpiration(1)
.withUserId(s)
.withCc(stringSeq.asClient)
Expand All @@ -135,9 +135,31 @@ class BindingsTest extends AnyFunSuite with Matchers with BeforeAndAfterAll {
operationBinding.ack.value() shouldBe true
}

test("test Amqp091OperationBinding030") {
val operationBinding = new Amqp091OperationBinding030()
.withExpiration(1)
.withUserId(s)
.withCc(stringSeq.asClient)
.withPriority(1)
.withDeliveryMode(1)
.withMandatory(true)
.withBcc(stringSeq.asClient)
.withTimestamp(true)
.withAck(true)
operationBinding.expiration.value() shouldBe 1
operationBinding.userId.value() shouldBe s
operationBinding.cc.toString shouldBe clientStringList.toString
operationBinding.bcc.toString shouldBe clientStringList.toString
operationBinding.priority.value() shouldBe 1
operationBinding.deliveryMode.value() shouldBe 1
operationBinding.mandatory.value() shouldBe true
operationBinding.timestamp.value() shouldBe true
operationBinding.ack.value() shouldBe true
}

test("test ChannelBindings") {
val internalChannelBindings: Seq[amf.apicontract.client.scala.model.domain.bindings.ChannelBinding] =
Seq(new Amqp091ChannelBinding010()._internal)
Seq(new Amqp091ChannelBinding010()._internal, new Amqp091ChannelBinding020()._internal)
val clientChannelBindings = internalChannelBindings.asClient

val channelBindings = new ChannelBindings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
],
"http://a.ml/vocabularies/apiBinding#bindingVersion": [
{
"@value": "31.07.92"
"@value": "0.1.0"
}
],
"http://a.ml/vocabularies/apiBinding#type": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
],
"http://a.ml/vocabularies/apiBinding#contentEncoding": "some/mime",
"http://a.ml/vocabularies/apiBinding#messageType": "sometype",
"http://a.ml/vocabularies/apiBinding#bindingVersion": "31.07.92",
"http://a.ml/vocabularies/apiBinding#bindingVersion": "0.1.0",
"http://a.ml/vocabularies/apiBinding#type": "amqp",
"http://a.ml/vocabularies/document-source-maps#sources": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ channels:
message:
bindings:
amqp:
bindingVersion: 31.07.92
bindingVersion: 0.1.0
messageType: sometype
contentEncoding: some/mime

0 comments on commit 52b7e4c

Please sign in to comment.