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-11425840: fix position and range in various reports #1517

Merged
merged 1 commit into from
Jul 28, 2022
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 @@ -24,6 +24,7 @@ import amf.core.internal.metamodel.Field
import amf.core.internal.metamodel.domain.ShapeModel
import amf.core.internal.metamodel.domain.common.NameFieldSchema
import amf.core.internal.metamodel.domain.extensions.{CustomDomainPropertyModel, PropertyShapeModel}
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain._
import amf.shapes.client.scala.model.domain.operations.AbstractParameter
import amf.shapes.internal.domain.metamodel._
Expand Down Expand Up @@ -139,7 +140,7 @@ object APICustomShaclFunctions extends BaseCustomShaclFunctions {
ValidationInfo(
UnionShapeModel.AnyOf,
Some(s"All union members must be Object type, ${elem.name.value()} it's not"),
Some(elem.annotations)
Some(union.annotations)
)
}
validationResults.foreach(info => validate(Some(info)))
Expand Down Expand Up @@ -350,7 +351,8 @@ object APICustomShaclFunctions extends BaseCustomShaclFunctions {
members,
validate,
UnionShapeModel.AnyOf,
{ name: String => s"Union must have at most one member with name '$name'" }
{ name: String => s"Union must have at most one member with name '$name'" },
Some(union.annotations)
)
case _ => // ignore
}
Expand All @@ -367,7 +369,8 @@ object APICustomShaclFunctions extends BaseCustomShaclFunctions {
interfaces,
validate,
NodeShapeModel.Inherits,
{ name: String => s"$typeName cannot implement interface '$name' more than once" }
{ name: String => s"$typeName cannot implement interface '$name' more than once" },
Some(shape.annotations)
)
case _ => // ignore
}
Expand All @@ -383,7 +386,8 @@ object APICustomShaclFunctions extends BaseCustomShaclFunctions {
enumValues,
validate,
ScalarShapeModel.Values,
{ name: String => s"Each enum value must be unique, '$name' it's not" }
{ name: String => s"Each enum value must be unique, '$name' it's not" },
Some(s.annotations)
)
case _ => // ignore
}
Expand Down Expand Up @@ -476,12 +480,13 @@ object APICustomShaclFunctions extends BaseCustomShaclFunctions {
s: Seq[NamedDomainElement],
validate: Option[ValidationInfo] => Unit,
field: Field,
message: String => String
message: String => String,
annotations: Some[Annotations]
): Unit = {
s.foreach({ elem =>
val elemName = elem.name.value()
if (elemName != null && isDuplicated(elemName, s))
validate(Some(ValidationInfo(field, Some(message(elemName)), Some(elem.annotations))))
validate(Some(ValidationInfo(field, Some(message(elemName)), annotations)))
})
}
private def isDuplicated(elemName: String, s: Seq[NamedDomainElement]) = s.count(_.name.value() == elemName) > 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package amf.apicontract.internal.validation.shacl.graphql

import amf.core.client.scala.model.domain.{DataNode, NamedDomainElement, ScalarNode}
import amf.core.client.scala.model.domain.{DataNode, NamedDomainElement, ScalarNode, Shape}
import amf.core.internal.metamodel.Field
import amf.core.internal.metamodel.domain.ScalarNodeModel
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.UnionShape
import amf.shapes.client.scala.model.domain.{ArrayShape, UnionShape}
import amf.shapes.client.scala.model.domain.operations.AbstractParameter
import amf.shapes.internal.domain.metamodel.NodeShapeModel
import amf.validation.internal.shacl.custom.CustomShaclValidator.ValidationInfo

object GraphQLArgumentValidator {
def validateInputTypes(obj: GraphQLObject): Seq[ValidationInfo] = {
val fields = obj.fields()

// fields arguments can't be output types
val operationValidations = fields.operations.flatMap { op =>
val operationValidations = obj.operations.flatMap { op =>
op.parameters.flatMap { param =>
if (!param.isValidInputType) {
validationInfo(
NodeShapeModel.Properties,
s"Argument '${param.name}' must be an input type, ${param.schema.name.value()} it's not",
s"Argument '${param.name}' must be an input type, ${getShapeName(param.schema)} it's not",
param.annotations
)
} else {
Expand All @@ -29,11 +27,11 @@ object GraphQLArgumentValidator {
}

// input type fields or directive arguments can't be output types
val propertiesValidations = fields.properties.flatMap { prop =>
val propertiesValidations = obj.properties.flatMap { prop =>
if (!prop.isValidInputType) {
validationInfo(
NodeShapeModel.Properties,
s"${prop.name} must be an input type, ${getPropTypeName(prop)} it's not",
s"${prop.name} must be an input type, ${getShapeName(prop.range)} it's not",
prop.annotations
)
} else None
Expand Down Expand Up @@ -116,9 +114,10 @@ object GraphQLArgumentValidator {
}
}

private def getPropTypeName(prop: GraphQLProperty): String = prop.range match {
case u: UnionShape => GraphQLNullable(u).name
case s => s.name.value()
private def getShapeName(shape: Shape): String = shape match {
case u: UnionShape => GraphQLNullable(u).name
case arr: ArrayShape => s"a list of ${getShapeName(arr.items)}"
case s => s.name.value()
}

private def validationInfo(field: Field, message: String, annotations: Annotations): Some[ValidationInfo] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import amf.core.client.scala.model.domain.extensions.{CustomDomainProperty, Doma
import amf.core.client.scala.model.domain.{DataNode, ObjectNode, ScalarNode, Shape}
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain.operations.{AbstractOperation, AbstractParameter}
import amf.shapes.client.scala.model.domain.{NilShape, NodeShape, ScalarShape, UnionShape}
import amf.shapes.client.scala.model.domain._

case class GraphQLObject(node: NodeShape) {
def name: String = node.name.value()
Expand Down Expand Up @@ -119,9 +119,10 @@ object GraphQLUtils {

def isValidInputType(schema: Shape): Boolean = {
schema match {
case u: UnionShape => GraphQLNullable(u).isValid
case n: NodeShape => GraphQLObject(n).isInput
case _ => true
case u: UnionShape => GraphQLNullable(u).isValid
case n: NodeShape => GraphQLObject(n).isInput
case arr: ArrayShape => isValidInputType(arr.items)
case _ => true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/broken-interface-fields.api.graphql#/declares/shape/Person
Property: http://www.w3.org/ns/shacl#property
Range: [(10,4)-(10,19)]
Range: [(9,0)-(11,1)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field name is required in Person
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/broken-interface-fields.api.graphql#/declares/shape/Person
Property: http://www.w3.org/ns/shacl#property
Range: [(10,4)-(10,19)]
Range: [(9,0)-(11,1)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/directive-argument-wrong-value.api.graphql#/web-api/customDomainProperties/MyDirective
Property: http://www.w3.org/ns/shacl#datatype
Range: [(1,7)-(1,31)]
Range: [(1,26)-(1,30)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-interfaces-object.api.graphql#/declares/shape/Person
Property: http://a.ml/vocabularies/shapes#inherits
Range: [(14,0)-(16,1)]
Range: [(9,0)-(12,1)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-members-union-extension.api.graphql#/declares/union/SearchResult/or/union/SearchResult_1
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(14,0)-(17,1)]
Range: [(21,0)-(21,37)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-members-union.api.graphql#/declares/union/SearchResult
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(9,0)-(12,1)]
Range: [(19,0)-(19,39)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-values-enum-extension.api.graphql#/declares/scalar/Planet/or/scalar/Planet_1
Property: http://www.w3.org/ns/shacl#in
Range: [(21,4)-(21,9)]
Range: [(20,0)-(23,1)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/duplicate-values-enum.api.graphql#/declares/scalar/Planet
Property: http://www.w3.org/ns/shacl#in
Range: [(10,4)-(10,11)]
Range: [(9,0)-(14,1)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type Query {

input InputType {
objectField: Object1
objectFieldList: [Object1]
objectFieldListRequired: [Object1!]
objectFieldListUltraRequired: [Object1!]!
interfaceField: Interface
unionField: Union
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
ModelId: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql
Profile: GraphQL
Conforms: false
Number of results: 3
Number of results: 6

Level: Violation

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: interfaceField must be an input type, Interface it's not
Message: objectField must be an input type, Object1 it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql#/declares/shape/InputType
Property: http://www.w3.org/ns/shacl#property
Range: [(12,4)-(12,24)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: objectField must be an input type, Object1 it's not
Message: objectFieldList must be an input type, a list of Object1 it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql#/declares/shape/InputType
Property: http://www.w3.org/ns/shacl#property
Range: [(12,4)-(12,24)]
Range: [(13,4)-(13,30)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: objectFieldListRequired must be an input type, a list of Object1 it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql#/declares/shape/InputType
Property: http://www.w3.org/ns/shacl#property
Range: [(14,4)-(14,39)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: objectFieldListUltraRequired must be an input type, a list of Object1 it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql#/declares/shape/InputType
Property: http://www.w3.org/ns/shacl#property
Range: [(15,4)-(15,45)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: interfaceField must be an input type, Interface it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql#/declares/shape/InputType
Property: http://www.w3.org/ns/shacl#property
Range: [(16,4)-(16,29)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: unionField must be an input type, Union it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-fields.api.graphql#/declares/shape/InputType
Property: http://www.w3.org/ns/shacl#property
Range: [(12,4)-(12,24)]
Range: [(17,4)-(17,21)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ Number of results: 3
Level: Violation

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: Argument 'input_' must be an input type, Interface it's not
Message: Argument 'input_' must be an input type, Object1 it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-interface-arguments.api.graphql#/declares/shape/Test
Property: http://www.w3.org/ns/shacl#property
Range: [(10,0)-(14,1)]
Range: [(11,19)-(11,34)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: Argument 'input_' must be an input type, Object1 it's not
Message: Argument 'input_' must be an input type, Interface it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-interface-arguments.api.graphql#/declares/shape/Test
Property: http://www.w3.org/ns/shacl#property
Range: [(10,0)-(14,1)]
Range: [(12,22)-(12,39)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: Argument 'input_' must be an input type, Union it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-interface-arguments.api.graphql#/declares/shape/Test
Property: http://www.w3.org/ns/shacl#property
Range: [(10,0)-(14,1)]
Range: [(13,18)-(13,31)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-object-arguments.api.graphql#/web-api/endpoint/%2Fquery%2FobjectArgument
Property: http://www.w3.org/ns/shacl#property
Range: [(7,4)-(7,43)]
Range: [(7,19)-(7,34)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type-in-endpoint
Message: input_ type must be an input type, Interface it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-object-arguments.api.graphql#/web-api/endpoint/%2Fquery%2FinterfaceArgument
Property: http://www.w3.org/ns/shacl#property
Range: [(8,4)-(8,48)]
Range: [(8,22)-(8,39)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type-in-endpoint
Message: input_ type must be an input type, Union it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/is-not-input-type-object-arguments.api.graphql#/web-api/endpoint/%2Fquery%2FunionArgument
Property: http://www.w3.org/ns/shacl#property
Range: [(9,4)-(9,40)]
Range: [(9,18)-(9,31)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ type Query {

interface Interface {
inputReturnField: InputType
inputReturnFieldList: [InputType]
}

input InputType {
randomText: String
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/union-invalid-members-extension.api.graphql#/declares/union/SearchResult/or/union/SearchResult_1
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(23,0)-(23,13)]
Range: [(27,0)-(27,54)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-union-members
Message: All union members must be Object type, Scalar it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/union-invalid-members-extension.api.graphql#/declares/union/SearchResult/or/union/SearchResult_1
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(23,0)-(23,13)]
Range: [(27,0)-(27,54)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-union-members
Message: All union members must be Object type, Union it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/union-invalid-members-extension.api.graphql#/declares/union/SearchResult/or/union/SearchResult_1
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(23,0)-(23,13)]
Range: [(27,0)-(27,54)]
Location:
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ Level: Violation
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/union-invalid-members.api.graphql#/declares/union/SearchResult
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(23,0)-(23,13)]
Range: [(25,0)-(25,47)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-union-members
Message: All union members must be Object type, Scalar it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/union-invalid-members.api.graphql#/declares/union/SearchResult
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(23,0)-(23,13)]
Range: [(25,0)-(25,47)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-union-members
Message: All union members must be Object type, Union it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/union-invalid-members.api.graphql#/declares/union/SearchResult
Property: http://a.ml/vocabularies/shapes#anyOf
Range: [(23,0)-(23,13)]
Range: [(25,0)-(25,47)]
Location:
Loading