OpenAPITools / OpenAPITools/openapi-generator

[BUG] [php] [php-nextgen] Cannot redeclare getModelName

Open
#23,949 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

Models that contain a model_name field generate a duplicate getModelName function.
One is generated for getting the value from the actual field and one is generated for the ModelInterface.

openapi-generator version

Tested version v7.22.0 and master at revision 170778aa04dd372b05facfd2c504afe8d21cd1d2

OpenAPI declaration file content or url

I created an example project at https://github.com/jeroen1602/openapitools-generator-php-issues

Generation Details

I used the following commands for generation:

openapi-generator-cli generate
    -i ./api.yml
    -g php-nextgen
    -c ./generate-nextgen.json
    -o ./out
openapi-generator-cli generate
    -i ./api.yml
    -g php
    -c ./generate-old.json
    -o ./out
Steps to reproduce

Using the example project

  • Generate the nextgen version using ./genreate-nextgen.sh (this uses docker to generate the output)
  • Look into the NextGen/src/Model/UsedBy.php and search for the getModelName function.
  • Generate the old version using ./generate-old.sh (this also uses docker to generate the output)
  • Look into the Old/lib/Model/UsedBy.php and search for the getModelName function.
  • Use php -l to verify that this is a parse error.
Related issues/PRs

I found this issue but it was closed without anything happening #2544

Suggest a fix

The ModelInterface cannot be changed without making any breaking changes. So that is off the table.

I think that if a getter collision would be generated then it should change the name of the field getter to make sure this doesn't cause a collision.

For example for the model:

Example:
  type: object
  properties:
    model_name:
      type: string
    name:
      type: string

Should create the php model like this:

class Example implements ModelInterface, ArrayAccess, JsonSerializable
{
   // Some boilerplate left out

   /**
    * Implementation for {@link ModelInterface}
    */
   public function getModelName(): string
   {
       return self::$openAPIModelName;
   }

    /**
    * Gets name
    *
    * @return string
    */
   public function getName(): string
   {
       return $this->container['name'];
   }

   /**
    * Gets model_name
    *
    * @return string
    */
   public function getModelNameField(): string // <- renamed the getter by appending `Field` to it.
   {
       return $this->container['model_name'];
   }

}

This does run the risk of creating a new collision if the model_name_field field also exists. But I'm willing to bet that this chance is a lot more rare.

I'm willing to create a PR, but I'm not really sure about the fix for this. Some brainstorming would be appreciated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked example project, its api.yml, and generate-nextgen.sh or generate-old.sh, then inspect NextGen/src/Model/UsedBy.php and Old/lib/Model/UsedBy.php. Run php -l on the generated files and trace the generator entry point responsible for the getters. Done means a model containing model_name no longer declares duplicate getModelName methods and the generated PHP parses successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
54/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.