Skip to content

[Bug]: Potentially wrong syntax for enum arrays #852

@darkbasic

Description

@darkbasic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions