Skip to content

Commit

Permalink
Merge branch 'master' into dyn-ref-fixes-3
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Oct 4, 2023
2 parents 588ceb7 + 316a0fb commit a384c49
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
<version>9.3.4.RC0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/erosb/jsonsKema/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ data class CompositeSchema(
val propertySchemas: Map<String, Schema> = emptyMap(),
val patternPropertySchemas: Map<Regexp, Schema> = emptyMap(),
val unevaluatedItemsSchema: Schema? = null,
val unevaluatedPropertiesSchema: Schema? = null
val unevaluatedPropertiesSchema: Schema? = null,
val unprocessedProperties: Map<IJsonString, IJsonValue> = emptyMap()
) : Schema(location) {
override fun <P> accept(visitor: SchemaVisitor<P>) = visitor.internallyVisitCompositeSchema(this)
override fun subschemas() = subschemas
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/com/github/erosb/jsonsKema/SchemaLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ class SchemaLoader(
var patternPropertySchemas: Map<Regexp, Schema> = emptyMap()
var unevaluatedItemsSchema: Schema? = null
var unevaluatedPropertiesSchema: Schema? = null
var unprocessedProperties: MutableMap<IJsonString, IJsonValue> = mutableMapOf()
return enterScope(schemaJson) {
schemaJson.properties.forEach { (name, value) ->
var subschema: Schema? = null
Expand Down Expand Up @@ -463,6 +464,9 @@ class SchemaLoader(
subschema = loader(ctx)
}
if (subschema != null) subschemas.add(subschema)
if (!isKnownKeyword(name.value)) {
unprocessedProperties[name] = value
}
}
return@enterScope CompositeSchema(
subschemas = subschemas,
Expand All @@ -478,7 +482,8 @@ class SchemaLoader(
dynamicRef = dynamicRef,
dynamicAnchor = dynamicAnchor,
unevaluatedItemsSchema = unevaluatedItemsSchema,
unevaluatedPropertiesSchema = unevaluatedPropertiesSchema
unevaluatedPropertiesSchema = unevaluatedPropertiesSchema,
unprocessedProperties = unprocessedProperties
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/erosb/jsonsKema/SchemaVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ abstract class SchemaVisitor<P> {
open fun visitFormatSchema(schema: FormatSchema): P? = visitChildren(schema)

open fun identity(): P? = null
open fun identity(parent: Schema): P? = identity()
open fun accumulate(parent: Schema, previous: P?, current: P?): P? = current ?: previous
open fun visitChildren(parent: Schema): P? {
var product: P? = identity()
var product: P? = identity(parent)
for (subschema in parent.subschemas()) {
val current = subschema.accept(this)
product = accumulate(parent, product, current)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ internal class AggregatingValidationFailure(
}

override fun join(parent: Schema, instance: IJsonValue, other: ValidationFailure): ValidationFailure {
if (parent != schema) {
TODO("something went wrong")
}
if (instance != this.instance) {
TODO("something went wrong: $instance vs ${this.instance}")
}
Expand Down
15 changes: 12 additions & 3 deletions src/test/kotlin/com/github/erosb/jsonsKema/SchemaLoaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,28 @@ class SchemaLoaderTest {
"writeOnly": false,
"readOnly": true,
"deprecated": false,
"default": null
"default": null,
"if": false,
"then": true,
"dummy": "hi"
}
""".trimIndent()
)()
val expected = CompositeSchema(
subschemas = emptySet(),
subschemas = setOf(
IfThenElseSchema(
FalseSchema(SourceLocation(8, 11, pointer("#/if"))),
TrueSchema(SourceLocation(9, 13, pointer("#/then"))),
null,
SourceLocation(8, 5, pointer("#/if")))),
location = UnknownSource,
title = JsonString("My title"),
description = JsonString("My description"),
readOnly = JsonBoolean(true),
writeOnly = JsonBoolean(false),
deprecated = JsonBoolean(false),
default = JsonNull()
default = JsonNull(),
unprocessedProperties = mutableMapOf(JsonString("dummy") to JsonString("hi"))
)
assertThat(actual).usingRecursiveComparison()
.ignoringFieldsOfTypes(SourceLocation::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.github.erosb.jsonsKema
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.net.URI
import org.skyscreamer.jsonassert.JSONAssert


class ValidationFailureTest {

Expand Down Expand Up @@ -68,4 +70,62 @@ class ValidationFailureTest {
Instance location: http://example.com/my-json: Line 70, character 66
""".trimIndent())
}

@Test
fun issue26Test() {
val doc = """{
"customerName": "acme",
"acquireDate": "2020-12-12"
}"""
val jsonValue = JsonParser(doc).parse()
val userSchema = """{
"type": "object",
"properties": {
"age": {
"type": "integer",
"minimum": 0
}
},
"additionalProperties": false,
"required": [
"age"
]
}
"""

val schemaJson = JsonParser(userSchema).parse()
val loadedSchema = SchemaLoader(schemaJson).load()
val validator = Validator.forSchema(loadedSchema)
JSONAssert.assertEquals(
"""
{
"instanceRef": "#",
"schemaRef": "#/additionalProperties",
"message": "multiple validation failures",
"causes": [
{
"instanceRef": "#/customerName",
"schemaRef": "#/additionalProperties",
"message": "false schema always fails",
"keyword": "false"
},
{
"instanceRef": "#/acquireDate",
"schemaRef": "#/additionalProperties",
"message": "false schema always fails",
"keyword": "false"
},
{
"instanceRef": "#",
"schemaRef": "#/required",
"message": "required properties are missing: age",
"keyword": "required"
}
]
}
""".trimIndent(),
validator.validate(jsonValue)?.toJSON().toString(),
false
)
}
}

0 comments on commit a384c49

Please sign in to comment.