diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java index db4c29e32bc..3be05eede2a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java @@ -142,11 +142,18 @@ public class PhpNextgenClientCodegen extends AbstractPhpCodegen { CodegenModel model = m.getModel(); for (CodegenProperty prop : model.vars) { + String propType; if (prop.isArray || prop.isMap) { - prop.vendorExtensions.putIfAbsent("x-php-prop-type", "array"); + propType = "array"; } else { - prop.vendorExtensions.putIfAbsent("x-php-prop-type", prop.dataType); + propType = prop.dataType; } + + if ((!prop.required || prop.isNullable)) { // optional or nullable + propType = "?" + propType; + } + + prop.vendorExtensions.putIfAbsent("x-php-prop-type", propType); } if (model.isEnum) { diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache index a4f1c451686..23367d5ca07 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache @@ -381,9 +381,9 @@ class ObjectSerializer * @param string[]|null $httpHeaders HTTP headers * @param string|null $discriminator discriminator if polymorphism is used * - * @return object|array|null a single or an array of $class instances + * @return mixed a single or an array of $class instances */ - public static function deserialize(mixed $data, string $class, string $httpHeaders = null): object|array|null + public static function deserialize(mixed $data, string $class, array $httpHeaders = null): mixed { if (null === $data) { return null; diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache index 5bf030f6d79..0a199d959f7 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache @@ -367,7 +367,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par * @deprecated {{/deprecated}} */ - public function {{getter}}(): {{^required}}?{{/required}}{{vendorExtensions.x-php-prop-type}} + public function {{getter}}(): {{vendorExtensions.x-php-prop-type}} { return $this->container['{{name}}']; } @@ -382,7 +382,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par * @deprecated {{/deprecated}} */ - public function {{setter}}({{^required}}?{{/required}}{{vendorExtensions.x-php-prop-type}} ${{name}}): static + public function {{setter}}({{vendorExtensions.x-php-prop-type}} ${{name}}): static { {{#isNullable}} if (is_null(${{name}})) { diff --git a/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php b/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php index d3816c8b64c..edf8a5c60b4 100644 --- a/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php +++ b/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php @@ -391,9 +391,9 @@ class ObjectSerializer * @param string[]|null $httpHeaders HTTP headers * @param string|null $discriminator discriminator if polymorphism is used * - * @return object|array|null a single or an array of $class instances + * @return mixed a single or an array of $class instances */ - public static function deserialize(mixed $data, string $class, string $httpHeaders = null): object|array|null + public static function deserialize(mixed $data, string $class, array $httpHeaders = null): mixed { if (null === $data) { return null; diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php index df5405d178a..72733e8b5d2 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php @@ -390,9 +390,9 @@ class ObjectSerializer * @param string[]|null $httpHeaders HTTP headers * @param string|null $discriminator discriminator if polymorphism is used * - * @return object|array|null a single or an array of $class instances + * @return mixed a single or an array of $class instances */ - public static function deserialize(mixed $data, string $class, string $httpHeaders = null): object|array|null + public static function deserialize(mixed $data, string $class, array $httpHeaders = null): mixed { if (null === $data) { return null; diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/AuthTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/AuthTest.php index daafb50ede7..5a4755bcc61 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/AuthTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/AuthTest.php @@ -33,7 +33,7 @@ class AuthTest extends TestCase $fakeHttpClient = new FakeHttpClient(); $api = new PetApi($fakeHttpClient, $authConfig); - $api->addPet(new Pet()); + $api->addPet(new Pet(array('name' => 'testing', 'photo_urls' => array('http://a.com')))); $headers = $fakeHttpClient->getLastRequest()->getHeaders(); diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/NullableTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/NullableTest.php index adac640ce87..c80812db339 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/NullableTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/NullableTest.php @@ -20,10 +20,16 @@ class NullableTest extends TestCase $name->setName(1); $this->assertEquals(1, $name->getName(), 'Non-nullable property can be set and retains its value'); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('non-nullable name cannot be null'); + // comment out below as strict type is now enabled + //$this->expectException(InvalidArgumentException::class); + //$this->expectExceptionMessage('non-nullable name cannot be null'); + $this->expectException(TypeError::class); + $this->expectExceptionMessage('must be of type int, null given'); + + //Failed asserting that exception of type "TypeError" matches expected exception "InvalidArgumentException". Message was: "OpenAPI\Client\Model\Name::setName(): Argument #1 ($name) must be of type int, null given, called in /Users/williamcheng/Code/openapi-generator7/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/NullableTest.php on line 26" at $name->setName(null); + } public function testNullableobject(): void @@ -36,4 +42,4 @@ class NullableTest extends TestCase $nullable->setIntegerProp(1); $this->assertEquals(1, $nullable->getIntegerProp(), 'Nullable property can be set and retains its value'); } -} \ No newline at end of file +} diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php index e7a8b97419a..15202763d20 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php @@ -136,6 +136,7 @@ class PetApiTest extends TestCase $updatedPet->setId($petId); $updatedPet->setName('updatePet'); $updatedPet->setStatus('pending'); + $updatedPet->setPhotoUrls(array('http://a.com')); $result = $this->api->updatePet($updatedPet); $this->assertNull($result); @@ -186,6 +187,7 @@ class PetApiTest extends TestCase $newPet = new Model\Pet; $newPet->setId($new_pet_id); $newPet->setName("PHP Unit Test 2"); + $newPet->setPhotoUrls(array("http://a.com")); // add a new pet (model) $add_response = $this->api->addPet($newPet); @@ -196,6 +198,7 @@ class PetApiTest extends TestCase $response = $this->api->getPetById($new_pet_id); $this->assertSame($new_pet_id, $response->getId()); $this->assertSame('PHP Unit Test 2', $response->getName()); + $this->assertSame(array("http://a.com"), $response->getPhotoUrls()); } /* diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ServerVariablesInOperationTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ServerVariablesInOperationTest.php index 208ae7ed329..385b895f0dc 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ServerVariablesInOperationTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ServerVariablesInOperationTest.php @@ -22,6 +22,8 @@ class ServerVariablesInOperationTest extends TestCase $this->fakeHttpClient = new FakeHttpClient(); $this->api = new Api\PetApi($this->fakeHttpClient); $this->pet = new Model\Pet(); + $this->pet->setName("something"); + $this->pet->setPhotoUrls(array("https://a.com")); } public function testServerVariablesInOperation(): void diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/StoreApiTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/StoreApiTest.php index 4bb85919dc5..51d64b8a93a 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/StoreApiTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/StoreApiTest.php @@ -30,6 +30,7 @@ class StoreApiTest extends TestCase $pet = new Pet(); $pet->setId($id); $pet->setName('PHP Unit Test'); + $pet->setPhotoUrls(array('http://a.com')); $pet->setStatus('available'); // new tag $tag = new Tag();