Skip to content

Commit

Permalink
added test for comparing map schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Apr 23, 2024
1 parent 6cc33dd commit e974050
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,11 @@ private class SchemaComparator(
schema.maxProperties.map(Bound.inclusive)
)

private def checkMinMaxProperties(writerSchema: Schema, readerSchema: Schema): Option[MinMaxPropertiesMismatch] = {
private def checkMinMaxProperties(writerSchema: Schema, readerSchema: Schema): Option[ObjectSizeBoundsMismatch] = {
val writerBounds = objectMinMaxProperties(writerSchema)
val readerBounds = objectMinMaxProperties(readerSchema)
if (readerBounds.contains(writerBounds)) None
else Some(MinMaxPropertiesMismatch(writerBounds, readerBounds))
else Some(ObjectSizeBoundsMismatch(writerBounds, readerBounds))
}

private def checkRequiredProperties(writerSchema: Schema, readerSchema: Schema): Option[MissingRequiredProperties] = {
Expand Down Expand Up @@ -475,8 +475,8 @@ private class SchemaComparator(
writerSchema: Schema,
readerSchema: Schema,
): Option[IncompatiblePropertyNames] = {
val writerPropertyNames = writerSchema.propertyNames.getOrElse(Schema.Empty)
val readerPropertyNames = readerSchema.propertyNames.getOrElse(Schema.Empty)
val writerPropertyNames = writerSchema.propertyNames.getOrElse(Schema(SchemaType.String))
val readerPropertyNames = readerSchema.propertyNames.getOrElse(Schema(SchemaType.String))
compare(writerPropertyNames, readerPropertyNames) match {
case Nil => None
case issues => Some(IncompatiblePropertyNames(issues))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ case class ArrayLengthBoundsMismatch(
s"target array length (minItems/maxItems) bounds $readerBounds are stricter than $writerBounds"
}

case class MinMaxPropertiesMismatch(
case class ObjectSizeBoundsMismatch(
writerBounds: Bounds[Int],
readerBounds: Bounds[Int]
) extends SchemaCompatibilityIssue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,60 @@ class SchemaComparatorTest extends AnyFunSuite {
))
}

test("comparing map schemas") {
assert(compare(
objectSchema.copy(
additionalProperties = Some(integerSchema),
propertyNames = Some(stringSchema.copy(maxLength = Some(8))),
minProperties = Some(1),
maxProperties = Some(10),
),
objectSchema.copy(
additionalProperties = Some(numberSchema),
),
) == Nil)

assert(compare(
objectSchema.copy(
additionalProperties = Some(integerSchema),
propertyNames = Some(stringSchema.copy(maxLength = Some(8))),
minProperties = Some(2),
maxProperties = Some(10),
),
objectSchema.copy(
additionalProperties = Some(numberSchema),
propertyNames = Some(stringSchema.copy(maxLength = Some(10))),
minProperties = Some(1),
maxProperties = Some(12),
),
) == Nil)

assert(compare(
objectSchema.copy(
additionalProperties = Some(stringSchema),
),
objectSchema.copy(
additionalProperties = Some(numberSchema),
propertyNames = Some(stringSchema.copy(maxLength = Some(10))),
minProperties = Some(1),
maxProperties = Some(12),
),
) == List(
IncompatibleAdditionalProperties(List(
TypeMismatch(List(SchemaType.String), List(SchemaType.Number))
)),
IncompatiblePropertyNames(List(
StringLengthBoundsMismatch(
Bounds(Some(Bound.inclusive(0)), None),
Bounds(Some(Bound.inclusive(0)), Some(Bound.inclusive(10)))
)
)),
ObjectSizeBoundsMismatch(
Bounds(Some(Bound.inclusive(0)), None),
Bounds(Some(Bound.inclusive(1)), Some(Bound.inclusive(12))
)
)))
}

//TODO significantly more tests
}

0 comments on commit e974050

Please sign in to comment.