Skip to content

bug: Method parameter injection fails for hyphenated request param names #106

@usernane

Description

@usernane

Problem Statement

When using #[ResponseBody] with method parameter injection, the framework resolves parameters by the PHP variable name, not the registered request parameter name. This breaks for any hyphenated parameter name (which is the standard convention in WebFiori).

Steps to Reproduce

#[RestController('my-service')]
class MyService extends WebService {

    #[GetMapping]
    #[ResponseBody]
    #[AllowAnonymous]
    #[RequestParam(name: 'app-id', type: ParamType::INT)]
    public function myMethod(int $appId): Json {
        // $appId is always null
        return new Json(['id' => $appId]);
    }
}

Calling with ?app-id=5 results in $appId being null (or a TypeError if not nullable).

Root Cause

In WebService::getMethodParameters():

foreach ($reflection->getParameters() as $param) {
    $paramName = $param->getName(); // returns 'appId' (PHP variable name)
    $value = $this->getParamVal($paramName); // looks for 'appId', but param is registered as 'app-id'
    $params[] = $value;
}

The PHP variable name appId doesn't match the registered request parameter name app-id, so getParamVal() returns null.

Expected Behavior

The framework should convert the camelCase PHP variable name to the hyphenated form (e.g., appIdapp-id) before calling getParamVal(), or try both forms. This is consistent with how WebFiori uses hyphens as the convention for parameter and column names throughout.

Workaround

Access hyphenated params manually inside the method body:

public function myMethod(string $username): Json {
    $appId = $this->getParamVal('app-id'); // works
    // ...
}

Only non-hyphenated params (like username) can be injected as method parameters.

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