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

Endpoint generation from OpenAPI fails for fields with examples #2621

Closed
nikolaiser opened this issue Jan 14, 2024 · 3 comments · Fixed by #2643
Closed

Endpoint generation from OpenAPI fails for fields with examples #2621

nikolaiser opened this issue Jan 14, 2024 · 3 comments · Fixed by #2643
Labels
💎 Bounty bug Something isn't working 💰 Rewarded

Comments

@nikolaiser
Copy link

Describe the bug
EndpointGen.fromOpenAPI fails if one of the components contains a field with an example

To Reproduce

The following scala-cli script

//> using scala 2.13.12
//> using repository sonatype:snapshots
//> using dep dev.zio::zio-http-gen:3.0.0-RC4+41-2d267b56-SNAPSHOT
//> using dep dev.zio::zio-json-yaml:0.6.2

import zio.http.gen.openapi.EndpointGen
import zio.http.endpoint.openapi.OpenAPI
import zio.http.endpoint.openapi.JsonSchema
import zio.json.yaml._
import zio.schema.codec.JsonCodec

object Main {

  val openapiYaml = """
                      |openapi: 3.0.3
                      |info:
                      | title: Example
                      | description: Example API documentation
                      | version: 1.0.0
                      |paths:
                      | /foo:
                      |   post:
                      |     requestBody:
                      |       content:
                      |         application/json:
                      |           schema:
                      |             $ref: '#/components/schemas/Bar' 
                      |     responses:
                      |       '200':
                      |         description: Success
                      |
                      |components:
                      | schemas:
                      |   Bar:
                      |     type: object
                      |     properties:
                      |       stringField:
                      |         type: string
                      |         example: 'abc'
  """.stripMargin

  implicit val openapiDecoder = JsonCodec.jsonDecoder(OpenAPI.schema)

  def main(args: Array[String]) = {
    val openapi = openapiYaml.fromYaml[OpenAPI].toOption.get
    println(EndpointGen.fromOpenAPI(openapi))
  }
}

Throws the exception

Exception in thread "main" java.lang.Exception: Could not generate code for field stringField of object stringField
        at zio.http.gen.openapi.EndpointGen$$anonfun$6.$anonfun$applyOrElse$11(EndpointGen.scala:654)
        at scala.Option.getOrElse(Option.scala:201)
        at zio.http.gen.openapi.EndpointGen$$anonfun$6.applyOrElse(EndpointGen.scala:654)
        at zio.http.gen.openapi.EndpointGen$$anonfun$6.applyOrElse(EndpointGen.scala:650)
        at scala.collection.StrictOptimizedIterableOps.collect(StrictOptimizedIterableOps.scala:151)
        at scala.collection.StrictOptimizedIterableOps.collect$(StrictOptimizedIterableOps.scala:136)
        at scala.collection.immutable.Map$Map1.collect(Map.scala:259)
        at zio.http.gen.openapi.EndpointGen.schemaToCode(EndpointGen.scala:650)
        at zio.http.gen.openapi.EndpointGen.$anonfun$fromOpenAPI$16(EndpointGen.scala:119)
        at scala.collection.StrictOptimizedIterableOps.flatMap(StrictOptimizedIterableOps.scala:118)
        at scala.collection.StrictOptimizedIterableOps.flatMap$(StrictOptimizedIterableOps.scala:105)
        at scala.collection.immutable.Map$Map1.flatMap(Map.scala:259)
        at zio.http.gen.openapi.EndpointGen.$anonfun$fromOpenAPI$15(EndpointGen.scala:108)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at zio.http.gen.openapi.EndpointGen.fromOpenAPI(EndpointGen.scala:107)
        at zio.http.gen.openapi.EndpointGen$.fromOpenAPI(EndpointGen.scala:32)
        at Main$.main(example-fail.scala:46)
        at Main.main(example-fail.scala)

Expected behaviour
EndpointGen.fromOpenAPI can correctly generate code for schemas with examples

@nikolaiser nikolaiser added the bug Something isn't working label Jan 14, 2024
@jdegoes
Copy link
Member

jdegoes commented Jan 18, 2024

/bounty $50

Copy link

algora-pbc bot commented Jan 18, 2024

💎 $50 bounty created by ZIO
🙋 If you start working on this, comment /attempt #2621 to notify everyone
👉 To claim this bounty, submit a pull request that includes the text /claim #2621 somewhere in its body
📝 Before proceeding, please make sure you can receive payouts in your country
💵 Payment arrives in your account 2-5 days after the bounty is rewarded
💯 You keep 100% of the bounty award
🙏 Thank you for contributing to zio/zio-http!

👉 Add a bountyShare on socials

Attempt Started (GMT+2) Solution
🟢 @987Nabil #2643

987Nabil added a commit to 987Nabil/zio-http that referenced this issue Jan 21, 2024
987Nabil added a commit that referenced this issue Jan 21, 2024
* OpenAPI code gen collections fix (#2620)

* OpenAPI code gen with annotations fix (#2621)
Copy link

algora-pbc bot commented Jan 22, 2024

🎉🎈 @987Nabil has been awarded $50! 🎈🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty bug Something isn't working 💰 Rewarded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants