Skip to content

Commit

Permalink
added comment about usage of anyOf for nullable schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
ghik committed Apr 17, 2024
1 parent bc448d7 commit bac9578
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apispec-model/src/main/scala/sttp/apispec/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ case class Schema(
else copy(`type` = Some(types :+ SchemaType.Null))

case None =>
// Representing nullable schemas (without explicit `type`) using `anyOf` is safer than `oneOf`.
// If `oneOf` was used, and the original schema was already nullable, `null` would not be a valid
// value for the resulting schema.
val nullSchema = Schema(SchemaType.Null)
if(anyOf.contains(nullSchema)) this // ensure idempotency
else if (anyOf.nonEmpty) copy(anyOf = anyOf :+ nullSchema)
Expand Down

0 comments on commit bac9578

Please sign in to comment.