php-laravel, enum models, nullable and default values (#12480)

* + support for enum models & separating mustache templates for enums and generics
+ extract property 'type' and 'default value' from a #ref to an enum model
+ support for `PascalCase` naming convention for PHP generators
+ use a default value for known basic types when they are not nullable
+ use 'null' as default value for nullable types when they do not specify the default value
+ use defined constant path as enum default value when found

* + sample output update

* + change model fields to be public and acessible from outside

* + sample output update

Co-authored-by: Mostafa Aghajani <mostafa.aghajani@virta.global>
This commit is contained in:
William Cheng
2022-05-27 11:16:26 +08:00
committed by GitHub
parent 5307a8dafd
commit 76eddeb713
54 changed files with 427 additions and 165 deletions

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class AdditionalPropertiesClass {
/** @var array<string,string> $mapProperty */
private $mapProperty;
public $mapProperty;
/** @var array<string,array<string,string>> $mapOfMapProperty */
private $mapOfMapProperty;
public $mapOfMapProperty;
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class AllOfWithSingleRef {
/** @var string $username */
private $username;
public $username = "";
/** @var SingleRefType $singleRefType */
private $singleRefType;
/** @var SingleRefType|null $singleRefType */
public $singleRefType = null;
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class Animal {
/** @var string $className */
private $className;
public $className = "";
/** @var string $color */
private $color;
public $color = 'red';
}

View File

@@ -10,12 +10,12 @@ namespace app\Models;
class ApiResponse {
/** @var int $code */
private $code;
public $code = 0;
/** @var string $type */
private $type;
public $type = "";
/** @var string $message */
private $message;
public $message = "";
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class ArrayOfArrayOfNumberOnly {
/** @var float[][] $arrayArrayNumber */
private $arrayArrayNumber;
public $arrayArrayNumber = [];
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class ArrayOfNumberOnly {
/** @var float[] $arrayNumber */
private $arrayNumber;
public $arrayNumber = [];
}

View File

@@ -10,12 +10,12 @@ namespace app\Models;
class ArrayTest {
/** @var string[] $arrayOfString */
private $arrayOfString;
public $arrayOfString = [];
/** @var int[][] $arrayArrayOfInteger */
private $arrayArrayOfInteger;
public $arrayArrayOfInteger = [];
/** @var \app\Models\ReadOnlyFirst[][] $arrayArrayOfModel */
private $arrayArrayOfModel;
public $arrayArrayOfModel = [];
}

View File

@@ -10,21 +10,21 @@ namespace app\Models;
class Capitalization {
/** @var string $smallCamel */
private $smallCamel;
public $smallCamel = "";
/** @var string $capitalCamel */
private $capitalCamel;
public $capitalCamel = "";
/** @var string $smallSnake */
private $smallSnake;
public $smallSnake = "";
/** @var string $capitalSnake */
private $capitalSnake;
public $capitalSnake = "";
/** @var string $sCAETHFlowPoints */
private $sCAETHFlowPoints;
public $sCAETHFlowPoints = "";
/** @var string $aTTNAME Name of the pet*/
private $aTTNAME;
public $aTTNAME = "";
}

View File

@@ -10,12 +10,12 @@ namespace app\Models;
class Cat {
/** @var string $className */
private $className;
public $className = "";
/** @var string $color */
private $color;
public $color = 'red';
/** @var bool $declawed */
private $declawed;
public $declawed = false;
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class CatAllOf {
/** @var bool $declawed */
private $declawed;
public $declawed = false;
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class Category {
/** @var int $id */
private $id;
public $id = 0;
/** @var string $name */
private $name;
public $name = 'default-name';
}

View File

@@ -6,10 +6,11 @@ namespace app\Models;
/**
* ClassModel
* @description Model for testing model with \"_class\" property
*/
class ClassModel {
/** @var string $class */
private $class;
public $class = "";
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class Client {
/** @var string $client */
private $client;
public $client = "";
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class DeprecatedObject {
/** @var string $name */
private $name;
public $name = "";
}

View File

@@ -10,12 +10,12 @@ namespace app\Models;
class Dog {
/** @var string $className */
private $className;
public $className = "";
/** @var string $color */
private $color;
public $color = 'red';
/** @var string $breed */
private $breed;
public $breed = "";
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class DogAllOf {
/** @var string $breed */
private $breed;
public $breed = "";
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class EnumArrays {
/** @var string $justSymbol */
private $justSymbol;
public $justSymbol = "";
/** @var string[] $arrayEnum */
private $arrayEnum;
public $arrayEnum = [];
}

View File

@@ -7,6 +7,27 @@ namespace app\Models;
/**
* EnumClass
*/
class EnumClass {
class EnumClass
{
/**
* Possible values of this enum
*/
const ABC = '_abc';
const EFG = '-efg';
const XYZ = '(xyz)';
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::ABC,
self::EFG,
self::XYZ
];
}
}

View File

@@ -10,27 +10,27 @@ namespace app\Models;
class EnumTest {
/** @var string $enumString */
private $enumString;
public $enumString = "";
/** @var string $enumStringRequired */
private $enumStringRequired;
public $enumStringRequired = "";
/** @var int $enumInteger */
private $enumInteger;
public $enumInteger = 0;
/** @var double $enumNumber */
private $enumNumber;
public $enumNumber = 0;
/** @var \app\Models\OuterEnum $outerEnum */
private $outerEnum;
/** @var string|null $outerEnum */
public $outerEnum = null;
/** @var \app\Models\OuterEnumInteger $outerEnumInteger */
private $outerEnumInteger;
/** @var int $outerEnumInteger */
public $outerEnumInteger = \app\Models\OuterEnumInteger::NUMBER_0;
/** @var \app\Models\OuterEnumDefaultValue $outerEnumDefaultValue */
private $outerEnumDefaultValue;
/** @var string $outerEnumDefaultValue */
public $outerEnumDefaultValue = \app\Models\OuterEnumDefaultValue::PLACED;
/** @var \app\Models\OuterEnumIntegerDefaultValue $outerEnumIntegerDefaultValue */
private $outerEnumIntegerDefaultValue;
/** @var int $outerEnumIntegerDefaultValue */
public $outerEnumIntegerDefaultValue = \app\Models\OuterEnumIntegerDefaultValue::NUMBER_0;
}

View File

@@ -6,10 +6,11 @@ namespace app\Models;
/**
* File
* @description Must be named `File` for test.
*/
class File {
/** @var string $sourceURI Test capitalization*/
private $sourceURI;
public $sourceURI = "";
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class FileSchemaTestClass {
/** @var \app\Models\File $file */
private $file;
public $file;
/** @var \app\Models\File[] $files */
private $files;
public $files = [];
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class Foo {
/** @var string $bar */
private $bar;
public $bar = 'bar';
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class FooGetDefaultResponse {
/** @var \app\Models\Foo $string */
private $string;
public $string;
}

View File

@@ -10,51 +10,51 @@ namespace app\Models;
class FormatTest {
/** @var int $integer */
private $integer;
public $integer = 0;
/** @var int $int32 */
private $int32;
public $int32 = 0;
/** @var int $int64 */
private $int64;
public $int64 = 0;
/** @var float $number */
private $number;
public $number = 0;
/** @var float $float */
private $float;
public $float = 0;
/** @var double $double */
private $double;
public $double = 0;
/** @var float $decimal */
private $decimal;
public $decimal = "";
/** @var string $string */
private $string;
public $string = "";
/** @var string $byte */
private $byte;
public $byte = "";
/** @var \SplFileObject $binary */
private $binary;
public $binary;
/** @var \DateTime $date */
private $date;
public $date;
/** @var \DateTime $dateTime */
private $dateTime;
public $dateTime;
/** @var string $uuid */
private $uuid;
public $uuid = "";
/** @var string $password */
private $password;
public $password = "";
/** @var string $patternWithDigits A string that is a 10 digit number. Can have leading zeros.*/
private $patternWithDigits;
public $patternWithDigits = "";
/** @var string $patternWithDigitsAndDelimiter A string starting with &#39;image_&#39; (case insensitive) and one to three digits following i.e. Image_01.*/
private $patternWithDigitsAndDelimiter;
public $patternWithDigitsAndDelimiter = "";
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class HasOnlyReadOnly {
/** @var string $bar */
private $bar;
public $bar = "";
/** @var string $foo */
private $foo;
public $foo = "";
}

View File

@@ -6,10 +6,11 @@ namespace app\Models;
/**
* HealthCheckResult
* @description Just a string to inform instance is up and running. Make it nullable in hope to get it as pointer in generated model.
*/
class HealthCheckResult {
/** @var string $nullableMessage */
private $nullableMessage;
/** @var string|null $nullableMessage */
public $nullableMessage = null;
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class InlineResponseDefault {
/** @var \app\Models\Foo $string */
private $string;
public $string;
}

View File

@@ -10,15 +10,15 @@ namespace app\Models;
class MapTest {
/** @var array<string,array<string,string>> $mapMapOfString */
private $mapMapOfString;
public $mapMapOfString;
/** @var array<string,string> $mapOfEnumString */
private $mapOfEnumString;
public $mapOfEnumString;
/** @var array<string,bool> $directMap */
private $directMap;
public $directMap;
/** @var array<string,bool> $indirectMap */
private $indirectMap;
public $indirectMap;
}

View File

@@ -10,12 +10,12 @@ namespace app\Models;
class MixedPropertiesAndAdditionalPropertiesClass {
/** @var string $uuid */
private $uuid;
public $uuid = "";
/** @var \DateTime $dateTime */
private $dateTime;
public $dateTime;
/** @var array<string,\app\Models\Animal> $map */
private $map;
public $map;
}

View File

@@ -6,13 +6,14 @@ namespace app\Models;
/**
* Model200Response
* @description Model for testing model name starting with number
*/
class Model200Response {
/** @var int $name */
private $name;
public $name = 0;
/** @var string $class */
private $class;
public $class = "";
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class ModelList {
/** @var string $_123list */
private $_123list;
public $_123list = "";
}

View File

@@ -6,10 +6,11 @@ namespace app\Models;
/**
* ModelReturn
* @description Model for testing reserved words
*/
class ModelReturn {
/** @var int $return */
private $return;
public $return = 0;
}

View File

@@ -6,19 +6,20 @@ namespace app\Models;
/**
* Name
* @description Model for testing model name same as property name
*/
class Name {
/** @var int $name */
private $name;
public $name = 0;
/** @var int $snakeCase */
private $snakeCase;
public $snakeCase = 0;
/** @var string $property */
private $property;
public $property = "";
/** @var int $_123number */
private $_123number;
public $_123number = 0;
}

View File

@@ -9,40 +9,40 @@ namespace app\Models;
*/
class NullableClass {
/** @var int $integerProp */
private $integerProp;
/** @var int|null $integerProp */
public $integerProp = null;
/** @var float $numberProp */
private $numberProp;
/** @var float|null $numberProp */
public $numberProp = null;
/** @var bool $booleanProp */
private $booleanProp;
/** @var bool|null $booleanProp */
public $booleanProp = null;
/** @var string $stringProp */
private $stringProp;
/** @var string|null $stringProp */
public $stringProp = null;
/** @var \DateTime $dateProp */
private $dateProp;
/** @var \DateTime|null $dateProp */
public $dateProp = null;
/** @var \DateTime $datetimeProp */
private $datetimeProp;
/** @var \DateTime|null $datetimeProp */
public $datetimeProp = null;
/** @var object[] $arrayNullableProp */
private $arrayNullableProp;
/** @var object[]|null $arrayNullableProp */
public $arrayNullableProp = null;
/** @var object[] $arrayAndItemsNullableProp */
private $arrayAndItemsNullableProp;
/** @var object[]|null $arrayAndItemsNullableProp */
public $arrayAndItemsNullableProp = null;
/** @var object[] $arrayItemsNullable */
private $arrayItemsNullable;
public $arrayItemsNullable = [];
/** @var array<string,object> $objectNullableProp */
private $objectNullableProp;
/** @var array<string,object>|null $objectNullableProp */
public $objectNullableProp = null;
/** @var array<string,object> $objectAndItemsNullableProp */
private $objectAndItemsNullableProp;
/** @var array<string,object>|null $objectAndItemsNullableProp */
public $objectAndItemsNullableProp = null;
/** @var array<string,object> $objectItemsNullable */
private $objectItemsNullable;
public $objectItemsNullable;
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class NumberOnly {
/** @var float $justNumber */
private $justNumber;
public $justNumber = 0;
}

View File

@@ -10,18 +10,18 @@ namespace app\Models;
class ObjectWithDeprecatedFields {
/** @var string $uuid */
private $uuid;
public $uuid = "";
/** @var float $id */
/** @deprecated */
private $id;
public $id = 0;
/** @var \app\Models\DeprecatedObject $deprecatedRef */
/** @deprecated */
private $deprecatedRef;
public $deprecatedRef;
/** @var string[] $bars */
/** @deprecated */
private $bars;
public $bars = [];
}

View File

@@ -10,21 +10,21 @@ namespace app\Models;
class Order {
/** @var int $id */
private $id;
public $id = 0;
/** @var int $petId */
private $petId;
public $petId = 0;
/** @var int $quantity */
private $quantity;
public $quantity = 0;
/** @var \DateTime $shipDate */
private $shipDate;
public $shipDate;
/** @var string $status Order Status*/
private $status;
public $status = "";
/** @var bool $complete */
private $complete;
public $complete = false;
}

View File

@@ -10,12 +10,12 @@ namespace app\Models;
class OuterComposite {
/** @var float $myNumber */
private $myNumber;
public $myNumber = 0;
/** @var string $myString */
private $myString;
public $myString = "";
/** @var bool $myBoolean */
private $myBoolean;
public $myBoolean = false;
}

View File

@@ -7,6 +7,27 @@ namespace app\Models;
/**
* OuterEnum
*/
class OuterEnum {
class OuterEnum
{
/**
* Possible values of this enum
*/
const PLACED = 'placed';
const APPROVED = 'approved';
const DELIVERED = 'delivered';
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::PLACED,
self::APPROVED,
self::DELIVERED
];
}
}

View File

@@ -7,6 +7,27 @@ namespace app\Models;
/**
* OuterEnumDefaultValue
*/
class OuterEnumDefaultValue {
class OuterEnumDefaultValue
{
/**
* Possible values of this enum
*/
const PLACED = 'placed';
const APPROVED = 'approved';
const DELIVERED = 'delivered';
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::PLACED,
self::APPROVED,
self::DELIVERED
];
}
}

View File

@@ -7,6 +7,27 @@ namespace app\Models;
/**
* OuterEnumInteger
*/
class OuterEnumInteger {
class OuterEnumInteger
{
/**
* Possible values of this enum
*/
const NUMBER_0 = 0;
const NUMBER_1 = 1;
const NUMBER_2 = 2;
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::NUMBER_0,
self::NUMBER_1,
self::NUMBER_2
];
}
}

View File

@@ -7,6 +7,27 @@ namespace app\Models;
/**
* OuterEnumIntegerDefaultValue
*/
class OuterEnumIntegerDefaultValue {
class OuterEnumIntegerDefaultValue
{
/**
* Possible values of this enum
*/
const NUMBER_0 = 0;
const NUMBER_1 = 1;
const NUMBER_2 = 2;
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::NUMBER_0,
self::NUMBER_1,
self::NUMBER_2
];
}
}

View File

@@ -9,7 +9,7 @@ namespace app\Models;
*/
class OuterObjectWithEnumProperty {
/** @var \app\Models\OuterEnumInteger $value */
private $value;
/** @var int $value */
public $value = \app\Models\OuterEnumInteger::NUMBER_0;
}

View File

@@ -10,21 +10,21 @@ namespace app\Models;
class Pet {
/** @var int $id */
private $id;
public $id = 0;
/** @var \app\Models\Category $category */
private $category;
public $category;
/** @var string $name */
private $name;
public $name = "";
/** @var string[] $photoUrls */
private $photoUrls;
public $photoUrls = [];
/** @var \app\Models\Tag[] $tags */
private $tags;
public $tags = [];
/** @var string $status pet status in the store*/
private $status;
public $status = "";
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class ReadOnlyFirst {
/** @var string $bar */
private $bar;
public $bar = "";
/** @var string $baz */
private $baz;
public $baz = "";
}

View File

@@ -7,6 +7,24 @@ namespace app\Models;
/**
* SingleRefType
*/
class SingleRefType {
class SingleRefType
{
/**
* Possible values of this enum
*/
const ADMIN = 'admin';
const USER = 'user';
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::ADMIN,
self::USER
];
}
}

View File

@@ -10,6 +10,6 @@ namespace app\Models;
class SpecialModelName {
/** @var int $specialPropertyName */
private $specialPropertyName;
public $specialPropertyName = 0;
}

View File

@@ -10,9 +10,9 @@ namespace app\Models;
class Tag {
/** @var int $id */
private $id;
public $id = 0;
/** @var string $name */
private $name;
public $name = "";
}

View File

@@ -10,27 +10,27 @@ namespace app\Models;
class User {
/** @var int $id */
private $id;
public $id = 0;
/** @var string $username */
private $username;
public $username = "";
/** @var string $firstName */
private $firstName;
public $firstName = "";
/** @var string $lastName */
private $lastName;
public $lastName = "";
/** @var string $email */
private $email;
public $email = "";
/** @var string $password */
private $password;
public $password = "";
/** @var string $phone */
private $phone;
public $phone = "";
/** @var int $userStatus User Status*/
private $userStatus;
public $userStatus = 0;
}