mattpolzin / OpenAPIKit

Codable Swift OpenAPI implementation.
MIT License
280 stars 35 forks source link

Add a hint for allowed values types #246

Closed kean closed 2 years ago

kean commented 2 years ago

Cherry-pick of https://github.com/mattpolzin/OpenAPIKit/pull/245 🍒

kean commented 2 years ago

Hmm, I found a potential caveat with approach:

Nullable enum A nullable enum can be defined as follows:

type: string
nullable: true  # <---
enum:
  - asc
  - desc
  - null        # <--- without quotes, i.e. null not "null"

Note that null must be explicitly included in the list of enum values. Using nullable: true alone is not enough here.

From https://swagger.io/docs/specification/data-models/enums/

Unbelievable. I don't understand why. Also null and "null" are different things. I'm also wondering how this is going to work in JSON?? You can't do "["a", null]", as far as I know.

I have this exact issue in one of the specs I'm testing against (Soundcloud):

access:
    type: string
    nullable: true
    description: |
    Level of access the user (logged in or anonymous) has to the track.
        * `playable` - user is allowed to listen to a full track.
        * `preview` - user is allowed to preview a track, meaning a snippet is available
        * `blocked` - user can only see the metadata of a track, no streaming is possible
    enum:
    - playable
    - preview
    - blocked
    - null
kean commented 2 years ago

As far as I can tell, almost nobody adds "null" to their enums and just use nullable. I've seen thousands of examples of proper optional enums without null. But ideally it should be addressed somehow. Maybe StringOrNull decodable type?

mattpolzin commented 2 years ago

Good catch. I think this is what we get if we change it to [String?] but it's been a while so I'd have to try it out and see.

mattpolzin commented 2 years ago

This should take care of the problem you uncovered: https://github.com/mattpolzin/OpenAPIKit/pull/247

mattpolzin commented 2 years ago

You can't do "["a", null]", as far as I know.

Since in JavaScript everything is nullable, this is fine.

kean commented 2 years ago

Thanks! I updated to the latest OpenAPIKit version and it now works as expected.

I also just made my tool public - https://github.com/kean/CreateAPI. I hope to release it soon too.

mattpolzin commented 2 years ago

I also just made my tool public

Congrats! I haven't dug in more than the README yet, but that's a powerful sales pitch. Very exciting project!

You absolutely don't need to convince me of anything, but how would you compare your goals for CreateAPI to the execution of the SwagGen project as of now?

kean commented 2 years ago

The main difference is execution. I've tried multiple code generators, not just SwagGen. They all produced code that doesn't compile. I tested with the GitHub spec as an input - that's the primary spec I'm optimizing for. So that's already not hard to beat. But I go way beyond just making sure the code compiles.