orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.18k stars 336 forks source link

MSW: Error using oneOf nested inside allOf #1526

Open C-Higgins opened 4 months ago

C-Higgins commented 4 months ago

Minimal input schema:

{
  "openapi": "3.0.1",
  "info": {
    "title": "bug report"
  },
  "paths": {
    "/something": {
      "post": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "uploads": {
                      "type": "array",
                      "items": {
                        "allOf": [
                          {
                            "type": "object",
                            "properties": {
                              "file_type": {
                                "type": "string"
                              }
                            }
                          },
                          {
                            "oneOf": [
                              {
                                "type": "object",
                                "properties": {
                                  "type": {
                                    "type": "string",
                                    "enum": ["a"]
                                  }
                                }
                              },
                              {
                                "type": "object",
                                "properties": {
                                  "type": {
                                    "type": "string",
                                    "enum": ["b"]
                                  },
                                  "other": {
                                    "type": "string"
                                  }
                                }
                              }
                            ]
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Output with syntax error:

export const getPostSomethingResponseMock = (overrideResponse: Partial< PostSomething200 > = {}): PostSomething200 => ({uploads: faker.helpers.arrayElement([Array.from({ length: faker.number.int({ min: 1, max: 10 }) }, (_, i) => i + 1).map(() => ({file_type: faker.helpers.arrayElement([faker.word.sample(), undefined]),{type: faker.helpers.arrayElement([faker.helpers.arrayElement(['a'] as const), undefined])},{other: faker.helpers.arrayElement([faker.word.sample(), undefined]), type: faker.helpers.arrayElement([faker.helpers.arrayElement(['b'] as const), undefined])}})), undefined]), ...overrideResponse})

Tested on orval 6.29.1 and 6.31.0. The code in the schema file and the API file is correct; only the MSW file has this problem.

This seems to only happen with a oneOf nested inside an allOf.

melloware commented 4 months ago

Similar to this issue: https://github.com/anymaniax/orval/issues/1508

And this one: https://github.com/anymaniax/orval/issues/1101