Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #35 from chintan-j-patel/master
Browse files Browse the repository at this point in the history
Fixing Swagger to ignore abstract classes
  • Loading branch information
stevenramirezgains authored Aug 31, 2018
2 parents 81e1545 + 3c4b484 commit 855d9e4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/Zynga/Framework/Dynamic/V1/DynamicClassCreation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ class DynamicClassCreation {
return in_array($interfaceName, $reflected->getInterfaceNames());
}

public static function isClassAbstract(
string $className,
): bool {
if (DynamicClassCreation::doesClassExist($className) === false) {
throw new UnableToFindClassException('class='.$className);
}

$reflected = ReflectionClasses::getReflection($className);

if (!$reflected instanceof ReflectionClass) {
throw new UnableToFindClassException(
'class='.$className.' - unable to reflect',
);
}

return $reflected->isAbstract();
}

public static function createClassByNameGeneric<Tv>(
string $name,
Vector<mixed> $params,
Expand Down
65 changes: 65 additions & 0 deletions src/Zynga/Framework/Dynamic/V1/DynamicClassCreationTest.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ use Zynga\Framework\Dynamic\V1\Exceptions\MissingRequiredParametersException;
use Zynga\Framework\Dynamic\V1\Mocks\EmptyClass;
use Zynga\Framework\Dynamic\V1\Mocks\ClassWithConstructor;
use Zynga\Framework\Dynamic\V1\Mocks\ClassWithConstructorParams;
use Zynga\Framework\ReflectionCache\V1\ReflectionClasses;

class DynamicClassCreationTest extends TestCase {

<<__Override>>
public function tearDown(): void {
parent::tearDown();

// Disable failure for ReflectionClasses so that
// it can function normally
ReflectionClasses::disableFailure();
}

public function testDoesClassExist(): void {
$this->assertTrue(
DynamicClassCreation::doesClassExist(
Expand All @@ -36,6 +46,19 @@ class DynamicClassCreationTest extends TestCase {
$this->assertTrue($obj instanceof EmptyClass);
}

public function testFailureToReflectThrowsExceptionForClassExistsGeneric(
): void {
$this->expectException(UnableToFindClassException::class);

$className = 'Zynga\Framework\Dynamic\V1\Mocks\EmptyClass';
ReflectionClasses::enableFailure($className);

DynamicClassCreation::createClassByNameGeneric(
$className,
Vector {},
);
}

public function testClassExists(): void {
$obj = DynamicClassCreation::createClassByName(
'Zynga\Framework\Dynamic\V1\Mocks\EmptyClass',
Expand Down Expand Up @@ -125,4 +148,46 @@ class DynamicClassCreationTest extends TestCase {
);
}

public function testFailureToReflectThrowsExceptionForDoesClassImplementInterface(
): void {
$this->expectException(UnableToFindClassException::class);

$className = 'Zynga\Framework\Dynamic\V1\Mocks\ClassWithSingleInterface';
ReflectionClasses::enableFailure($className);

DynamicClassCreation::doesClassImplementInterface(
$className,
'Zynga\Framework\Dynamic\V1\Mocks\EmptyInterface',
);
}

public function testIsClassAbstract(): void {
$this->assertTrue(
DynamicClassCreation::isClassAbstract(
'Zynga\Framework\Dynamic\V1\Mocks\AbstractClass',
),
'AbstractClass is not marked as abstract',
);
$this->assertFalse(
DynamicClassCreation::isClassAbstract(
'Zynga\Framework\Dynamic\V1\Mocks\EmptyClass',
),
'EmptyClass is marked as abstract',
);
}

public function testInvalidClassThrowsExceptionForIsClassAbstract(): void {
$this->expectException(UnableToFindClassException::class);
$obj = DynamicClassCreation::isClassAbstract('Not_Real_Class');
}

public function testFailureToReflectThrowsExceptionForIsClassAbstract(
): void {
$this->expectException(UnableToFindClassException::class);

$className = 'Zynga\Framework\Dynamic\V1\Mocks\AbstractClass';
ReflectionClasses::enableFailure($className);

DynamicClassCreation::isClassAbstract($className);
}
}
5 changes: 5 additions & 0 deletions src/Zynga/Framework/Dynamic/V1/Mocks/AbstractClass.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?hh // strict

namespace Zynga\Framework\Dynamic\V1\Mocks;

abstract class AbstractClass {}
34 changes: 26 additions & 8 deletions src/Zynga/Framework/ReflectionCache/V1/ReflectionClasses.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ use \Exception;
use \ReflectionClass;

class ReflectionClasses {
private static bool $shouldFail = false;
private static string $classNameToFail = '';

private static Map<string, ?ReflectionClass> $_classes = Map {};

public static function getReflection(mixed $classOrName): ?ReflectionClass {
$className = self::getClassName($classOrName);

$className = '';

if (is_string($classOrName)) {
$className = $classOrName;
} else if (is_object($classOrName)) {
$className = get_class($classOrName);
} else {
$className = strval($classOrName);
if (self::$shouldFail === true && self::$classNameToFail === $className) {
return null;
}

try {
Expand Down Expand Up @@ -49,4 +46,25 @@ class ReflectionClasses {

}

private static function getClassName(mixed $classOrName): string {
if (is_string($classOrName)) {
return $classOrName;
}

if (is_object($classOrName)) {
return get_class($classOrName);
}

return strval($classOrName);
}

public static function enableFailure(mixed $classOrName): void {
self::$shouldFail = true;
self::$classNameToFail = self::getClassName($classOrName);
}

public static function disableFailure(): void {
self::$shouldFail = false;
self::$classNameToFail = '';
}
}
9 changes: 9 additions & 0 deletions src/Zynga/Framework/Service/V2/Swagger/Base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ abstract class Base extends ServiceBase {
// non-services might not have consistent constructors
continue;
}

if (DynamicClassCreation::isClassAbstract(
$longClassName,
)) {
// We also need to check this before instantiating the class because
// abstract class cant be instantiated and will cause fatal error
continue;
}

// Stand up the service in question and ask it about the request
// and response objects it has.
$service =
Expand Down

0 comments on commit 855d9e4

Please sign in to comment.