Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
Ocarthon opened this issue Feb 4, 2025 · 0 comments · Fixed by #20594
Closed
5 of 6 tasks

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

Ocarthon opened this issue Feb 4, 2025 · 0 comments · Fixed by #20594

Comments

@Ocarthon
Copy link
Contributor

Ocarthon commented Feb 4, 2025

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}}
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant