swagger-scala-module icon indicating copy to clipboard operation
swagger-scala-module copied to clipboard

case class with field option[Int] is resolved as object and not Number

Open ruioliveiras opened this issue 7 years ago • 9 comments

Hi, When I have something like this:

case class Test(quantity:Int, quantityOpt:Option[Int])

The swagger model will have this properties:

    properties:
      quantity:
        type: integer
        format: int32
      quantityOpt:
        type: object

But the right definition should be something like this

    properties:
      quantity:
        type: integer
        format: int32
      quantityOpt:
        type: integer
        format: int32

@pjfanning , @fehguy

There are any solution for it ?

Thanks !

ruioliveiras avatar Jun 22 '18 12:06 ruioliveiras

The current code uses Java reflection and suffers from type erasure - the model converter basically just gets notified that quantityOpt is an Option[Any].

You should be able to use swagger annotations on the quantityOpt field to fix your swagger doc. https://github.com/swagger-api/swagger-core/wiki/annotations-1.5.x

pjfanning avatar Jun 22 '18 22:06 pjfanning

I added an example of how you can workaround this issue - https://github.com/pjfanning/swagger-akka-http-sample/commit/c2fa6004d3a14c7ecec6d469f1b171229ce69dcf

pjfanning avatar Jun 23 '18 18:06 pjfanning

Thanks @pjfanning, using @ApiModelProperty works but let's hope it's a pain when you have a lot of classes.

gaeljw avatar Jul 26 '18 09:07 gaeljw

Isn't it possible to fix without annotation? We are using external models. So its difficult to add annotations on every option field.

niladrikar0306 avatar Aug 06 '19 04:08 niladrikar0306

What is the solution to this using OpenAPI V3.0? Option[Int] still doesn't work in latest version

GMTurbo avatar Oct 09 '19 16:10 GMTurbo

@GMTurbo https://github.com/swagger-api/swagger-scala-module/issues/55#issuecomment-399596128 -- this is still the case

pjfanning avatar Oct 09 '19 18:10 pjfanning

so i'm not sure what annotation to use to modify the quantityOpt field. I have a bunch of case classes that have a lot of Options in them.

Because I'm using OpenAPI V3.0, i don't have access to the @ApiModelProperty annotation, so i'm supposed to use @Schema now but i'm not sure how to get it to work.

I've tried the following:

case class Metrics(id: String,
                   @(Schema @field)(`type` = "int") size: Option[Int] = None,

This doesn't seem to work although it compiles fine. Any ideas?

GMTurbo avatar Oct 09 '19 20:10 GMTurbo

If you need OpenAPI 3, then you could try https://github.com/swagger-akka-http/swagger-scala-module - https://github.com/swagger-api/swagger-scala-module only supports swagger 2 spec.

pjfanning avatar Oct 10 '19 06:10 pjfanning

Ok, so i got this working using the following:

case class Metrics(id: String,
                   @(Schema @field)(implementation = classOf[Int]) size: Option[Int] = None

Thanks

GMTurbo avatar Oct 11 '19 19:10 GMTurbo