whenNotNull in JsonResource is required when type of property is Carbon
#943
-
|
Hi everyone, I'm having an issue with JsonResource when it comes to Schema::create('my_models', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('nickname')->nullable();
$table->date('dob')->nullable();
});
// --------------------------------------------------------
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $name
* @property null|string $nickname
* @property null|\Carbon\Carbon $dob
*/
class MyModel extends Model
{
protected $table = 'my_models';
protected function casts(): array
{
return [
'dob' => 'datetime:Y-m-d',
];
}
}This is the associated resource: /**
* @property \App\Models\MyModel $resource
*/
class MyResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->resource->id,
'name' => $this->resource->name,
// This works fine
'nickname' => $this->whenNotNull($this->resource->nickname),
// This says it is required
'dateOfBirth' => $this->whenNotNull($this->resource->dob),
];
}
}"MyResource": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"nickname": {
"type": "string"
},
"dateOfBirth": {
"anyOf": [
{
"type": "string",
"format": "date-time"
},
{
"type": "object" // <-- This is also unexpected
}
]
}
},
"required": [
"id",
"name",
"dateOfBirth" // <-- Why is it required?
],
"title": "MyResource"
}For this purpose, I created a specific branch on this repo: https://github.com/apasquini95/demo_scramble/tree/date_resource_when_not_null Is it intended or am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
@apasquini95 this is a bug. Thanks for the reproduction. Will fix. |
Beta Was this translation helpful? Give feedback.
@apasquini95 this is a bug. Thanks for the reproduction. Will fix.