DoclerLabs / api-client-generator

API client generator is a console application capable of generating an API client based on OpenAPI(Swagger) specification.
MIT License
34 stars 19 forks source link

Nullable properties with length validations trigger errors on PHP 8.1 #90

Closed negruhorea closed 1 year ago

negruhorea commented 1 year ago

The following snipped

ObjectType:
      type: object
      properties:
        title:
          type: string
          maxLength: 255
          example: Lorem Ipsum
          nullable: true    

Produces:

public function setTitle(?string $title): self
    {
        if (\grapheme_strlen($title) > 255) {
            throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Length should be less than 255.', 'title', $title));
        }
        $this->title                            = $title;
        $this->optionalPropertyChanged['title'] = true;

        return $this;
    }

When $title argument is null then PHP 8.1 triggers an error.

A solution would be to check if the $title is not null before applying validation.