sttp
sttp copied to clipboard
Issue with scala-sttp client generator for OpenAPI (via OpenAPI Generator)
Hello sttp team
We are facing an issue with scala-sttp OpenAPI generator wherein additionalProperties specified for an Object are not being handled when the Scala client is generated. We have reported the issue in the OpenAPI generator and wanted to bring this to your attention.
https://github.com/OpenAPITools/openapi-generator/issues/8955
Any feedback/workaround you may provide would be much appreciated.
Thanks, Manas
Hi,
Thanks, for letting us know. I took some time to look into that issue, still one thing remains unclear to me. How do you coop with such objects later, I mean the objects from the hashMap? What do they actually represents in the runtime? Are they just unparsed json objects with types specific to the chosen library?
If they are primitives you could probably parse them in runtime to the jvm primitives, but that's a special case as in generally they can be arbitrary json objects. Parsing primitives and leaving complex objects untouched is an option but I don't think that it is a good one (it can be relatively easy implemented on the client side assuming that map values are stored as jsons).
Update: Second question - Would such encoding work for you? (assuming use of circe)
case class Person(name: String, age: Int, _additionalProperties: Map[String, Json])
(io.circe.Json
representing arbitrary json values/structures)
with following encoders and decoders
implicit val personDecoder: Decoder[Person] =
new Decoder[Person] {
override def apply(c: HCursor): Result[Person] =
for {
name <- c.downField("name").as[String]
age <- c.downField("age").as[Int]
props <- c.as[JsonObject]
} yield Person(
name,
age,
props.toMap.filterKeys(_ != "name").filterKeys(_ != "age")
)
}
implicit val personEncoder: Encoder[Person] =
new Encoder[Person] {
override def apply(a: Person): Json =
Encoder
.forProduct2[Person, String, Int]("name", "age")(p =>
(p.name, p.age)
)
.apply(a)
.deepMerge(
Encoder.encodeMap[String, Json].apply(a._additionalProperties)
)
}
Now you can also try a dedicated sttp->openapi generator: https://github.com/ghostbuster91/sttp-openapi-generator/
Closing as we are not able to fix this situation from sttp perspective.