Skip to content

[Laravel] Swagger always suggests parameters using camelCase which collision with validation rules #6932

Open
@llei4

Description

@llei4

API Platform version(s) affected: 4.0.16

Description

Swagger always suggests parameters using camelCase. This works good on "simple" Models because both CamelCase and snake_case parameters are accepted and treated in the same way after camelCase parameters are normalized to snake_case when treating the input data.

However, this collisions when definining validation request rules which expects parameters to be camel_case in order to link them to Database Fields.

How to reproduce
Having 1 Model:

<?php

namespace App\Models;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use Illuminate\Database\Eloquent\Model;

#[ApiResource(
    operations:[
        new Post()
    ],
)]
class Test extends Model
{
    protected $table = 'test';
}

Created using folliwng migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{

    public function up(): void
    {
        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('sur_name');
            $table->timestamps();
        });

    }0

    public function down(): void
    {
        Schema::dropIfExists('test');
    }
};

The suggestion/Example on Swagger is:

Image

When trying this call, Model is saved properly. GOOD.

If then I add some validation rules on the Model on sur_name field:

...
#[ApiResource(
    operations:[
        new Post()
    ],
    rules: [
        'sur_name' => 'required'
    ]
)]
class Test extends Model
{
...

And calling again POST with the suggested Payload I receive:

Image

I then I swtich the Payload using sur_name instead of surName so:

Image

The Model is persisted properly.

Possible Solution

Maybe one solution would be Swagger always suggesting fields using snake_case, otherwise these confusions may happen.

I am not sure if I am doing something very wrong using camelCase vs snake_case notation on Models but this is something very related to #6927. Maybe some explanations how camelCase vs snake_case parameters/attributes are treated would be useful @soyuka.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions