Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-integer numbers cause incorrect OpenApi YAML #207

Closed
gbogard opened this issue Aug 27, 2019 · 6 comments
Closed

Non-integer numbers cause incorrect OpenApi YAML #207

gbogard opened this issue Aug 27, 2019 · 6 comments

Comments

@gbogard
Copy link

gbogard commented Aug 27, 2019

When a json Input has non-integer fields (double or float) and one provides an example for the input, those fields in the example get serialized as !!int '<the value>' when interpreting the endpoint as OpenAPI YAML.

This is causing Swagger to refuse the documentation. (See screenshot)

Reproduction

import tapir._
import tapir.docs.openapi._
import tapir.swagger.http4s.SwaggerHttp4s
import tapir.openapi.circe.yaml._
import tapir.openapi.Info
import tapir.json.circe._
import cats.effect._
import io.circe.generic.auto._

object Documentation {

  case class Request(height: Float, Width: Float)

  val myEndPoint = endpoint
    .post
    .in(jsonBody[Request].example(Request(45, 52)))
    .out(stringBody)

  val info = Info(
    "My API",
    ""
  )

  val yaml                                    = myEndPoint.toOpenAPI(info).toYaml
  def routes()(implicit cs: ContextShift[IO]) = new SwaggerHttp4s(yaml).routes[IO]
}

The resulting YAML :

openapi: 3.0.1
info:
  title: My API
  version: ''
paths:
  /:
    post:
      operationId: postRoot
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Request'
            example:
              height: !!int '45.0'
              Width: !!int '52.0'
        required: true
      responses:
        '200':
          description: ''
          content:
            text/plain:
              schema:
                type: string
components:
  schemas:
    Request:
      required:
      - height
      - Width
      type: object
      properties:
        height:
          type: number
        Width:
          type: number

I believe the !!int '45.0' is causing the issues with Swagger. This issue on the Circe-yaml project must be closely related. Any idea how I can provide examples for such types ?

@adamw
Copy link
Member

adamw commented Aug 29, 2019

@gbogard the temporary work-around is to provide non-whole example values, e.g. Request(45.9, 52.1)

@gbogard
Copy link
Author

gbogard commented Aug 30, 2019

We'll do that for now, thanks !

@guersam
Copy link
Contributor

guersam commented Oct 27, 2019

My current workaround is using regex:

def fixDecimalExamples(s: String): String =
  s.replaceAll("!!int ('\\d*\\.\\d*')", "$1")

fixDecimalExamples(myEndpoint.toOpenAPI(info).toYaml)

@kell18
Copy link

kell18 commented Nov 25, 2019

@guersam Thanks for the snippet! But you're treating floats as strings then. It'll respect the types:

def swagger_fixIntToFloat(s: String): String =
  s.replaceAll("!!int ('\\d*\\.\\d*')", "!!float $1")

@adamw
Copy link
Member

adamw commented Nov 26, 2019

Isn't this issue gone with circe-yaml 0.12.0?

@adamw
Copy link
Member

adamw commented Nov 26, 2019

yes, jsut tested, should be fixed, (latest is 0.12.4)

@adamw adamw closed this as completed Nov 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants