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

Incorrect code generation for oneOf fields #2623

Closed
nikolaiser opened this issue Jan 14, 2024 · 6 comments · Fixed by #2657
Closed

Incorrect code generation for oneOf fields #2623

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

Comments

@nikolaiser
Copy link

Describe the bug
Code that is generated for oneOf fields is incorrect

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
import zio.http.gen.scala.CodeGen
import java.nio.file.Paths

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:
                      |       field:
                      |         oneOf:
                      |           - $ref: '#/components/schemas/Baz'
                      |           - $ref: '#/components/schemas/Qux'
                      | 
                      |   Baz:
                      |     type: object
                      |     properties:
                      |       stringField:
                      |         type: string
                      |
                      |   Qux:
                      |     type: object
                      |     properties:
                      |       stringField:
                      |         type: string
  """.stripMargin

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

  def main(args: Array[String]) = {
    val openapi = openapiYaml.fromYaml[OpenAPI].toOption.get
    val files = EndpointGen.fromOpenAPI(openapi)
    CodeGen.writeFiles(
      files,
      Paths.get("./oneof-output"),
      "foo.bar",
      None
    )
  }
}

Generates a file

package foo.bar.component

import zio.schema._

case class Bar(
 field: Option[Field]
)
object Bar {


 implicit val codec: Schema[Bar] = DeriveSchema.gen[Bar]



}
case class Field(

)
object Field {


 implicit val codec: Schema[Field] = DeriveSchema.gen[Field]



}

Expected behaviour
Fields with a oneOf type should not be represented by an empty case class

@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 #2623 along with your implementation plan
👉 To claim this bounty, submit a pull request that includes the text /claim #2623 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+0) Solution
🔴 @ekhov Jan 18, 2024, 6:43:57 PM WIP
🟢 @987Nabil #2657

@ekhov
Copy link
Contributor

ekhov commented Jan 18, 2024

/attempt #2623

Algora profile Completed bounties Tech Active attempts Options
@ekhov    1 ZIO bounty
+ 0 bounties from 0 projects
Python, Scala
Cancel attempt

@ekhov
Copy link
Contributor

ekhov commented Jan 22, 2024

Will not finish this in time, everyone is welcome to take this issue

Copy link

algora-pbc bot commented Jan 28, 2024

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented Jan 28, 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.

3 participants