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

support nested any-of to use with openapi v3.1 type null

Open smarlhens opened this issue 2 years ago • 5 comments

Hi there 👋🏻

Thanks @ferdikoomen for your amazing job on this library 🙏🏻

I've tried my best to implement support for nested anyOf in order to use the type null rework on open api specification v3.1.

It seems related to https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1643.

I remain at your disposal for any feedback on the implementation.

I wish you a great day ☀️

Best regards ☮️

smarlhens avatar Aug 07 '23 20:08 smarlhens

I have a nested anyOf within an oneOf. Like:

"SomeDto": {
        "type": "object",
        "properties": {
          "value": {
            "oneOf": [
              {
                "type": "integer",
                "format": "int32"
              },
              {
                "type": "string"
              },
              {
                "type": "array",
                "items": {
                  "anyOf": [
                    {
                      "type": "string"
                    },
                    {
                      "type": "number"
                    }
                  ]
                }
              }
            ],
            "nullable": true
          }
        },
        "additionalProperties": false
      },

The generated api code seems to ignore the AnyOf, and generates the type as:

export type SomeDto = {
  value?: (number | string) | null;
};

instead of:

export type SomeDto = {
  value?: (number | string | Array<number | string>) | null;
};

does this PR also fix this issue?

PaulVrugt avatar Aug 10 '23 10:08 PaulVrugt

hey @PaulVrugt,

with this PR changes, it will generate:

export type SomeDto = {
    value?: (number | string | Array<(string | number)>) | null;
};

you can check on your machine if you want:

git clone [email protected]:smarlhens/openapi-typescript-codegen.git
git checkout feat/support-nested-any-of
npm install

then add at the end of components.schemas inside test/spec/v3.json:

"SomeDto": {
    "type": "object",
    "properties": {
        "value": {
            "oneOf": [
                {
                    "type": "integer",
                    "format": "int32"
                },
                {
                    "type": "string"
                },
                {
                    "type": "array",
                    "items": {
                        "anyOf": [
                            {
                                "type": "string"
                            },
                            {
                                "type": "number"
                            }
                        ]
                    }
                }
            ],
            "nullable": true
        }
    }
}

and then run

npm run test:update
git diff

you will see that inside test/__snapshots__/index.spec.ts.snap, in line to be added, the export type of SomeDto will be equal to the first snippet I wrote.

smarlhens avatar Aug 10 '23 11:08 smarlhens

Hi @smarlhens ,

so the short answer is yes right? :)

PaulVrugt avatar Aug 10 '23 11:08 PaulVrugt

@ferdikoomen can we get this merged? We are running into this in our code

PaulVrugt avatar Aug 10 '23 11:08 PaulVrugt

Hi @smarlhens ,

so the short answer is yes right? :)

Yes 👍🏻

smarlhens avatar Aug 10 '23 11:08 smarlhens