Skip to content

Commit

Permalink
fix type, update tests in php nextgen (#16758)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Oct 9, 2023
1 parent 5fb6fcf commit 48f454c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,18 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}'];
}
Expand All @@ -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}})) {
Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/php-nextgen/src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ public static function serializeCollection(array $collection, string $style, boo
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ public static function serializeCollection(array $collection, string $style, boo
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testApiToken()

$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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public function testNotNullableException(): void
$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
Expand All @@ -36,4 +42,4 @@ public function testNullableobject(): void
$nullable->setIntegerProp(1);
$this->assertEquals(1, $nullable->getIntegerProp(), 'Nullable property can be set and retains its value');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function testUpdatePet()
$updatedPet->setId($petId);
$updatedPet->setName('updatePet');
$updatedPet->setStatus('pending');
$updatedPet->setPhotoUrls(array('http://a.com'));
$result = $this->api->updatePet($updatedPet);
$this->assertNull($result);

Expand Down Expand Up @@ -186,6 +187,7 @@ public function testAddPet()
$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);
Expand All @@ -196,6 +198,7 @@ public function testAddPet()
$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());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function setUp(): void
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function setUpBeforeClass(): void
$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();
Expand Down

0 comments on commit 48f454c

Please sign in to comment.