-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1539 from aml-org/W-11437709
W-11437709: Move validations from Parsing to Validation stage
- Loading branch information
Showing
30 changed files
with
386 additions
and
616 deletions.
There are no files selected for viewing
This file contains 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 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 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
98 changes: 98 additions & 0 deletions
98
...amf/apicontract/internal/validation/shacl/graphql/GraphQLDirectiveLocationValidator.scala
This file contains 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,98 @@ | ||
package amf.apicontract.internal.validation.shacl.graphql | ||
import amf.apicontract.client.scala.model.domain.EndPoint | ||
import amf.apicontract.client.scala.model.domain.api.WebApi | ||
import amf.core.client.scala.model.domain.{AmfScalar, DataNode, DomainElement} | ||
import amf.core.client.scala.model.domain.extensions.{DomainExtension, PropertyShape} | ||
import amf.core.internal.metamodel.domain.DomainElementModel | ||
import amf.core.internal.metamodel.domain.common.{NameFieldSchema, NameFieldShacl} | ||
import amf.core.internal.metamodel.domain.extensions.PropertyShapeModel | ||
import amf.shapes.client.scala.model.domain.{NodeShape, ScalarShape, UnionShape} | ||
import amf.shapes.client.scala.model.domain.operations.{ShapeOperation, ShapeParameter} | ||
import amf.shapes.internal.domain.metamodel.operations.ShapeParameterModel | ||
import amf.validation.internal.shacl.custom.CustomShaclValidator.ValidationInfo | ||
|
||
object GraphQLDirectiveLocationValidator { | ||
def apply( | ||
directiveApplications: Seq[DomainExtension], | ||
element: DomainElement, | ||
appliedToDirectiveArgument: Boolean = false | ||
): Seq[Option[ValidationInfo]] = { | ||
directiveApplications | ||
.map(application => validateApplication(application, element, appliedToDirectiveArgument)) | ||
.collect { case Some(x) => Some(x) } | ||
} | ||
|
||
private def validateApplication( | ||
directiveApplication: DomainExtension, | ||
element: DomainElement, | ||
appliedToDirectiveArgument: Boolean = false | ||
): Option[ValidationInfo] = { | ||
val validDomains = directiveApplication.definedBy.domain.map(_.toString) | ||
val currentDomains = element.meta.typeIris // maybe head? | ||
val isAValidLocation = currentDomains.exists(validDomains.contains) | ||
|
||
// when parsing directives we parse arguments as property shapes of a virtual node shape | ||
lazy val isValidApplicationToDirectiveArgument = appliedToDirectiveArgument && element.meta | ||
.isInstanceOf[PropertyShapeModel.type] && validDomains.intersect(ShapeParameterModel.typeIris).nonEmpty | ||
|
||
if (!isAValidLocation && !isValidApplicationToDirectiveArgument) { | ||
val message = buildErrorMessage(directiveApplication, element, appliedToDirectiveArgument) | ||
Some( | ||
ValidationInfo(DomainElementModel.CustomDomainProperties, Some(message), Some(directiveApplication.annotations)) | ||
) | ||
} else None | ||
} | ||
|
||
private def buildErrorMessage( | ||
directiveApplication: DomainExtension, | ||
element: DomainElement, | ||
appliedToDirectiveArgument: Boolean | ||
) = { | ||
val kind = inferGraphQLKind(element, appliedToDirectiveArgument) | ||
val nameOpt: Option[String] = extractName(element) | ||
|
||
nameOpt match { | ||
case Some(name) => s"Directive ${directiveApplication.name.value()} cannot be applied to $kind $name" | ||
case _ => s"Directive ${directiveApplication.name.value()} cannot be applied to $kind" | ||
} | ||
} | ||
private def extractName(element: DomainElement) = { | ||
element.fields | ||
.getValueAsOption(NameFieldSchema.Name) | ||
.orElse(element.fields.getValueAsOption(NameFieldShacl.Name)) | ||
.flatMap { v => | ||
element match { | ||
case _: WebApi => None | ||
case _: EndPoint => | ||
Some( | ||
v.value | ||
.asInstanceOf[AmfScalar] | ||
.toString | ||
.stripPrefix("Query.") | ||
.stripPrefix("Mutation.") | ||
.stripPrefix("Subscription.") | ||
) | ||
case _ => Some(v.value.asInstanceOf[AmfScalar].toString) | ||
} | ||
} | ||
.map(name => s"'$name'") | ||
} | ||
private def inferGraphQLKind(element: DomainElement, appliedToDirectiveArgument: Boolean): String = { | ||
element match { | ||
case _: PropertyShape if appliedToDirectiveArgument => "argument" | ||
case _: PropertyShape => "field" | ||
case _: ShapeOperation => "field" | ||
case _: ShapeParameter => "argument" | ||
case s: ScalarShape if s.values.nonEmpty => "enum" | ||
case _: ScalarShape => "scalar" | ||
case n: NodeShape if n.isAbstract.value() => "interface" | ||
case n: NodeShape if n.isInputOnly.value() => "input object" | ||
case _: NodeShape => "object" | ||
case _: UnionShape => "union" | ||
case _: DataNode => "value" | ||
case _: EndPoint => "field" | ||
case _: WebApi => "schema" | ||
case _ => "type" // should be unreachable | ||
} | ||
} | ||
} |
18 changes: 5 additions & 13 deletions
18
...red/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.federation.report
This file contains 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 |
---|---|---|
@@ -1,22 +1,14 @@ | ||
ModelId: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql | ||
Profile: GraphQL | ||
Conforms: false | ||
Number of results: 2 | ||
Number of results: 1 | ||
|
||
Level: Violation | ||
|
||
- Constraint: http://a.ml/vocabularies/amf/parser#duplicated-argument | ||
- Constraint: http://a.ml/vocabularies/amf/parser#duplicated-argument-field | ||
Message: Cannot exist two or more arguments with name 'unit' | ||
Severity: Violation | ||
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql#/declares/shape/Starship/supportedOperation/length/expects/request/parameter/parameter/unit | ||
Property: | ||
Range: [(12,11)-(12,29)] | ||
Location: | ||
|
||
- Constraint: http://a.ml/vocabularies/amf/parser#duplicated-argument | ||
Message: Cannot exist two or more arguments with name 'unit' | ||
Severity: Violation | ||
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql#/declares/shape/Starship/supportedOperation/length/expects/request/parameter/parameter/unit_1 | ||
Property: | ||
Range: [(12,31)-(12,48)] | ||
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql#/declares/shape/Starship | ||
Property: http://a.ml/vocabularies/shapes#parameter | ||
Range: [(12,4)-(12,56)] | ||
Location: |
18 changes: 5 additions & 13 deletions
18
amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.report
This file contains 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 |
---|---|---|
@@ -1,22 +1,14 @@ | ||
ModelId: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql | ||
Profile: GraphQL | ||
Conforms: false | ||
Number of results: 2 | ||
Number of results: 1 | ||
|
||
Level: Violation | ||
|
||
- Constraint: http://a.ml/vocabularies/amf/parser#duplicated-argument | ||
- Constraint: http://a.ml/vocabularies/amf/parser#duplicated-argument-field | ||
Message: Cannot exist two or more arguments with name 'unit' | ||
Severity: Violation | ||
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql#/declares/shape/Starship/supportedOperation/length/expects/request/parameter/parameter/unit | ||
Property: | ||
Range: [(12,11)-(12,29)] | ||
Location: | ||
|
||
- Constraint: http://a.ml/vocabularies/amf/parser#duplicated-argument | ||
Message: Cannot exist two or more arguments with name 'unit' | ||
Severity: Violation | ||
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql#/declares/shape/Starship/supportedOperation/length/expects/request/parameter/parameter/unit_1 | ||
Property: | ||
Range: [(12,31)-(12,48)] | ||
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-arguments.api.graphql#/declares/shape/Starship | ||
Property: http://a.ml/vocabularies/shapes#parameter | ||
Range: [(12,4)-(12,56)] | ||
Location: |
Oops, something went wrong.