zoubingwu / msw-auto-mock

A cli tool to generate random mock data from OpenAPI definition for msw.
246 stars 54 forks source link

[BUG] Nested objects with `allOf` are being generated as null #11

Closed doubleppereira closed 1 year ago

doubleppereira commented 1 year ago

msw-auto-mock seems to have Problems with return values based on components/models with nested objects which have allOf instead of type. This is a standard open-api document and it's being generated by Swashbuckle See below the schema of the PaymentResultApiResponse and the result prop.

Open-API Doc Example below

"/v1/checkout/get-payment-status": {
      "post": {
        "tags": [
          "Checkout",
          "RequiresAuthentication",
          "RequiresKycTier-0"
        ],
        "operationId": "GetPaymentStatus",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "allOf": [
                  {
                    "$ref": "#/components/schemas/GetPaymentStatusRequest"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/PaymentResultApiResponse"
                }
              }
            }
          },
          "429": {
            "description": "Too Many Requests"
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ApiResponse"
                }
              }
            }
          }
        },
        "security": [
          { }
        ]
      }
    },

// ...
  "components": {
    "schemas": {
        "PaymentResultApiResponse": {
            "required": [
            "isSuccessful"
            ],
            "type": "object",
            "properties": {
            "result": {
                "allOf": [ <--- is not being generated
                {
                    "$ref": "#/components/schemas/PaymentResult"
                }
                ],
                "nullable": true
            },
            "timestamp": {
                "type": "integer",
                "format": "int64",
                "readOnly": true
            },
            "isSuccessful": {
                "type": "boolean"
            },
            "errorMessage": {
                "type": "string",
                "nullable": true
            },
            "errorCode": {
                "type": "integer",
                "format": "int32",
                "nullable": true
            },
            "requestId": {
                "type": "string",
                "nullable": true
            }
            },
            "additionalProperties": false
        },

Output:

 ctx.json({
          result: null,
          timestamp: faker.datatype.number(),
          isSuccessful: faker.datatype.boolean(),
          errorMessage: faker.lorem.slug(1),
          errorCode: faker.datatype.number(),
          requestId: faker.lorem.slug(1),
        }),
doubleppereira commented 1 year ago

I tried to play a bit with the code but I got to a situation of circular dependency issue on some other cases when resolving allOf refs

zoubingwu commented 1 year ago

Thanks for your feedback, I'll look into it.

doubleppereira commented 1 year ago

Hi guys. Any update on this?

nbrdx commented 1 year ago

Hey there, just ran into the same problem. Also looking for updates 🙂

zoubingwu commented 1 year ago

@doubleppereira @nbrdx hey this should be fixed in v0.11.0, pls upgrade to latest version and try it again.