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-11415930 - Added resolution suite #1504

Merged
merged 1 commit into from
Jul 22, 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
56 changes: 56 additions & 0 deletions amf-cli/js/typings/amf-client-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,59 @@ declare module 'amf-client-js' {


}

export class JsonSchemaDocument implements Document {
location: string
usage: StrField
id: string
raw: undefined | string
processingData: BaseUnitProcessingData
sourceSpec: undefined | Spec
sourceInformation: BaseUnitSourceInformation
modelVersion: StrField
encodes: DomainElement
declares: Array<DomainElement>
schemaVersion: StrField

constructor()
constructor(encoding: DomainElement)

findByType(typeId: string): Array<DomainElement>

cloneUnit(): BaseUnit

withReferences(references: Array<BaseUnit>): this

withDeclaredElement(declared: DomainElement): this

withRaw(raw: string): this

withUsage(usage: string): this

findById(id: string): undefined | DomainElement

withLocation(location: string): this

withReferenceAlias(alias: string, id: string, fullUrl: string, relativeUrl: string): BaseUnit

withEncodes(encoded: DomainElement): this

pkg(): StrField

withPkg(pkg: string): this

withDeclares(declares: Array<DomainElement>): this

references(): Array<BaseUnit>

withProcessingData(data: BaseUnitProcessingData): this

withId(id: string): this



}

export class Amqp091OperationBinding implements OperationBinding {
priority: IntField
customDomainProperties: Array<DomainExtension>
Expand Down Expand Up @@ -2665,6 +2718,9 @@ declare module 'amf-client-js' {
static fromSpec(spec: Spec): AMFConfiguration


}
export class JsonSchemaConfiguration {
static JsonSchema(): ShapesConfiguration
}
export class Extension extends Document {
constructor()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json-schema.org/draft-07/schema",
"title": "My Schemas",
"type": "array",
"items": {
"$ref": "#/definitions/Person"
},
"definitions": {
"Person": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Computer",
"type": "object",
"properties": {
"processor": {
"$ref": "referenced.json#/definitions/processor"
},
"RAM": {
"$ref": "referenced.json#/definitions/RAM"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Computer",
"type": "object",
"definitions": {
"processor": {
"type": "object",
"properties": {
"frequency": {
"type": "string",
"enum": ["3.2ghz", "3.4ghz"]
},
"socket": {
"type": "string"
}
}
},
"RAM": {
"type": "object",
"properties": {
"memory": {
"type": "string",
"enum": ["8GB", "16GB", "32GB"]
},
"frequency": {
"type": "integer"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package amf.jsonschema

import amf.io.FileAssertionTest
import amf.shapes.client.scala.config.JsonSchemaConfiguration
import amf.shapes.client.scala.model.document.JsonSchemaDocument
import amf.shapes.client.scala.model.domain.{ArrayShape, NodeShape}
import org.scalatest.funsuite.AsyncFunSuite
import org.scalatest.matchers.should.Matchers

import scala.concurrent.{ExecutionContext, Future}

class JsonSchemaResolutionTest extends AsyncFunSuite with Matchers with FileAssertionTest {
private val base = "file://amf-cli/shared/src/test/resources/jsonschema/schemas/"
private val client = JsonSchemaConfiguration.JsonSchema().baseUnitClient()

override implicit def executionContext: ExecutionContext = ExecutionContext.Implicits.global

test("Json Schema Fragment with root $ref to an internal declaration") {
for {
parsed <- client.parse(base + "simple-inner-ref.json")
resolved <- Future.successful(client.transform(parsed.baseUnit.cloneUnit()))
} yield {
parsed.conforms shouldBe true
resolved.conforms shouldBe true
parsed.baseUnit shouldBe a[JsonSchemaDocument]
resolved.baseUnit shouldBe a[JsonSchemaDocument]
val parsedDoc = parsed.baseUnit.asInstanceOf[JsonSchemaDocument]
val resolvedDoc = resolved.baseUnit.asInstanceOf[JsonSchemaDocument]
parsedDoc.encodes shouldBe a[ArrayShape]
resolvedDoc.encodes shouldBe a[ArrayShape]
val parsedEncoded = parsedDoc.encodes.asInstanceOf[ArrayShape]
val resolvedEncoded = resolvedDoc.encodes.asInstanceOf[ArrayShape]
parsedEncoded.items shouldBe a[NodeShape]
resolvedEncoded.items shouldBe a[NodeShape]
parsedEncoded.items.isLink shouldBe true
resolvedEncoded.items.isLink shouldBe false
resolvedEncoded.items.asInstanceOf[NodeShape].properties.size shouldBe 2
resolvedEncoded.items.asInstanceOf[NodeShape].properties.map(_.name.value()).contains("age") shouldBe true
}
}

test("Json Schema Fragment with root $ref to an external Json Schema declaration") {
for {
parsed <- client.parse(base + "simple-ref-external/base.json")
resolved <- Future.successful(client.transform(parsed.baseUnit.cloneUnit()))
} yield {
parsed.conforms shouldBe true
resolved.conforms shouldBe true
parsed.baseUnit shouldBe a[JsonSchemaDocument]
resolved.baseUnit shouldBe a[JsonSchemaDocument]
val parsedDoc = parsed.baseUnit.asInstanceOf[JsonSchemaDocument]
val resolvedDoc = resolved.baseUnit.asInstanceOf[JsonSchemaDocument]
parsedDoc.encodes shouldBe a[NodeShape]
resolvedDoc.encodes shouldBe a[NodeShape]
parsedDoc.encodes.asInstanceOf[NodeShape].properties.head.range.isLink shouldBe true
resolvedDoc.encodes.asInstanceOf[NodeShape].properties.head.range.isLink shouldBe false
resolvedDoc.encodes.asInstanceOf[NodeShape].properties.head.range shouldBe a[NodeShape]
val encodedPropertyRange =
resolvedDoc.encodes.asInstanceOf[NodeShape].properties.head.range.asInstanceOf[NodeShape]
encodedPropertyRange.properties.map(_.name.value()).contains("frequency") shouldBe true
}
}

}