openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Support for creating sets, instead of arrays, when uniqueItems is true

Open jasonlor opened this issue 1 year ago • 6 comments

Description

Is it possible to update the library to generate sets instead of arrays if uniqueItems: true?

jasonlor avatar Aug 23 '24 18:08 jasonlor

@jasonlor can you show an example of what you're looking for and what's the use case?

mrlubos avatar Aug 23 '24 18:08 mrlubos

Yep, so I'm using this library to generate types from my open API definition.

Here's a basic example:

      "TypeToBeGenerated": {
        "type": "array",
        "items": {
           "type": "string"
        },
        "uniqueItems": true
      },

In this situation, I'd like the following type as an output:

export type TypeToBeGenerated = Set<String> instead of export type TypeToBeGenerated = Array<String>

jasonlor avatar Aug 23 '24 18:08 jasonlor

But your API does not return a set, it returns an array... how do you use the response from the API when it's typed as Set? They don't have identical APIs

mrlubos avatar Aug 23 '24 19:08 mrlubos

When I construct a response or request, I want to guarantee uniqueness by removing duplicates. I suppose the alternative approach for my use case would be to remove duplicates if uniqueItems: true but still send arrays.

Wouldn't defining parameters as a set, be a better guarantee, even if it is transported as an array?

jasonlor avatar Aug 23 '24 21:08 jasonlor

While that is a viable approach for your use case, I'm not sure it makes sense to include as a configuration option for everyone. What would make sense, however, is giving you control over the generation step so that you can configure this behaviour for yourself rather than bake it into the package. Which OpenAPI version are you working with?

mrlubos avatar Aug 23 '24 23:08 mrlubos

That makes complete sense, I'm working with OpenAPI 3.0

jasonlor avatar Aug 27 '24 18:08 jasonlor

Added a RSVP label on this issue in case more people are interested in it. In the meantime, I encourage you to check out the WIP parser and see if you can figure out how you'd want to be able to "plug" into it and override this for your needs (I did not consider this use case yet)

mrlubos avatar Oct 20 '24 17:10 mrlubos