Skip to content

Commit 699ea72

Browse files
committed
[Fix] Handle NaN and other double serialization types
1 parent 3971b4f commit 699ea72

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/scala-sttp/additionalTypeSerializers.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,13 @@ trait AdditionalTypeSerializers {
7171
case s: String => Json.fromString(s)
7272
case other => Json.fromString(other.toString)
7373
}
74+
75+
implicit final lazy val NanTolerantDoubleDecoder: Decoder[Double] =
76+
Decoder.decodeDouble.or(Decoder.decodeString.emap {
77+
case "NaN" => Right(Double.NaN)
78+
case "Infinity" => Right(Double.PositiveInfinity)
79+
case "-Infinity" => Right(Double.NegativeInfinity)
80+
case s => Left(s"Cannot decode '$s' as Double")
81+
})
7482
}
7583
{{/circe}}

samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/core/AdditionalTypeSerializers.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ trait AdditionalTypeSerializers {
4848
case s: String => Json.fromString(s)
4949
case other => Json.fromString(other.toString)
5050
}
51+
52+
implicit final lazy val NanTolerantDoubleDecoder: Decoder[Double] =
53+
Decoder.decodeDouble.or(Decoder.decodeString.emap {
54+
case "NaN" => Right(Double.NaN)
55+
case "Infinity" => Right(Double.PositiveInfinity)
56+
case "-Infinity" => Right(Double.NegativeInfinity)
57+
case s => Left(s"Cannot decode '$s' as Double")
58+
})
5159
}

0 commit comments

Comments
 (0)