Skip to content

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 [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.

@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.

2 participants