pjfanning / swagger-akka-http-sample

Sample demonstrating use of swagger-akka-http
Apache License 2.0
84 stars 45 forks source link

Is there a way to put boolean query params as an optional parameters. Currently it seems I can just have boolean to be selected as either true or false. #11

Closed mikip91 closed 4 years ago

mikip91 commented 4 years ago

@pjfanning : Please respond as I have an urgent business requirement for passing null values for booleans and not to include it in url is value is null for boolean

pjfanning commented 4 years ago

Have you tried a case class with a param that has type Option[Boolean]?

mikip91 commented 4 years ago

Hey there,

Yes I did try.

It does not allow me to specify null from swagger. The default value is considered as true.

On Mon, Mar 9, 2020, 04:55 PJ Fanning notifications@github.com wrote:

Have you tried a case class with a param that has type Option[Boolean]?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pjfanning/swagger-akka-http-sample/issues/11?email_source=notifications&email_token=AILODRNFKOUZWY3C7IJA62DRGSVHJA5CNFSM4LD7LXP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOGGU7I#issuecomment-596404861, or unsubscribe https://github.com/notifications/unsubscribe-auth/AILODRMKXNY6Z267U2IXSKLRGSVHJANCNFSM4LD7LXPQ .

pjfanning commented 4 years ago

Do you know which of the following is happening?

Maybe, if you could provide a sample, it would be easier to check the problem.

mikip91 commented 4 years ago

I think it's the first option.

There is not a way to select anything else apart from true or false in the swagger page and hence the selection persists. Is there a way we could add Null, True and false options so that when we pass null the query params can be omitted?

On Mon, Mar 9, 2020, 07:53 PJ Fanning notifications@github.com wrote:

Do you know which of the following is happening?

  • the swagger json that is generated incorrectly marks the field as required?
  • the client misinterprets the swagger json and sends a value of true instead of null?
  • the server-side code receives null but incorrectly interprets that as Some(true)?

Maybe, if you could provide a sample, it would be easier to check the problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pjfanning/swagger-akka-http-sample/issues/11?email_source=notifications&email_token=AILODRNYHA7KVCI4HTWQCF3RGTKDBA5CNFSM4LD7LXP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOGZP7Q#issuecomment-596482046, or unsubscribe https://github.com/notifications/unsubscribe-auth/AILODRJ4TQ7DX7LBXZE2F3LRGTKDBANCNFSM4LD7LXPQ .

pjfanning commented 4 years ago

You are using the swagger UI and it only offers true and false as options?

pjfanning commented 4 years ago

I added a branch to this git repo called optional-boolean and modified the addOptional web service to use booleans instead of ints.

Note the way that I had to annotate the Option[Boolean] because the inner type is erased by the compiler.

  // due to type erasure of inner types for scala.Option[T] when T is a primitive type like Int or Boolean,
  // the only way to ensure generated swagger doc has int/boolean type for number2 is to use an annotation
  case class AddOptionRequest(number: Boolean,
                              @Schema(required = false, implementation = classOf[Boolean]) number2: Option[Boolean] = None)

The generated swagger json is correct - in that, it treats the optional boolean as optional.

      "AddOptionRequest" : {
        "required" : [ "number" ],
        "type" : "object",
        "properties" : {
          "number" : {
            "type" : "boolean"
          },
          "number2" : {
            "type" : "boolean"
          }
        }
      }
mikip91 commented 4 years ago

Thanks a lot. I will try that and will keep you posted.

On Mon, Mar 9, 2020, 08:41 PJ Fanning notifications@github.com wrote:

I added a branch to this git repo called optional-boolean and modified the addOptional web service to use booleans instead of ints.

Note the way that I had to annotate the Option[Boolean] because the inner type is erased by the compiler.

// due to type erasure of inner types for scala.Option[T] when T is a primitive type like Int or Boolean, // the only way to ensure generated swagger doc has int/boolean type for number2 is to use an annotation case class AddOptionRequest(number: Boolean, @Schema(required = false, implementation = classOf[Boolean]) number2: Option[Boolean] = None)

The generated swagger json is correct - in that, it treats the optional boolean as optional.

  "AddOptionRequest" : {
    "required" : [ "number" ],
    "type" : "object",
    "properties" : {
      "number" : {
        "type" : "boolean"
      },
      "number2" : {
        "type" : "boolean"
      }
    }
  }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pjfanning/swagger-akka-http-sample/issues/11?email_source=notifications&email_token=AILODRK63CKSPVD36SBS6OTRGTPYFA5CNFSM4LD7LXP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOG6MQA#issuecomment-596502080, or unsubscribe https://github.com/notifications/unsubscribe-auth/AILODRIJB6L4L2IAVG4AW73RGTPYFANCNFSM4LD7LXPQ .

mikip91 commented 4 years ago

You are using the swagger UI and it only offers true and false as options?

@pjfanning Yes!

mikip91 commented 4 years ago

@pjfanning: We are still using swagger-akka-http version 1.1.0. Could you please post the optional-boolean changes for version 1.1.0. We are having a hard time upgrading the swagger-akka-http version to 2.0.4?

pjfanning commented 4 years ago

I created a 2nd branch optional-boolean-1.5

mikip91 commented 4 years ago

@pjfanning: Thanks for reverting so quickly.

I was able to pull the changes in our code. However, our query still stands. The problem is:

I have a boolean field name discoverable which could have true false or null. So, if I select null the query params should not be passed at all. I still am not able to find a way to pass null thorough swagger-ui(v2.1.1). If you could please give me a pointer which would help?

pjfanning commented 4 years ago

I think you'll need to direct swagger-ui problems to the swagger-ui team

mikip91 commented 4 years ago

Thanks @pjfanning. Got this sorted. Appreciate your help.