You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In many cases, CreateSomethingInput and CreateSomethingCommand have exactly the same fields and the same validation constraints. The processor then mostly does mechanical
mapping:
$command = new CreateSomethingCommand(
fieldA: $input->fieldA,
fieldB: $input->fieldB,
...
);
In that context, do you consider it acceptable to use the application Command directly as the API Platform input?
With Symfony Validator constraints directly on the Command:
final class CreateSomethingCommand
{
public function __construct(
#[Assert\NotBlank]
public ?string $name = null,
#[Assert\NotNull]
#[Assert\PositiveOrZero]
public ?int $quantity = null,
) {}
}
The Processor would only add server-side context when needed, such as the current user, organization, tenant, IP address, etc., then dispatch the Command.
Pros I see:
less boilerplate
less duplication between API input DTO and Command
less mapping with no business value
the use case input contract is easier to read
Possible drawbacks:
the application layer depends on Symfony Validator attributes
the Command also becomes part of the HTTP contract
if the API contract diverges from the use case later, we may need to reintroduce a dedicated API DTO
it blurs the strict separation between HTTP adapter and application layer
What do you usually do in production Symfony/API Platform projects?
Always use a dedicated API input DTO, then map to a Command?
Use the Command directly as API Platform input?
Use a hybrid rule: Command-as-input when it is 1:1, dedicated DTO when the API contract diverges?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
For a custom POST operation in API Platform, we often end up with this flow:
In many cases,
CreateSomethingInputandCreateSomethingCommandhave exactly the same fields and the same validation constraints. The processor then mostly does mechanicalmapping:
In that context, do you consider it acceptable to use the application Command directly as the API Platform
input?Example:
With Symfony Validator constraints directly on the Command:
The Processor would only add server-side context when needed, such as the current user, organization, tenant, IP address, etc., then dispatch the Command.
Pros I see:
Possible drawbacks:
What do you usually do in production Symfony/API Platform projects?
input?Beta Was this translation helpful? Give feedback.
All reactions