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-12587175: union shouldn't propagate custom domain properties to its members #1732

Merged
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
2 changes: 1 addition & 1 deletion amf-apicontract.versions
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ amf.apicontract=5.3.0-SNAPSHOT
amf.aml=6.3.0-SNAPSHOT
amf.model=3.8.1
antlr4Version=0.6.22
amf.validation.profile.dialect=1.4.0-SNAPSHOT
amf.validation.profile.dialect=1.5.0-SNAPSHOT
amf.validation.report.dialect=1.2.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#%RAML 1.0
title: Something
annotationTypes:
complex:
allowedTargets: TypeDeclaration
type: string
flag:
type: nil
allowedTargets: TypeDeclaration

types:
Type1:
(complex): "Type1"
type: object
properties:
type1prop: string
Type2:
(flag):
type: object
properties:
type2prop: string
Union:
(flag):
(complex): "Union"
type: Type1 | Type2
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import amf.core.client.common.transform.PipelineId
import amf.core.client.scala.config.RenderOptions
import amf.core.client.scala.model.document.{BaseUnit, Document}
import amf.core.client.scala.model.domain.extensions.PropertyShape
import amf.core.client.scala.model.domain.{AmfArray, Annotation, ExternalSourceElement, Shape}
import amf.core.internal.annotations.{DeclaredElement, Inferred, VirtualElement, VirtualNode}
import amf.core.client.scala.model.domain.{AmfArray, Annotation, ExternalSourceElement, ScalarNode, Shape}
import amf.core.internal.annotations.{DeclaredElement, Inferred, SourceYPart, VirtualElement, VirtualNode}
import amf.core.internal.parser.domain.Annotations
import amf.graphql.client.scala.GraphQLConfiguration
import amf.shapes.client.scala.config.JsonSchemaConfiguration
import amf.shapes.client.scala.model.document.JsonSchemaDocument
import amf.shapes.client.scala.model.domain._
import amf.shapes.internal.annotations.{BaseVirtualNode, TargetName}
import amf.shapes.internal.domain.metamodel.AnyShapeModel
import amf.testing.BaseUnitUtils._
import amf.testing.ConfigProvider.configFor
import org.mulesoft.common.client.lexical.{Position, PositionRange}
import org.scalatest.Assertion
import org.scalatest
import org.scalatest.{Assertion, Checkpoints}
import org.scalatest.funsuite.AsyncFunSuite
import org.scalatest.matchers.should.Matchers
import org.yaml.model.{YNodePlain, YScalar}
Expand Down Expand Up @@ -479,4 +482,39 @@ class AMFModelAssertionTest extends AsyncFunSuite with Matchers {
obtainedAst.trim shouldEqual expectedAst.trim
}
}

test("Assert union custom domain properties are not propagated to members") {

def assertForAllTypes(shapes: Shape*)(assertion: Shape => Unit) = {
shapes.foreach(assertion)
}

val api = s"$basePath/raml/union-annots-dont-override-member-annots/api.raml"
val cp = new scalatest.Checkpoints.Checkpoint()
modelAssertion(api, pipelineId = PipelineId.Editing) { unit =>
val doc = unit.asInstanceOf[Document]
val union = doc.declares.collect { case union: UnionShape => union }.head
val type1 = doc.declares.collect { case node: NodeShape if node.name.value() == "Type1" => node }.head
val type2 = doc.declares.collect { case node: NodeShape if node.name.value() == "Type2" => node }.head
union.customDomainProperties.length shouldBe 2
assertForAllTypes(type1, union.anyOf.head) { shape =>
cp { shape.customDomainProperties.length shouldBe 1 }
cp { shape.customDomainProperties.head.extension.asInstanceOf[ScalarNode].value.value() shouldEqual "Type1" }
}
assertForAllTypes(type2, union.anyOf(1)) { shape =>
cp { shape.customDomainProperties.length shouldBe 1 }
cp { shape.customDomainProperties.head.name.value() shouldEqual "flag" }
}
cp.reportAll()
succeed
}
}

private def sourcePartOf(unit: BaseUnit, extract: JsonSchemaDocument => Shape) = {
val doc = unit.asInstanceOf[JsonSchemaDocument]
val shapeOfInterest = extract(doc)
val sourceYPart = shapeOfInterest.annotations.find(_.isInstanceOf[SourceYPart]).get.asInstanceOf[SourceYPart]
val obtainedAst = sourceYPart.ast.toString
obtainedAst
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package amf.shapes.internal.domain.resolution.shape_normalization

import amf.core.client.platform.model.domain.CustomDomainProperty
import amf.core.client.scala.errorhandling.AMFErrorHandler
import amf.core.client.scala.model.DataType
import amf.core.client.scala.model.domain.extensions.PropertyShape
import amf.core.client.scala.model.domain.{AmfArray, AmfScalar, RecursiveShape, Shape}
import amf.core.client.scala.validation.AMFValidationResult
import amf.core.internal.annotations.{Inferred, InheritanceProvenance, LexicalInformation}
import amf.core.internal.metamodel.Field
import amf.core.internal.metamodel.domain.ShapeModel
import amf.core.internal.metamodel.domain.{DomainElementModel, ShapeModel}
import amf.core.internal.metamodel.domain.extensions.PropertyShapeModel
import amf.core.internal.parser.domain.{Annotations, Value}
import amf.core.internal.utils.IdCounter
Expand Down Expand Up @@ -392,7 +393,7 @@ private[resolution] class MinShapeAlgorithm()(implicit val context: Normalizatio
baseNode.fields.setWithoutId(NodeShapeModel.Properties, AmfArray(minProps.toSeq), annotations)

computeNarrowRestrictions(
NodeShapeModel.fields,
NodeShapeModel.fields :+ DomainElementModel.CustomDomainProperties, // custom domain isn't part of nodeshape model fields
baseNode,
superNode,
filteredFields = Seq(NodeShapeModel.Properties, NodeShapeModel.Examples)
Expand Down Expand Up @@ -556,7 +557,8 @@ private[resolution] class MinShapeAlgorithm()(implicit val context: Normalizatio
AnyShapeModel.DefaultValueString,
AnyShapeModel.Default,
AnyShapeModel.Examples,
AnyShapeModel.Description
AnyShapeModel.Description,
AnyShapeModel.CustomDomainProperties
)
val filteredBase = baseShape.copyShape()
filteredBase.fields.filter(f => !filteredFields.contains(f._1))
Expand Down