Skip to content
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

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

Open
llei4 opened this issue Jan 29, 2025 · 0 comments

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant