Skip to content

Commit

Permalink
Fix (#278) ClassCastException in OneOf implementation (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt authored Jun 21, 2023
1 parent 1e5ddb3 commit 698281a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.amazon.ionschema.Violation
import com.amazon.ionschema.Violations
import com.amazon.ionschema.internal.DeferredReferenceManager
import com.amazon.ionschema.internal.SchemaInternal
import com.amazon.ionschema.internal.TypeInternal
import com.amazon.ionschema.internal.TypeReference

/**
Expand Down Expand Up @@ -108,7 +109,7 @@ internal class OneOf(ion: IonValue, schema: SchemaInternal, referenceManager: De
oneOfViolation.message = "value matches %s types, expected 1".format(validTypes.size)

validTypes.forEach {
val typeDef = (it as ConstraintBase).ion
val typeDef = (it as TypeInternal).isl
oneOfViolation.add(
Violation(
typeDef, "type_matched",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.amazon.ionschema.internal.constraint

import com.amazon.ionschema.ION
import com.amazon.ionschema.IonSchemaSystemBuilder
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test

class OneOfTest {
val iss = IonSchemaSystemBuilder.standard().build()

@Test
fun issue278() {
// Reproduction of https://github.com/amazon-ion/ion-schema-kotlin/issues/278
val schemaText = """
${'$'}ion_schema_2_0
type::{
name: foo,
one_of: [foo_a, foo_b]
}
type::{
name: foo_a,
type: struct,
fields: {
id: { type: string, occurs: required },
}
}
type::{
name: foo_b,
type: struct,
fields: {
digest: { type: string, occurs: required },
}
}
"""

val schema = iss.newSchema(schemaText)
val type = schema.getType("foo")!!

// In issue #278, this was throwing a ClassCastException.
// This should return normally and indicate that the value is invalid.
val result = type.validate(ION.singleValue("""{ id: "abc", digest: "def" }"""))
assertFalse(result.isValid())
}
}

0 comments on commit 698281a

Please sign in to comment.