-
Notifications
You must be signed in to change notification settings - Fork 6k
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
- Add two property names where the only difference is one contains an underscore, such as
helloworldandhello_world. - Generate PHP code
- Execute code or tests
- 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