Skip to content

Commit

Permalink
improved error when comparing with empty schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Apr 23, 2024
1 parent 4c0e068 commit 6cc33dd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions apispec-model/src/main/scala/sttp/apispec/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ object SchemaType {
case object String extends SchemaType("string")
case object Integer extends SchemaType("integer")
case object Null extends SchemaType("null")

final val Values = List(Boolean, Object, Array, Number, String, Integer, Null)
}

object SchemaFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,20 @@ private class SchemaComparator(
}
}

private def getTypes(schema: Schema): Option[List[SchemaType]] = schema match {
case Schema.Empty => Some(SchemaType.Values)
case Schema.Nothing => Some(Nil)
case s => s.`type`
}

/**
* Checks if all writer types are compatible with at least one reader type.
* This check assumes schemas that have at least one `type` defined.
*/
private def checkType(writerSchema: Schema, readerSchema: Schema): Option[TypeMismatch] =
for {
writerTypes <- writerSchema.`type`
readerTypes <- readerSchema.`type`
writerTypes <- getTypes(writerSchema)
readerTypes <- getTypes(readerSchema)
incompatibleWriterTypes =
writerTypes.filter(wtpe => !readerTypes.exists(rtpe => typesCompatible(wtpe, rtpe)))
if incompatibleWriterTypes.nonEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SchemaComparatorTest extends AnyFunSuite {
assert(compare(stringSchema, Schema.Empty) == Nil)
assert(compare(opaqueSchema, Schema.Empty) == Nil)
assert(compare(Schema.Empty, stringSchema) == List(
GeneralSchemaMismatch(Schema.Empty, stringSchema) //TODO: better issue for this case
TypeMismatch(SchemaType.Values.filterNot(_ == SchemaType.String), List(SchemaType.String))
))
}

Expand Down Expand Up @@ -436,7 +436,7 @@ class SchemaComparatorTest extends AnyFunSuite {
TypeMismatch(List(SchemaType.String), List(SchemaType.Boolean))
)),
IncompatiblePrefixItem(2, List(
GeneralSchemaMismatch(Schema.Empty, stringSchema)
TypeMismatch(SchemaType.Values.filterNot(_ == SchemaType.String), List(SchemaType.String))
))
))
}
Expand Down

0 comments on commit 6cc33dd

Please sign in to comment.