I am using API Platform / Symfony (latest version) to manage my API.
Then, I use the @rtk-query/codegen-openapi library to generate my TypeScript typings. The problem is that many fields are marked as non-required even though they are supposed to be required, especially the IDs :
export type UserMeasurementJsonldUserMeasurementRead = {
"@context"?:
| string
| {
"@vocab": string;
hydra: "http://www.w3.org/ns/hydra/core#";
[key: string]: any;
};
"@id"?: string;
"@type"?: string;
id?: string;
date: string;
userDescription?: string | null;
createdAt?: string;
createdBy?: UserJsonldUserMeasurementRead;
};
I understand that only the 'date' field is required, since I added a NotNull assertion, but it seems odd that such important fields like 'id' are optional. Below is the schema from Swagger:
"UserMeasurement.jsonld-UserMeasurement.read":{
"type":"object",
"description":"",
"deprecated":false,
"properties":{
"@id":{
"readOnly":true,
"type":"string"
},
"id":{
"readOnly":true,
"type":"string",
"format":"uuid"
},
"date":{
"type":"string",
"format":"date-time"
},
"userDescription":{
"type":[
"string",
"null"
]
},
"createdAt":{
"type":"string",
"format":"date-time"
},
"createdBy":{
"$ref":"#\/components\/schemas\/User.jsonld-UserMeasurement.read"
}
},
"required":[
"date"
]
}
Is it possible to handle this without having to put NonNull everywhere, especially on the IDs? Here is my PHP entity, even though it’s very basic:
class UserMeasurement
{
#[ORM\Id]
#[ORM\Column(type: "uuid", unique: true)]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
#[Groups(groups: ['UserMeasurement:read'])]
protected UuidInterface $id;
#[ORM\Column(type: Types::DATE_MUTABLE, name: '_date')]
//#[Assert\DateTime] // dump(assert non fonctionnel)
#[Assert\NotNull]
#[Groups(groups: ['UserMeasurement:read', 'UserMeasurement:write'])]
private ?\DateTimeInterface $date = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Localizable]
#[Assert\NotBlank(allowNull: true)]
#[Groups(groups: ['UserMeasurement:read', 'UserMeasurement:write'])]
private ?string $userDescription = null;
#[ORM\Column]
#[Groups(groups: ['UserMeasurement:read'])]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
#[Groups(groups: ['UserMeasurement:read'])]
private ?User $createdBy = null;
...
}