Skip to content

[BUG] [php] [php-nextgen] enumUnknownDefaultCase not deserialized #20593

@Ocarthon

Description

@Ocarthon

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the php or php-nextgen generator and enabling enumUnknownDefaultCase, the setter for for enum properties throws an exception if the value is not one of the defined ones. Instead, it should return the unknown enum value.

While the following example output is related to the php generator, the same applies to the php-nextgen generator.

Expected output
    public function setColor($color)
    {
        if (is_null($color)) {
            throw new \InvalidArgumentException('non-nullable color cannot be null');
        }
        $allowedValues = $this->getColorAllowableValues();
        if (!in_array($color, $allowedValues, true)) {
            $color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;
        }
        $this->container['color'] = $color;

        return $this;
    }
Actual output
    public function setColor($color)
    {
        if (is_null($color)) {
            throw new \InvalidArgumentException('non-nullable color cannot be null');
        }
        $allowedValues = $this->getColorAllowableValues();
        if (!in_array($color, $allowedValues, true)) {
            throw new \InvalidArgumentException(
                sprintf(
                    "Invalid value '%s' for 'color', must be one of '%s'",
                    $color,
                    implode("', '", $allowedValues)
                )
            );
        }
        $this->container['color'] = $color;

        return $this;
    }
openapi-generator version

7.12.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 2.0.0
  title: test
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      responses:
        '200':
          description: OK
components:
  schemas:
    Pet:
      type: object
      properties:
        Color:
          type: string
          enum: [RED, BLUE, GREEN]
Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i /local/pet.yaml \
    -o /local/ \
    -g php \
    --additional-properties="enumUnknownDefaultCase=true"
Steps to reproduce

generate code

Related issues/PRs
Suggest a fix

Apply the following diff in php/model_generic.mustache and php-nextgen/model_generic.mustache

        if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
+            {{#enumUnknownDefaultCase}}
+                {{#allowableValues}}{{#enumVars}}{{#-last}}return self::{{enumName}}_{{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}
+            {{/enumUnknownDefaultCase}}
+            {{^enumUnknownDefaultCase}}
            throw new \InvalidArgumentException(
                sprintf(
                    "Invalid value '%s' for '{{name}}', must be one of '%s'",
                    ${{{name}}},
                    implode("', '", $allowedValues)
                )
            );
+            {{/enumUnknownDefaultCase}}
        }

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