Skip to content

M2.3.2 : Nullable getters in Service Contracts will throw a reflection error when used in the web API  #25656

Closed
@kanduvisla

Description

@kanduvisla

Preconditions

  1. Magento 2.3.2 & 2.3-develop

Steps to reproduce

Create an interface with a nullable getter. In our scenario it was a method that could return an array of instances of another object, or null:

/**
 * @return null|\Vendor\Module\Api\Data\CompanyAddressInterface[]
 */
public function getAddresses(): ?array;

Expected result

When properly configured and used as a web API, the object should be serialized using Reflection and it's nested objects as well.

Actual result

The following error is thrown:

image

Possible cause

My guess is that when determinating the type of return class, something goes wrong when a return type can be nullable. See also the workaround:

Workaround

The current woraround that will not trigger this error and make the web API work as expected is to flip the arguments, so instead of:

/**
 * @return null|\Vendor\Module\Api\Data\CompanyAddressInterface[]
 */
public function getAddresses(): ?array;

I use:

/**
 * @return \Vendor\Module\Api\Data\CompanyAddressInterface[]|null
 */
public function getAddresses(): ?array;

Now everything works as expected, but it seems to me that this should be considered a bug.

Metadata

Metadata

Assignees

Labels

Component: ApiUse with concrete module component label E.g. "Component: Api" + "Catalog"Event: cd-cologne19Fixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions