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 API: OpenAPI documentation generator crashes on parameterized case classes #2767

Open
dubinsky opened this issue Apr 10, 2024 · 3 comments
Labels
💎 Bounty bug Something isn't working

Comments

@dubinsky
Copy link

Describe the bug

With parameterized case class used as endpoint's output, Open API documentation generator crashes.

To Reproduce
Steps to reproduce the behaviour:

  1. Run the following code:
//> using scala 3.4.0
//> using dep dev.zio::zio-http:3.0.0-RC6

import zio.http.Method
import zio.http.endpoint.Endpoint
import zio.http.endpoint.openapi.JsonSchema.SchemaStyle
import zio.http.endpoint.openapi.OpenAPIGen
import zio.schema.{DeriveSchema, Schema}

object OpenAPIBug:
  final case class Item(isEdited: Boolean)
  given Schema[Item] = DeriveSchema.gen[Item]
  final case class Data[A](data: List[A])
  given dataSchema[A: Schema]: Schema[Data[A]] = DeriveSchema.gen[Data[A]]
  def main(args: Array[String]): Unit = OpenAPIGen.fromEndpoints(SchemaStyle.Reference, List(
    Endpoint(Method.GET / "bug").out[Data[Item]]
  ))
  1. Observe the following exception:
java.util.NoSuchElementException: None.get
  at zio.http.endpoint.openapi.OpenAPIGen$$anon$7.applyOrElse(OpenAPIGen.scala:707)

(Caused by the fact that zio.http.endpoint.openapi.OpenAPI.Key.fromString("Data[A]") returns None.)

Expected behaviour
I expect correct Open API documentation to be generated, or, at a minimum, no crashes ;)

@dubinsky dubinsky added the bug Something isn't working label Apr 10, 2024
@lachezar
Copy link
Contributor

lachezar commented May 21, 2024

I’ve also experienced the same issue as reported.

Update:

I've found a workaround - manually generating the Schema with a simple id.

Example:

given [V <: ValidationStatus]: Schema[CreateItemInput[V]] =
  Schema.CaseClass3[String, Money, ProductType, CreateItemInput[V]](
    id0 = TypeId.fromTypeName("CreateItemInput"), // <- "simple" id
    field01 = Schema.Field(name0 = "name", schema0 = Schema[String], get0 = _.name, set0 = (v, x) => v.copy(name = x)),
    field02 =
      Schema.Field(name0 = "price", schema0 = Schema[Money], get0 = _.price, set0 = (v, x) => v.copy(price = x)),
    field03 = Schema.Field(
      name0 = "productType",
      schema0 = Schema[ProductType],
      get0 = _.productType,
      set0 = (v, x) => v.copy(productType = x),
    ),
    construct0 = (name, price, productType) => CreateItemInput[V](name, price, productType),
  )

@jdegoes
Copy link
Member

jdegoes commented Jun 5, 2024

/bounty $150

Copy link

algora-pbc bot commented Jun 5, 2024

💎 $150 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #2767 with your implementation plan
  2. Submit work: Create a pull request including /claim #2767 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

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
Projects
None yet
Development

No branches or pull requests

3 participants