Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

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

Open
ruioliveiras opened this issue Jun 22, 2018 · 9 comments

Comments

@ruioliveiras
Copy link

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 !

@pjfanning
Copy link
Contributor

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
Copy link
Contributor

I added an example of how you can workaround this issue - pjfanning/swagger-akka-http-sample@c2fa600

@gaeljw
Copy link

gaeljw commented Jul 26, 2018

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

@niladrikar0306
Copy link

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

@GMTurbo
Copy link

GMTurbo commented Oct 9, 2019

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

@pjfanning
Copy link
Contributor

@GMTurbo #55 (comment) -- this is still the case

@GMTurbo
Copy link

GMTurbo commented Oct 9, 2019

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?

@pjfanning
Copy link
Contributor

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.

@GMTurbo
Copy link

GMTurbo commented Oct 11, 2019

Ok, so i got this working using the following:

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

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants