Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
llei4 opened this issue Jan 29, 2025 · 1 comment

Comments

@llei4
Copy link

llei4 commented Jan 29, 2025

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.

Copy link

stale bot commented Mar 30, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 30, 2025
@stale stale bot closed this as completed Apr 7, 2025
@soyuka soyuka added bug and removed stale labels Apr 8, 2025
@soyuka soyuka reopened this Apr 8, 2025
@soyuka soyuka added the Laravel label May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants