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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

smarlhens
Copy link

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 #1643.

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

I wish you a great day โ˜€๏ธ

Best regards โ˜ฎ๏ธ

@PaulVrugt
Copy link

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?

@smarlhens
Copy link
Author

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 git@github.com: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.

@PaulVrugt
Copy link

Hi @smarlhens ,

so the short answer is yes right? :)

@PaulVrugt
Copy link

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

@smarlhens
Copy link
Author

Hi @smarlhens ,

so the short answer is yes right? :)

Yes ๐Ÿ‘๐Ÿป

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants