swagger-api / swagger-api/swagger-codegen

[PHP] Generates invalid code due to property renaming

Open
#4,551 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: PHP help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Generated PHP code can create invalid PHP code when converting property names to methods, if the property names are similar except one also contains one or more underscore characters. For example, helloworld and hello_world.

After generating both petstore and testpetstore (see below), here's part of the diff:

SwaggerClient-php/lib/Model/Pet.php

@@ -98,6 +102,8 @@
     protected static $setters = array(
         'id' => 'setId',
         'name' => 'setName',
+        'hello_world' => 'setHelloWorld',
+        'helloworld' => 'setHelloworld',
         'tag' => 'setTag'
     );

@@ -113,6 +119,8 @@
     protected static $getters = array(
         'id' => 'getId',
         'name' => 'getName',
+        'hello_world' => 'getHelloWorld',
+        'helloworld' => 'getHelloworld',
         'tag' => 'getTag'
     );

In other words, Pet.php redeclares getHelloWorld() and setHelloWorld() twice in Pet.php, which is a fatal error:

     /**
+     * Gets hello_world
+     * @return string
+     */
+    public function getHelloWorld()
+    {
+        return $this->container['hello_world'];
+    }
+
+    /**
+     * Sets hello_world
+     * @param string $hello_world
+     * @return $this
+     */
+    public function setHelloWorld($hello_world)
+    {
+        $this->container['hello_world'] = $hello_world;
+
+        return $this;
+    }
+
+    /**
+     * Gets helloworld
+     * @return string
+     */
+    public function getHelloworld()
+    {
+        return $this->container['helloworld'];
+    }
+
+    /**
+     * Sets helloworld
+     * @param string $helloworld
+     * @return $this
+     */
+    public function setHelloworld($helloworld)
+    {
+        $this->container['helloworld'] = $helloworld;
+
+        return $this;
+    }
+
+    /**
      * Gets tag
      * @return string
      */

It's not important that the method's case is slightly different. This also means tests break due to redeclared methods:

SwaggerClient-php/test/Model/PetTest.php

+     * Test attribute "hello_world"
+     */
+    public function testPropertyHelloWorld()
+    {
+
+    }
+
+    /**
+     * Test attribute "helloworld"
+     */
+    public function testPropertyHelloworld()
+    {
+
+    }
+
+    /**

I do see that a ensureUniqueParams configuration option exists but it's different. Issue #2766 refers to modelPropertyNaming but PHP does not have this, nor do I think using such an option should be required to fix this. PHP does have variableNamingConvention but that's for variables.

Swagger-codegen version

2.2.1

Swagger declaration file content or url

This can be reproduced using a slightly modified petstore.json with the following change:

--- petstore.json	2017-01-12 13:10:54.000000000 -0800
+++ testpetstore.json	2017-01-12 13:19:08.000000000 -0800
@@ -123,6 +123,12 @@
         "name": {
           "type": "string"
         },
+        "hello_world": {
+          "type": "string"
+        },
+        "helloworld": {
+          "type": "string"
+        },
         "tag": {
           "type": "string"
         }
Command line used for generation

swagger-codegen generate
--input-spec ./testpetstore.json
--lang php
--output /tmp/testpetstore

Steps to reproduce
  1. Add two property names where the only difference is one contains an underscore, such as helloworld and hello_world.
  2. Generate PHP code
  3. Execute code or tests
  4. See PHP Fatal error: Cannot redeclare ...
Suggest a Fix

Check for conflicts before generating the code. If two generated method names will be the same, then make one different somehow. I do not know the renaming logic behind ensureUniqueParams but suspect the same or similar logic could be used for methods.

It might be worth having generate echo a warning/notice when methods end up being renamed.

Related

Might be related to Issue #2766

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

Reproduce the issue with the modified petstore.json using the documented swagger-codegen generate command and inspect SwaggerClient-php/lib/Model/Pet.php. Check the generated methods and SwaggerClient-php/test/Model/PetTest.php for collisions between hello_world and helloworld. Done means the generated PHP no longer redeclares methods and the generated tests run without the fatal error.

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
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.