Skip to content

[BUG] [php] Property is both nullable and non-nullable #21497

@florentausha

Description

@florentausha
Description

Hi!

I'm having an issue with the php generator.
One of my properties have been set in required properties and nullable.
However, I can't set this value to null when using the generated PHP class:

$test = (new GrantShowsInner())
            ->setImageUrl(null)
            ->setName('coucou');

        dd($test->listInvalidProperties());

This returns

array:1 [
  0 => "'image_url' can't be null"
]

Since the value is nullable and required, setting it to null should validate the schema requirements.

openapi-generator version

I'm using v7.13.0

OpenAPI declaration file content or url

This is a sample of my 3.1.0 spec:

    GrantShows:
      type: array
      items:
        properties:
          image_url:
            $ref: './shows.yaml#/components/schemas/ShowImageUrl'
          name:
            $ref: './shows.yaml#/components/schemas/ShowName'
        required:
          - image_url
          - name
        type: object
    ShowImageUrl:
      description: URL leading to the image of the show.
      type:
        - string
        - "null"
Generation Details

Generation script:

#!/bin/bash

cd "$(dirname "$0")"

rm -rf ./generated-openapi
rm -rf ./app/OpenApi

# Iterate over yaml files in /local/public/docs and generate classes for each
for file in ./public/docs/*.yaml; do
    filename=$(basename -- "$file")
    echo "Generating classes for $filename"
    docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v7.13.0 generate \
        -i "/local/public/docs/$filename" \
        -g php \
        --global-property=apiDocs=false,apiTests=false,modelsDocs=false,modelTests=false \
        --invoker-package "App\OpenApi" \
        -o /local/generated-openapi
    if [ $? -ne 0 ]; then
        echo "Error generating classes for $filename. Stopping script."
        exit 1
    fi
done

# Copy generated files to project
cp -r ./generated-openapi/lib ./app/OpenApi
rm -rf ./generated-openapi

# Discard files that we won't use
rm -rf ./app/OpenApi/Api
Suggest a fix

I think that function listInvalidProperties is incorrect:

    /**
     * Show all the invalid properties with reasons.
     *
     * @return array invalid properties with reasons
     */
    public function listInvalidProperties()
    {
        $invalidProperties = [];

        if ($this->container['image_url'] === null) {
            $invalidProperties[] = "'image_url' can't be null";
        }
        if ($this->container['name'] === null) {
            $invalidProperties[] = "'name' can't be null";
        }
        return $invalidProperties;
    }

Since property image_url is in the list of nullable properties:

    /**
      * Array of nullable properties. Used for (de)serialization
      *
      * @var boolean[]
      */
    protected static array $openAPINullables = [
        'image_url' => true,
        'name' => false
    ];

Thanks for your help on this matter!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions