Apiato: Removal of "Transporters"

Created on 5 Mar 2021  ·  3Comments  ·  Source: apiato/apiato

Is your feature request related to a problem?

Transporters are just not needed and don't add any meaningful value because:

  • We have request validation through Laravel rules. and transporters just (in part) do the same.
  • Unneeded dependency on DTO pkg.
  • Increase in maintenance costs. (e.g. maintaining Transporters for every Request becomes cumbersome when are having many routes)

Describe the solution you'd like

Removal of "Transporters" and use Request object instead.

Describe alternatives you've considered

None. Suggestions are welcome.

Additional context or How I came to this conclusion

Take this example:

  • Transporters solve this (as written in Apiato docs):
    > They are very useful for reducing the number of parameters in functions, which prevents the duplication of the long parameters.
  • As Porto docs dictates:
    > A Task SHOULD NOT accept a Request object in any of its functions. It can take anything in its functions parameters but never a Request object. This will keep it free to use from anywhere, and can be tested independently.

Let's say we reached the point where we need to pass data from Action -> Task and we have 30 fields in either Transporter or Request which doesn't matter and our Task needs all those 30 fields.

What can we do here? We can do one of this:

  • Pass the Transporter/Request object (which are interchangeable because both have the same data) - _Forbidden by Porto Pattern_
  • Pass 30 fields as arguments one by one.
  • Pass array of data using $transporter->toArray() or $request->all() (best solution IMO)

As you can see here, Transporters actually don't solve anything at all, because all Request data are just present in Transporters as well and they are basically the same data wise. And all fields that are in Request usually end up in Transporters (allowed) properties.

Here look at a more Transporterish example:

Note: We just can't set additionalProperties => false right now as explained here.
This is just an example of Transporter bugs which are many.

But for this examples sake let's say we can set additionalProperties => false

        'properties' => [
            'first_field',
            'second_field',
            'third_field',
        ],
        // allow for undefined properties
        'additionalProperties' => false,

to get this exact functionality we just can use:

        $data = $data->sanitizeInput([
            'first_field',
            'second_field',
            'third_field',
        ]);

and to get the functionality of 'additionalProperties' => true, well you get it already with Request object $request->all() and even if you want to sanitize known fields + have unknow fields (additionalProperties) we can do something about that by implementing something like this $request->sanitizeInputAndKeepAdditionalProperties() (I'm not good at naming :|)

Discussion Proposal

Most helpful comment

This is possible to remove transporters layer and add its advantages (Like default values) to Request concept!
This is a good decision for sake of simplicity and avoiding code duplication, because we all know naming all input fields in 2 places (transporter and request) is not a good practice!

All 3 comments

This is possible to remove transporters layer and add its advantages (Like default values) to Request concept!
This is a good decision for sake of simplicity and avoiding code duplication, because we all know naming all input fields in 2 places (transporter and request) is not a good practice!

I've never used transporter in my project.
I always add AdditionalProperties in request with $this->offsetSet('someKey', $someValue);.

eg.

    /**
     * @return  array
     */
    public function rules()
    {
        return [
            'id'            => [
                'required',
                function ($attribute, $value, $fail)
                {
                    $innerAllotOrder = InnerAllotOrder::query()
                        ->where([
                            ['id', $value],
                        ])
                        ->first();

                    if (!$innerAllotOrder) {
                        return $fail($this->attributes()[$attribute].'不存在');
                    }

                    if ($innerAllotOrder->status != InnerAllotOrder::STATUS_SUBMIT) {
                        return $fail($this->attributes()[$attribute].'已调拨,不支持更新');
                    }

                    $this->offsetSet('innerAllotOrder', $innerAllotOrder);
                }
            ],
            'remark'            => 'string',
        ];
    }

Since Transporters finally have been removed in version 10.0 I close this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vituocgia picture vituocgia  ·  5Comments

overbid picture overbid  ·  4Comments

san4io picture san4io  ·  7Comments

FWidm picture FWidm  ·  3Comments

phuocnt0612 picture phuocnt0612  ·  3Comments