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-11410812 - Support in draft 2019 as declarations key along with #1495

Merged
merged 2 commits into from
Jul 19, 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
@@ -0,0 +1,16 @@
{
"$schema": "https://json-schema.org/draft-111125751482/schema",
"title": "My Schemas",
"definitions": {
"Person": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ModelId: file://amf-cli/shared/src/test/resources/jsonschema/schemas/multiple-declarations-key.json
Profile:
Conforms: true
Number of results: 1

Level: Warning

- Constraint: http://a.ml/vocabularies/amf/parser#multiple-def-key
Message: Multiple definition keys found in the JSON Schema. You should use only one of them: $defs, definitions
Severity: Warning
Target:
Property:
Range: [(1,0)-(28,1)]
Location: file://amf-cli/shared/src/test/resources/jsonschema/schemas/multiple-declarations-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json-schema.org/draft/2019-09/schema#",
"title": "My Schemas",
"definitions": {
"Person": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
},
"$defs": {
"Animal": {
"properties": {
"breed": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package amf.jsonschema

import amf.apicontract.client.scala.OASConfiguration
import amf.apicontract.client.scala.OASConfiguration.OAS20
import amf.apicontract.client.scala.RAMLConfiguration.RAML10
import amf.validation.UniquePlatformReportGenTest

import scala.concurrent.Future

class JsonSchemaLinkerReportTest extends UniquePlatformReportGenTest with JsonSchemaDocumentTest {
override val basePath: String = "file://amf-cli/shared/src/test/resources/jsonschema/"
override val reportsPath: String = "amf-cli/shared/src/test/resources/jsonschema/reports/"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package amf.jsonschema

import amf.apicontract.client.scala.{AMFConfiguration, ConfigurationAdapter}
import amf.shapes.client.scala.config.JsonSchemaConfiguration
import amf.validation.UniquePlatformReportGenTest

class JsonSchemaValidationTest extends UniquePlatformReportGenTest {

override val basePath: String = "file://amf-cli/shared/src/test/resources/jsonschema/schemas/"
override val reportsPath: String = "amf-cli/shared/src/test/resources/jsonschema/reports/"
val config: AMFConfiguration = ConfigurationAdapter.adapt(JsonSchemaConfiguration.JsonSchema())

test("JSON Schema without $schema key") {
validate(
"multiple-declarations-key.json",
Some("multiple-declarations-key.json.report"),
configOverride = Some(config)
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class JsonSchemaPluginSetupTest extends AsyncFunSuite with Matchers with FileAss
}
}

test("JsonSchemaParsePlugin plugin isn't called with an invalid draft") {
recoverToSucceededIf[UnsupportedDomainForDocumentException] {
parse("invalid-draft.json")
}
}

test("JsonSchemaRenderPlugin renders a JsonSchemaDocument") {
parse("simple.json")
.map { parsed =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,49 @@ import amf.aml.internal.parse.common.{DeclarationKey, DeclarationKeyCollector}
import amf.core.client.scala.model.domain.AmfScalar
import amf.core.internal.annotations.DeclaredElement
import amf.core.internal.metamodel.domain.ShapeModel
import amf.core.internal.parser.YMapOps
import amf.core.internal.parser.domain.Annotations
import amf.core.internal.parser.{YMapOps, _}
import amf.shapes.client.scala.model.domain.{AnyShape, NodeShape}
import amf.shapes.internal.spec.oas.parser.OasTypeParser
import amf.shapes.internal.validation.definitions.ShapeParserSideValidations.UnableToParseShape
import amf.shapes.internal.validation.definitions.ShapeParserSideValidations.{MultipleDefinitionKey, UnableToParseShape}
import org.yaml.model.{YMap, YScalar}
import amf.core.internal.parser._

object TypeDeclarationParser {

def parseTypeDeclarations(map: YMap, definitionsKey: String, declarationKeysHolder: Option[DeclarationKeyCollector])(
implicit ctx: ShapeParserContext
): List[AnyShape] = parseTypeDeclarations(map, Seq(definitionsKey), declarationKeysHolder)

def parseTypeDeclarations(
map: YMap,
definitionsKeys: Seq[String],
declarationKeysHolder: Option[DeclarationKeyCollector]
)(implicit ctx: ShapeParserContext): List[AnyShape] = {
validateMultipleDeclarationKeys(map, definitionsKeys)
definitionsKeys.flatMap(defKey => parseDeclarationMap(map, defKey, declarationKeysHolder)).toList
}

private def validateMultipleDeclarationKeys(map: YMap, definitionsKeys: Seq[String])(implicit
ctx: ShapeParserContext
): Unit = {
val foundDefKeys = definitionsKeys.flatMap(map.key(_))
if (foundDefKeys.size > 1) { // If there is more than 1 definition key present in the map
ctx.eh.warning(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to parse only one key when both are presents, maybe should be a violation instead of a warning?
I think when some information is lost (not parsed) we should throw violations, because some information is not evaluated (and those ignored ast may be invalid at amf)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are parsing all the keys (or at least I suppose I do that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitionsKeys.flatMap(defKey => parseDeclarationMap(map, defKey, declarationKeysHolder)).toList
All the declarations in all the keys should be parsing

specification = MultipleDefinitionKey,
node = "",
message =
MultipleDefinitionKey.message + s". You should use only one of them: ${definitionsKeys.mkString(", ")}",
location = map.location
)
}
}

private def parseDeclarationMap(
map: YMap,
definitionsKey: String,
declarationKeysHolder: Option[DeclarationKeyCollector]
)(implicit
ctx: ShapeParserContext
): List[AnyShape] = {
map.key(definitionsKey).toList.flatMap { entry =>
declarationKeysHolder.foreach(_.addDeclarationKey(DeclarationKey(entry)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ case class JsonSchemaDocumentParser(root: Root)(implicit val ctx: ShapeParserCon
}
}

private def declarationsKey(version: JSONSchemaVersion) = version match {
case JSONSchemaDraft201909SchemaVersion => "$defs"
case _ => "definitions"
private def declarationsKey(version: JSONSchemaVersion): Seq[String] = version match {
case JSONSchemaDraft201909SchemaVersion => Seq($defKey, definitionsKey)
case _ => Seq(definitionsKey)
}

final val definitionsKey = "definitions"
final val $defKey: String = "$defs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ object ShapeParserSideValidations extends Validations {
"Invalid JsonSchema reference"
)

val MultipleDefinitionKey = validation(
"multiple-def-key",
"Multiple definition keys found in the JSON Schema"
)

override val levels: Map[String, Map[ProfileName, String]] = Map(
InvalidShapeFormat.id -> all(WARNING),
JsonSchemaInheritanceWarning.id -> all(WARNING),
Expand Down Expand Up @@ -425,6 +430,7 @@ object ShapeParserSideValidations extends Validations {
MandatorySchema,
UnknownSchemaDraft,
JsonSchemaDefinitionNotFound,
InvalidJsonSchemaReference
InvalidJsonSchemaReference,
MultipleDefinitionKey
)
}