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

Ignore $ref keyword referencing HTTP resources (close #238) #240

Merged
merged 1 commit into from
Apr 27, 2023
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 @@ -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(())
}
}