Release: swagger-php 2.0.9
Currently it is possible to use the enum attribute to define the excected values.
a) is it possible to reference a PHP constant here?
b) even better would be a possibility to reference a class where the enumeration types are defined as constants?
/**
*
* @SWG\Definition(description="Generic Response", required={"statusCode"})
*/
class GenericResponse
{
/**
* @SWG\Property(type="string",enum={"SUCCESS", "ERROR"},description="Status-Code")
*/
private $statusCode;
...
}
Sample for external enum reference:
/**
*
* @SWG\Definition(description="Generic Response", required={"statusCode"})
*/
class GenericResponse
{
/**
* @SWG\Property(type="string",enumRef=GenericResponseStatusCodeEnum,description="Status-Code")
*/
private $statusCode;
...
}
class GenericResponseStatusCodeEnum
{
const SUCCESS="SUCCESS";
const ERROR="ERROR";
}
output with GenericResponseStatusCodeEnum constants automatically merged into swagger contract enum:
GenericResponse": {
"description": "...",
"required": [
"statusCode"
],
"properties": {
"statusCode": {
"description": "Status-Code",
"type": "string",
"enum": [
"success",
"error"
]
},
"message": {
"description": "Message",
"type": "string"
}
}
},
What do you think?
Global constants are already supported, I believe constants inside classes are more troublesome as doctrine doesn't autoload the class.
I tried both and both don't seem to work:
1.) constants inside classes:
namespace Models;
/**
* @SWG\Definition
*/
class MyResponse
{
const STATUS_TEST = "test";
/**
* @var string
* @SWG\Property(enum={"success", "error", GenericResponse::STATUS_TEST})
*/
private $statusCode;
}
Error: [WARN] [Semantical Error] Couldn't find constant GenericResponse::STATUS_TEST, ModelsMyResponse->statusCode in srcserverModelsMyResponse.php on line 15.
2.) And global constants don't work as well:
const STATUS_TEST = "test";
/**
*
* @SWG\Definition
*/
class MyResponse
{
/**
* Status-Code
* @var string
* @SWG\Property(enum={"success", "error", STATUS_TEST})
*/
private $statusCode;
public function hello() {
echo STATUS_TEST;
}
}
$var = new MyResponse();
// this gives "test" as expected
$var->hello();
Error: [WARN] [Semantical Error] Couldn't find constant STATUS_TEST, ModelsMyResponse->statusCode in srcserverModelsMyResponse.php on line 18.
The constants must be defined before parsing. If you're using the cli use the --bootstrap option to include a php file which defines the constants
Or even better, fix the doctrine-annotations library with improved support for (autoloading) constants
Just stumbled upon this: doctrine/annotations does in theory trigger autoloading of referenced classes. Will look at it, if needed and if someone wants to report a bug upstream.
I was able to get this working by using the FQCN (Fully Qualified Class Name) inside of the annotation.
@OA\Property(property="type", type="string", enum={ App\Services\Audit\AuditType::RECORD_VIEW})
Most helpful comment
The constants must be defined before parsing. If you're using the cli use the --bootstrap option to include a php file which defines the constants
Or even better, fix the doctrine-annotations library with improved support for (autoloading) constants