openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Enum of object in array uses union types

Open tommilligan opened this issue 4 years ago • 7 comments

If a schema response definition consists of:

  • an array
  • containing an object
  • which contains an enum field

This enum will be generated using union types, not an enum.

This does not happen for objects not nested in an array.

A minimal reproduction can be found here: https://github.com/tommilligan/openapi-typescript-codegen-861-reproduction


Actual output:

export type GetAllAnimalsResponse = {
    animals: Array<{
        type: 'frog' | 'badger',
    }>;
}

Expected output:

export type GetAllAnimalsResponse = {
    animals: Array<{
        type: GetAllAnimalsResponse.type,
    }>;
}

export namespace GetAllAnimalsResponse {
    export enum type {
        FROG = 'frog',
        BADGER = 'badger',
    }
}

tommilligan avatar Oct 19 '21 11:10 tommilligan

I'll look to see if I can submit a PR, any hint of where to start would be appreciated.

tommilligan avatar Oct 19 '21 11:10 tommilligan

After some poking, I suspect that this is due to this case here: https://github.com/ferdikoomen/openapi-typescript-codegen/blob/b532279969308937b879106fb41c372fbb24c90b/src/utils/registerHandlebarHelpers.ts#L44

I see that we only refer to parent when nesting as part of an interface - it seems logical that we'd also need to do some sort of parent referencing when nesting in arrays?

tommilligan avatar Oct 19 '21 12:10 tommilligan