fix type, update tests in php nextgen (#16758)

This commit is contained in:
William Cheng 2023-10-09 14:40:34 +08:00 committed by GitHub
parent 5fb6fcf9ef
commit 48f454cf72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 33 additions and 14 deletions

View File

@ -142,11 +142,18 @@ public class PhpNextgenClientCodegen extends AbstractPhpCodegen {
CodegenModel model = m.getModel(); CodegenModel model = m.getModel();
for (CodegenProperty prop : model.vars) { for (CodegenProperty prop : model.vars) {
String propType;
if (prop.isArray || prop.isMap) { if (prop.isArray || prop.isMap) {
prop.vendorExtensions.putIfAbsent("x-php-prop-type", "array"); propType = "array";
} else { } 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) { if (model.isEnum) {

View File

@ -381,9 +381,9 @@ class ObjectSerializer
* @param string[]|null $httpHeaders HTTP headers * @param string[]|null $httpHeaders HTTP headers
* @param string|null $discriminator discriminator if polymorphism is used * @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) { if (null === $data) {
return null; return null;

View File

@ -367,7 +367,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
* @deprecated * @deprecated
{{/deprecated}} {{/deprecated}}
*/ */
public function {{getter}}(): {{^required}}?{{/required}}{{vendorExtensions.x-php-prop-type}} public function {{getter}}(): {{vendorExtensions.x-php-prop-type}}
{ {
return $this->container['{{name}}']; return $this->container['{{name}}'];
} }
@ -382,7 +382,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
* @deprecated * @deprecated
{{/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}} {{#isNullable}}
if (is_null(${{name}})) { if (is_null(${{name}})) {

View File

@ -391,9 +391,9 @@ class ObjectSerializer
* @param string[]|null $httpHeaders HTTP headers * @param string[]|null $httpHeaders HTTP headers
* @param string|null $discriminator discriminator if polymorphism is used * @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) { if (null === $data) {
return null; return null;

View File

@ -390,9 +390,9 @@ class ObjectSerializer
* @param string[]|null $httpHeaders HTTP headers * @param string[]|null $httpHeaders HTTP headers
* @param string|null $discriminator discriminator if polymorphism is used * @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) { if (null === $data) {
return null; return null;

View File

@ -33,7 +33,7 @@ class AuthTest extends TestCase
$fakeHttpClient = new FakeHttpClient(); $fakeHttpClient = new FakeHttpClient();
$api = new PetApi($fakeHttpClient, $authConfig); $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(); $headers = $fakeHttpClient->getLastRequest()->getHeaders();

View File

@ -20,10 +20,16 @@ class NullableTest extends TestCase
$name->setName(1); $name->setName(1);
$this->assertEquals(1, $name->getName(), 'Non-nullable property can be set and retains its value'); $this->assertEquals(1, $name->getName(), 'Non-nullable property can be set and retains its value');
$this->expectException(InvalidArgumentException::class); // comment out below as strict type is now enabled
$this->expectExceptionMessage('non-nullable name cannot be null'); //$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); $name->setName(null);
} }
public function testNullableobject(): void public function testNullableobject(): void

View File

@ -136,6 +136,7 @@ class PetApiTest extends TestCase
$updatedPet->setId($petId); $updatedPet->setId($petId);
$updatedPet->setName('updatePet'); $updatedPet->setName('updatePet');
$updatedPet->setStatus('pending'); $updatedPet->setStatus('pending');
$updatedPet->setPhotoUrls(array('http://a.com'));
$result = $this->api->updatePet($updatedPet); $result = $this->api->updatePet($updatedPet);
$this->assertNull($result); $this->assertNull($result);
@ -186,6 +187,7 @@ class PetApiTest extends TestCase
$newPet = new Model\Pet; $newPet = new Model\Pet;
$newPet->setId($new_pet_id); $newPet->setId($new_pet_id);
$newPet->setName("PHP Unit Test 2"); $newPet->setName("PHP Unit Test 2");
$newPet->setPhotoUrls(array("http://a.com"));
// add a new pet (model) // add a new pet (model)
$add_response = $this->api->addPet($newPet); $add_response = $this->api->addPet($newPet);
@ -196,6 +198,7 @@ class PetApiTest extends TestCase
$response = $this->api->getPetById($new_pet_id); $response = $this->api->getPetById($new_pet_id);
$this->assertSame($new_pet_id, $response->getId()); $this->assertSame($new_pet_id, $response->getId());
$this->assertSame('PHP Unit Test 2', $response->getName()); $this->assertSame('PHP Unit Test 2', $response->getName());
$this->assertSame(array("http://a.com"), $response->getPhotoUrls());
} }
/* /*

View File

@ -22,6 +22,8 @@ class ServerVariablesInOperationTest extends TestCase
$this->fakeHttpClient = new FakeHttpClient(); $this->fakeHttpClient = new FakeHttpClient();
$this->api = new Api\PetApi($this->fakeHttpClient); $this->api = new Api\PetApi($this->fakeHttpClient);
$this->pet = new Model\Pet(); $this->pet = new Model\Pet();
$this->pet->setName("something");
$this->pet->setPhotoUrls(array("https://a.com"));
} }
public function testServerVariablesInOperation(): void public function testServerVariablesInOperation(): void

View File

@ -30,6 +30,7 @@ class StoreApiTest extends TestCase
$pet = new Pet(); $pet = new Pet();
$pet->setId($id); $pet->setId($id);
$pet->setName('PHP Unit Test'); $pet->setName('PHP Unit Test');
$pet->setPhotoUrls(array('http://a.com'));
$pet->setStatus('available'); $pet->setStatus('available');
// new tag // new tag
$tag = new Tag(); $tag = new Tag();