Skip to content
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

W-16346159: AVRO fixes #2028

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.apicontract.internal.spec.avro.parser.domain.AvroShapeParser
import amf.core.client.scala.model.document.BaseUnitProcessingData
import amf.core.client.scala.parse.document.SyamlParsedDocument
import amf.core.internal.metamodel.document.DocumentModel
import amf.core.internal.metamodel.document.{DocumentModel, FragmentModel}
import amf.core.internal.parser.domain.Annotations
import amf.core.internal.parser.{Root, YMapOps}
import amf.core.internal.remote.Spec
Expand All @@ -24,7 +24,7 @@ class AvroDocumentParser(root: Root)(implicit ctx: AvroSchemaContext) extends Qu
map.key("namespace", (DocumentModel.Package in doc).allowingAnnotations)

val parsedShape = parseType(map)
parsedShape.foreach(shape => doc.withEncodes(shape))
parsedShape.foreach(shape => doc.setWithoutId(FragmentModel.Encodes, shape, Annotations.inferred()))
doc
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package amf.apicontract.internal.spec.avro.parser.domain
import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.{AnyShape, ArrayShape}
import amf.shapes.internal.domain.metamodel.ArrayShapeModel
import org.yaml.model.{YMap, YMapEntry}

case class AvroArrayShapeParser(map: YMap)(implicit ctx: AvroSchemaContext)
extends AvroCollectionShapeParser[ArrayShape](map, "items") {
override val shape: ArrayShape = ArrayShape(map)
override def setMembers(anyShape: AnyShape): Unit = shape.withItems(anyShape)
override val shape: ArrayShape = ArrayShape(map)
override def setMembers(anyShape: AnyShape): Unit =
shape.setWithoutId(ArrayShapeModel.Items, anyShape, Annotations.inferred())

override def parseMembers(e: YMapEntry): AnyShape = AvroTextParser(e.value).parse()

override def parseSpecificFields(): Unit = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package amf.apicontract.internal.spec.avro.parser.domain

import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.core.client.scala.model.domain.Shape
import amf.core.client.scala.model.domain.{AmfArray, ArrayNode, DataNode, Shape}
import amf.core.internal.datanode.DataNodeParser
import amf.core.internal.metamodel.domain.ShapeModel
import amf.core.internal.metamodel.domain.{ArrayNodeModel, ShapeModel}
import amf.core.internal.parser.YMapOps
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.AnyShape
Expand Down Expand Up @@ -41,8 +41,18 @@ abstract class AvroComplexShapeParser(map: YMap)(implicit ctx: AvroSchemaContext
map.key(
"default",
entry => {
val dataNode = DataNodeParser(entry.value).parse()
shape.set(ShapeModel.Default, dataNode, Annotations(entry))
val dataNode: DataNode = DataNodeParser(entry.value).parse()
dataNode match {
// todo: by default the DataNodeParser returns ArrayNodes without annotations (we should add them in amf-core?)
case arrayNode: ArrayNode =>
arrayNode.setWithoutId(
ArrayNodeModel.Member,
AmfArray(arrayNode.members, Annotations.inferred()),
Annotations.inferred()
)
shape.set(ShapeModel.Default, arrayNode, Annotations(entry))
case node => shape.set(ShapeModel.Default, node, Annotations(entry))
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package amf.apicontract.internal.spec.avro.parser.domain

import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.core.client.scala.model.domain.{AmfArray, AmfScalar, ScalarNode}
import amf.core.internal.metamodel.domain.ScalarNodeModel.Value
import amf.core.client.scala.model.DataType
import amf.core.client.scala.model.domain.{AmfArray, ScalarNode}
import amf.core.internal.metamodel.domain.ShapeModel
import amf.core.internal.metamodel.domain.{ScalarNodeModel, ShapeModel}
import amf.core.internal.parser.domain.Annotations
import amf.core.internal.parser.{YMapOps, YScalarYRead}
import amf.shapes.client.scala.model.domain.AnyShape
Expand Down Expand Up @@ -31,5 +32,11 @@ class AvroEnumParser(map: YMap)(implicit ctx: AvroSchemaContext) extends AvroTex
symbols.nodes.map(buildDataNode)
}

private def buildDataNode(symbol: YNode) = ScalarNode(symbol.as[YScalar].text, Some(DataType.String), symbol)
private def buildDataNode(symbol: YNode): ScalarNode = {
val enum = symbol.as[YScalar].text
val ann = Annotations(symbol)
ScalarNode(ann)
.set(Value, AmfScalar(enum, ann), Annotations.inferred())
.set(ScalarNodeModel.DataType, AmfScalar(DataType.String, Annotations.inferred()), Annotations.inferred())
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package amf.apicontract.internal.spec.avro.parser.domain

import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.core.client.scala.model.domain.AmfScalar
import amf.core.client.scala.vocabulary.Namespace.Xsd
import amf.core.internal.parser.YMapOps
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.{AnyShape, ScalarShape}
import amf.shapes.internal.domain.metamodel.AnyShapeModel
import amf.shapes.internal.domain.metamodel.{AnyShapeModel, ScalarShapeModel}
import org.yaml.model.YMap

case class AvroFixedShapeParser(map: YMap)(implicit ctx: AvroSchemaContext) extends AvroComplexShapeParser(map) {
override val shape: AnyShape =
ScalarShape(Annotations(map)).withDataType(Xsd.base + "fixed", Annotations(map.entries.head))
override val shape: AnyShape = {
ScalarShape(Annotations(map)).setWithoutId(
ScalarShapeModel.DataType,
AmfScalar(Xsd.base + "fixed", Annotations.inferred()),
Annotations.inferred()
)
}

override def parseSpecificFields(): Unit = {
map.key("size", (AnyShapeModel.Size in shape).allowingAnnotations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package amf.apicontract.internal.spec.avro.parser.domain

import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.core.client.scala.model.DataType
import amf.core.client.scala.model.domain.AmfScalar
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.{AnyShape, NilShape, NodeShape, ScalarShape}
import amf.shapes.internal.domain.metamodel.ScalarShapeModel
import org.yaml.model.YMap

/** parses primitive avro types such as null, boolean, int, long, float, double, bytes, string */
Expand All @@ -21,9 +23,9 @@ case class AvroTextTypeParser(`type`: String, maybeMap: Option[YMap])(implicit
override lazy val shape: ScalarShape = ScalarShape(annotations)

override def parse(): AnyShape = `type` match {
case "null" => NilShape(annotations).withName(`type`)
case "null" => NilShape(annotations).withName(`type`, typeAnnotations)
case s if avroPrimitiveTypes.contains(s) =>
shape.withDataType(DataType(`type`), typeAnnotations)
shape.setWithoutId(ScalarShapeModel.DataType, AmfScalar(DataType(`type`), annotations), typeAnnotations)
case _ if ctx.globalSpace.contains(`type`) =>
val originalShape = ctx.globalSpace(`type`).asInstanceOf[NodeShape]
originalShape.link(`type`, originalShape.annotations)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package amf.apicontract.internal.spec.avro.parser.domain

import amf.apicontract.internal.spec.avro.parser.context.AvroSchemaContext
import amf.core.client.scala.model.domain.AmfArray
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.UnionShape
import amf.shapes.internal.domain.metamodel.UnionShapeModel
import org.yaml.model.{YMap, YNode}

case class AvroUnionShapeParser(members: Seq[YNode], node: YNode)(implicit ctx: AvroSchemaContext)
extends AvroComplexShapeParser(node.as[YMap]) {
override val shape: UnionShape = UnionShape(node).withName("union")
override val shape: UnionShape = UnionShape(node).withSynthesizeName("union")

override def parseSpecificFields(): Unit = {
val parsedMembers = members.map(node => AvroTextParser(node).parse())
shape.withAnyOf(parsedMembers, Annotations(node))
shape.setWithoutId(UnionShapeModel.AnyOf, AmfArray(parsedMembers, Annotations(node)), Annotations(node))
}
}
Loading