Skip to content

Commit

Permalink
Ignore $ref keyword referencing HTTP resources (close #238)
Browse files Browse the repository at this point in the history
  • Loading branch information
pondzix committed Apr 27, 2023
1 parent c4864bb commit a1e1dda
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import com.snowplowanalytics.iglu.client.resolver.StorageTime
import com.snowplowanalytics.iglu.core.circe.MetaSchemas
// Scala
import com.fasterxml.jackson.databind.JsonNode
import com.networknt.schema.uri.URIFetcher
import com.snowplowanalytics.iglu.client.resolver.Resolver.SchemaLookupResult
import java.io.{ByteArrayInputStream, InputStream}
import java.net.URI
import java.nio.charset.StandardCharsets
import scala.jdk.CollectionConverters._

// Cats
Expand Down Expand Up @@ -55,12 +59,21 @@ object CirceValidator extends Validator[Json] {

private val V4SchemaInstance = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)

private val fakeUrlFetcher = new URIFetcher {
override def fetch(uri: URI): InputStream = {
// No effect on validation, because we return empty JSON Schema which matches any data.
val emptyJsonObject = Json.obj()
new ByteArrayInputStream(emptyJsonObject.toString().getBytes(StandardCharsets.UTF_8))
}
}

private val IgluMetaschemaFactory =
JsonSchemaFactory
.builder(V4SchemaInstance)
.addMetaSchema(IgluMetaschema)
.forceHttps(false)
.removeEmptyFragmentSuffix(false)
.uriFetcher(fakeUrlFetcher, "http", "https")
.build()

private val SchemaValidatorsConfig: SchemaValidatorsConfig = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.snowplowanalytics.iglu.client.validator

import io.circe.literal._
import org.specs2.mutable.Specification

class ValidatingWithRefSpec extends Specification {

val schema =
json"""
{
"$$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
"description": "Test schema using $$ref with 'http'/'https' protocols",
"self": {
"vendor": "com.test",
"name": "test",
"format": "jsonschema",
"version": "1-0-0"
},
"properties": {
"id": {
"allOf": [
{"$$ref": "http://json-schema.org/draft-04/schema#"},
{"$$ref": "https://json-schema.org/draft-04/schema"},
{"$$ref": "http://anything"},
{"$$ref": "https://anything"}
]
}
}
}
"""

"Validator should ignore '$ref' keyword" in {
val data = json"""{"id": "can_be_anything1234"}"""
CirceValidator.validate(data, schema) must beRight(())
}
}

0 comments on commit a1e1dda

Please sign in to comment.