scramble icon indicating copy to clipboard operation
scramble copied to clipboard

[Bug]: Potentially wrong syntax for enum arrays

Open darkbasic opened this issue 7 months ago • 0 comments

What happened?

The generated JSON specification for enum arrays is potentially incorrect.

How to reproduce the bug

Take the following code:

class AnalyticsDataRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'metrics' => ['nullable', 'array', Rule::in(MetricsEnum::values())],
            // ...
        ];
    }
}

It generates the following OpenAPI json:

{
    "name": "metrics",
    "in": "query",
    "required": true,
    "schema": {
        "type": "array",
        "enum": [
            "product_requirements_mandatory_completed",
            "product_requirements_optional_completed",
            "product_requirements_total_completed"
        ],
        "items": {
            "type": "string"
        },
        "minItems": 1
    }
},

In Stoplight it generates an enum array, as expected. In openapi-typescript on the other hand it generates a union instead.

An openapi-typescript developer believes that the JSON specification is incorrect and should be the following instead:

{
  "name": "metrics",
  "in": "query",
  "required": true,
  "schema": {
    "type": "array",
    "items": {
      "type": "string",
      "enum": [
        "product_requirements_mandatory_completed",
        "product_requirements_optional_completed",
        "product_requirements_total_completed"
      ]
    },
    "minItems": 1
  }
}

This syntax works in both openapi-typescript and Stoplight.

Package Version

0.12.19

PHP Version

Will fill out later

Laravel Version

Will fill out later

Which operating systems does with happen with?

Linux

Notes

No response

darkbasic avatar May 27 '25 17:05 darkbasic