swagger-akka-http / swagger-scala-module

Swagger support for scala
Apache License 2.0
10 stars 10 forks source link

Type Erasure on Option[Int], Option[Boolean] (options wrapping primitives) #117

Closed jtargui closed 2 years ago

jtargui commented 3 years ago

Hello

In readme.md you told us:

All properties, besides those wrapped in Option or explicitly set via annotations @Schema(required = false, implementation = classOf[Int]), default to required = true in the generated swagger model. See https://github.com/swagger-api/swagger-scala-module/issues/7

case class AddOptionRequest(number: Int, @Schema(required = false, implementation = classOf[Int]) number2: Option[Int] = None)

We know java and Java Reflection are used. It ends suffering type erasure... it replaces Option [Boolean] with an Option [Object]. This happens with Option[Int], Option[Boolean] (options wrapping primitives)

Can solve it without using @Schema decorator?

Thanks...

pjfanning commented 3 years ago

It might be possible to fix it but it would take someone to work on it. Any volunteers?

jtargui commented 3 years ago

Hi!

Thanks for your fast answer! I will try to work with it and any advice would be appreciated :)

pjfanning commented 3 years ago

One potential workaround is to use scala's BigInt instead of Int. I don't think Option[BigInt] suffers from the erasure problem

pjfanning commented 3 years ago

@jtargui the code is quite small so you could try debugging the test "process Model with Scala Option Int" to try to see if you can get it to end up with an IntegerSchema

One caveat is that "io.swagger.core.v3" % "swagger-core" is doing most of the work and I think by the time this lib is called with the 'AnnotatedType' the wrapped class is already lost. But it is worthwhile to get a fresh set of eyes on the problem.

pjfanning commented 3 years ago

One workaround I've considered in the past is supporting an scala object that has a function that takes a class name, a field name and a io.swagger.v3.oas.models.media.Schema instance so that the SwaggerScalaModelConverter could try to ignore the auto-generated Schema and replace it with the registered override. A bit ugly but it means you could do the overrides using code instead of modifying the case classes to have Schema annotations.

pjfanning commented 3 years ago

https://github.com/http4s/rho uses scala-reflect to derive swagger models so may be better able to handle Option[Int]. scala-reflect is no longer supported for Scala 3 but alternatives like izumi-reflect exist. I still suspect that swagger-scala-module would need a major rewrite to support anything other than java reflection.

jtargui commented 3 years ago

Thanks a lot for your comments. I will start to think how to solve it and I will share with you... Thanks! :-)

pjfanning commented 2 years ago

https://github.com/swagger-akka-http/swagger-scala-module/pull/166 provides a solution for Scala2. Scala3 will have to wait (the Scala3 version of this lib is not widely used yet).

pjfanning commented 2 years ago

I will leave this issue open for any Scala 3 macro experts who might want to look at a fix. The Scala 2 support is available in v2.7.0.

pjfanning commented 2 years ago

scala 3 support is there now - although classes defined inside objects are not properly supported (https://github.com/gzoller/scala-reflection/issues/40)