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

[gen] accept OpenAPI "open" types like "any" & "anyObject" #3199

Open
hochgi opened this issue Nov 6, 2024 · 6 comments · May be fixed by #3205
Open

[gen] accept OpenAPI "open" types like "any" & "anyObject" #3199

hochgi opened this issue Nov 6, 2024 · 6 comments · May be fixed by #3205
Labels
💎 Bounty enhancement New feature or request

Comments

@hochgi
Copy link
Contributor

hochgi commented Nov 6, 2024

defining "Any" in OpenAPI according to the docs: https://swagger.io/docs/specification/v3_0/data-models/data-types/
Screenshot 2024-11-06 at 8 50 01

These won't work in zio-http-gen.

Expectation:

components:
  schemas:
    Foo:
      type: object
      properties:
        name:
          type: string
        anything: {}

should be rendered as:

case class Foo(name: String, anything: Any)

Also, support "free form objects:
Screenshot 2024-11-06 at 17 33 55
e.g:

components:
  schemas:
    Foo:
      type: object
      properties:
        name:
          type: string
        anything:
          type: object
          additionalProperties: {} # or true, or nothing at all

should be rendered as:

case class Foo(name: String, anything: Map[String, Any])

Also, since Any is not really nice to use, and a more common use case would be to generate with Json:

case class Foo(name: String, anything: Json)

or:

case class Foo(name: String, anything: Json.Obj)

instead of Any/Map[String, Any]

we should add an ability to configure this.
Obviously we need to provide a way to generate a zio-json compatible code,
but perhaps other Json ASTs could be supported (like circe/playjson/etc'…)?
This controls both the code, and the required imports.

default should be Any/Map[String, Any], since it would be surprising to generate code that would fail to compile unless you add a 3rd party dependency.

@hochgi hochgi added the enhancement New feature or request label Nov 6, 2024
@jdegoes
Copy link
Member

jdegoes commented Nov 9, 2024

No, any must map to Json, whereas an object must map to Map[String, Json].

Using Scala's Any type does not make sense.

/bounty $125

Copy link

algora-pbc bot commented Nov 9, 2024

💎 $125 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3199 with your implementation plan
  2. Submit work: Create a pull request including /claim #3199 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
🟢 @hochgi Nov 11, 2024, 3:07:17 PM #3205

@hochgi
Copy link
Contributor Author

hochgi commented Nov 9, 2024

No, any must map to Json, whereas an object must map to Map[String, Json].

Using Scala's Any type does not make sense.

@jdegoes I agree that any "serious" use case should use Json. But this requires to manually add a dependency in the build for the Json AST.

IMO, code generation tools should not generate code that requires extra dependencies to compile, unless user explicitly opt in for it.

Yes, Any is aweful to deal with in real code.
And maybe in scala 3 we can do a bit better with union types Boolean | String | Decimal | List[_] | Map[String, _]?
But as a user, I would expect it. Generating Json is surprising.

Regarding objects - not a big difference, but why is Map[String, Json] preferred over Json.Obj?
EDIT: I tried to also add special casing for Json.Obj, and now I see why you want Map[String, Json].
There is no real added value to work with Json.Obj instead, and the complications to the code are just not worth it.
So PR focuses on exactly what you suggested.

@987Nabil
Copy link
Contributor

@hochgi zio-json is a dependency of zio-http. I think Json is okay

@hochgi
Copy link
Contributor Author

hochgi commented Nov 11, 2024

/attempt #3199

Algora profile Completed bounties Tech Active attempts Options
@hochgi 4 ZIO bounties
Scala, Java
Cancel attempt

Copy link

algora-pbc bot commented Nov 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants