TIL: OpenAPI — nullable property in deepObject

If you want to have optional parameters in a schema with the deepObject style, you probably run into problems — at least with the league/openapi-psr7-validator. The easy fix or workaround for this, is to add nullable: true to the parent schema.

TIL: OpenAPI — nullable property in deepObject
A photo of a deepObject notation.

If you want to have optional parameters in a schema with the deepObject style, you probably run into problems — at least with the league/openapi-psr7-validator.

If your deepObject looks like this:

schema: new Schema(ref: ListFilter::class),
style: 'deepObject'

But some properties of ListFilter are nullable

#[OA\Property(nullable: true)]
?string $excludeMediaIds = ''

Then, this will result in an error:

League\\OpenAPIValidation\\PSR7\\Validators\\SerializedParameter::castToSchemaType():
Argument #2 ($type) must be of type ?string, array given, 
called in /var/www/html/vendor/league/openapi-psr7-validator/src/PSR7/Validators/SerializedParameter.php on line 290

The easy fix or workaround for this, is to add nullable: true to the parent schema:

schema: new Schema(ref: ListFilter::class, nullable: true),
style: 'deepObject'

… happy validating 🛡️