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

Automatic examples for OpenAPIGen #3196

Closed
barthorre opened this issue Nov 4, 2024 · 7 comments · Fixed by #3273
Closed

Automatic examples for OpenAPIGen #3196

barthorre opened this issue Nov 4, 2024 · 7 comments · Fixed by #3273
Labels

Comments

@barthorre
Copy link

barthorre commented Nov 4, 2024

Is your feature request related to a problem? Please describe.

When using zio-schema to define an input message for a POST endpoint, I don't want to have to define
example(s) for the OpenAPI swagger manually.

Describe the solution you'd like

I would like the OpenAPIGen to have an option to generate examples based on the default value of a zio-schema.

Describe alternatives you've considered

As an alternative I have added the default values of my zio-schema's as examples to the Endpoint definition.

Additional context

I am working with proto specs, which get translated through scalapb to zio-schema's.
Consider this example:

message Message {
  string id = 1;
  oneof data {
    Data1 one = 2;  
    Data2 two = 3;
  }
}

It would be really nice of I could generate 2 example requests on the swagger endpoint automatically, one for each entry in the enum.

The workaround defined above is using something like this

    case record: Schema.Record[_] =>
      record.fields.find(_.fieldName == "data").toList.flatMap { field =>
        field.schema match {
          case enum: Schema.Enum[_] => `enum`.cases.flatMap { cse =>
              cse.schema.defaultValue.toOption.flatMap {
                case a: Message.Data    => Some(cse.id -> Message.of("id", a))
              }
            }
          case _ => throw new RuntimeException("Expected enum")
        }
      }
    case _ => throw new RuntimeException("Expected record")
  }
  
  
val endpoint =  Endpoint(RoutePattern.POST / "message").in[Message].examplesIn(examples: _*).out[String]
  OpenAPIGen.fromEndpoints("API", "1.0.0", endpoint))
@barthorre barthorre added the enhancement New feature or request label Nov 4, 2024
@jdegoes
Copy link
Member

jdegoes commented Nov 9, 2024

/bounty $250 for basic implementation that works off Schema and can handle any Schema but which does not generate meaningful information (e.g. strings are just alphanumeric, etc., regardless of field name).

Copy link

algora-pbc bot commented Nov 9, 2024

💎 $250 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3196 with your implementation plan
  2. Submit work: Create a pull request including /claim #3196 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

Attempt Started (GMT+0) Solution
🔴 @SAIKIRANSURAPALLI Dec 31, 2024, 6:08:55 PM WIP
🟢 @987Nabil #3273

@SAIKIRANSURAPALLI
Copy link

SAIKIRANSURAPALLI commented Dec 31, 2024

/attempt #3196

Copy link

algora-pbc bot commented Jan 7, 2025

@SAIKIRANSURAPALLI: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

Copy link

algora-pbc bot commented Jan 14, 2025

The bounty is up for grabs! Everyone is welcome to /attempt #3196 🙌

Copy link

algora-pbc bot commented Jan 17, 2025

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

987Nabil added a commit to 987Nabil/zio-http that referenced this issue Jan 18, 2025
987Nabil added a commit to 987Nabil/zio-http that referenced this issue Jan 18, 2025
Copy link

algora-pbc bot commented Jan 18, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants