Skip to content

Commit

Permalink
php-laravel, enum models, nullable and default values (#12480)
Browse files Browse the repository at this point in the history
* + 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 <[email protected]>
  • Loading branch information
wing328 and aghajani authored May 27, 2022
1 parent 5307a8d commit 76eddeb
Show file tree
Hide file tree
Showing 54 changed files with 427 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ public String toVarName(String name) {
// return the name in camelCase style
// phone_number => phoneNumber
name = camelize(name, true);
} else if ("PascalCase".equals(variableNamingConvention)) {
name = camelize(name, false);
} else { // default to snake case
// return the name in underscore style
// PhoneNumber => phone_number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
package org.openapitools.codegen.languages;

import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.ModelUtils;

import io.swagger.v3.oas.models.media.Schema;

import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -296,4 +300,96 @@ protected String toControllerName(String name) {

return camelize(name, false) + "Controller";
}

@Override
protected String getEnumDefaultValue(String defaultValue, String dataType) {
return defaultValue;
}

@Override
public CodegenProperty fromProperty(String name, Schema p) {
CodegenProperty property = super.fromProperty(name, p);
Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, p);

//Referenced enum case:
if (!property.isEnum && referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
property.dataType = this.getSchemaType(referencedSchema);
property.defaultValue = this.toDefaultValue(referencedSchema);
List<Object> _enum = referencedSchema.getEnum();

Map<String, Object> allowableValues = new HashMap<>();
allowableValues.put("values", _enum);
if (allowableValues.size() > 0) {
property.allowableValues = allowableValues;
}
}

return property;
}

@Override
public String toDefaultValue(Schema p) {
if (ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "false";
}
} else if (ModelUtils.isDateSchema(p)) {
// TODO
} else if (ModelUtils.isDateTimeSchema(p)) {
// TODO
} else if (ModelUtils.isFileSchema(p)) {
// TODO
} else if (ModelUtils.isNumberSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "0";
}
} else if (ModelUtils.isIntegerSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "0";
}
} else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) {
return "'" + p.getDefault() + "'";
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "\"\"";
}
} else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();
} else if (!Boolean.TRUE.equals(p.getNullable())) {
return "[]";
}
}

return null;
}

@Override
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "::" + value;
}

@Override
public String toEnumVarName(String value, String datatype) {
if (value.length() == 0) {
return super.toEnumVarName(value, datatype);
}

// number
if ("int".equals(datatype) || "double".equals(datatype) || "float".equals(datatype)) {
String varName = "NUMBER_" + value;
varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}

return super.toEnumVarName(value, datatype);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?php
{{#models}}{{#model}}/**
{{#models}}
{{#model}}
/**
* {{classname}}
*/
namespace {{modelPackage}};

/**
* {{classname}}
{{#description}}
* @description {{{.}}}
{{/description}}
*/
class {{classname}} {
{{#vars}}
/** @var {{{dataType}}} ${{name}} {{description}}*/
{{#deprecated}}
/** @deprecated */
{{/deprecated}}
private ${{name}};

{{/vars}}
}
{{/model}}{{/models}}
{{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class {{classname}}
{
/**
* Possible values of this enum
*/
{{#allowableValues}}
{{#enumVars}}
const {{{name}}} = {{{value}}};

{{/enumVars}}
{{/allowableValues}}
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
{{#allowableValues}}
{{#enumVars}}
self::{{{name}}}{{^-last}},
{{/-last}}
{{/enumVars}}
{{/allowableValues}}

];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class {{classname}} {
{{#vars}}
/** @var {{{dataType}}}{{#isNullable}}|null{{/isNullable}} ${{name}} {{description}}*/
{{#deprecated}}
/** @deprecated */
{{/deprecated}}
public ${{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}{{#isNullable}} = null{{/isNullable}}{{/defaultValue}};

{{/vars}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class AdditionalPropertiesClass {

/** @var array<string,string> $mapProperty */
private $mapProperty;
public $mapProperty;

/** @var array<string,array<string,string>> $mapOfMapProperty */
private $mapOfMapProperty;
public $mapOfMapProperty;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class AllOfWithSingleRef {

/** @var string $username */
private $username;
public $username = "";

/** @var SingleRefType $singleRefType */
private $singleRefType;
/** @var SingleRefType|null $singleRefType */
public $singleRefType = null;

}
4 changes: 2 additions & 2 deletions samples/server/petstore/php-laravel/lib/app/Models/Animal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class Animal {

/** @var string $className */
private $className;
public $className = "";

/** @var string $color */
private $color;
public $color = 'red';

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
class ApiResponse {

/** @var int $code */
private $code;
public $code = 0;

/** @var string $type */
private $type;
public $type = "";

/** @var string $message */
private $message;
public $message = "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
class ArrayOfArrayOfNumberOnly {

/** @var float[][] $arrayArrayNumber */
private $arrayArrayNumber;
public $arrayArrayNumber = [];

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
class ArrayOfNumberOnly {

/** @var float[] $arrayNumber */
private $arrayNumber;
public $arrayNumber = [];

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
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 = [];

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
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 = "";

}
6 changes: 3 additions & 3 deletions samples/server/petstore/php-laravel/lib/app/Models/Cat.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
class CatAllOf {

/** @var bool $declawed */
private $declawed;
public $declawed = false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class Category {

/** @var int $id */
private $id;
public $id = 0;

/** @var string $name */
private $name;
public $name = 'default-name';

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

/**
* ClassModel
* @description Model for testing model with \"_class\" property
*/
class ClassModel {

/** @var string $class */
private $class;
public $class = "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
class Client {

/** @var string $client */
private $client;
public $client = "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
class DeprecatedObject {

/** @var string $name */
private $name;
public $name = "";

}
6 changes: 3 additions & 3 deletions samples/server/petstore/php-laravel/lib/app/Models/Dog.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
class Dog {

/** @var string $className */
private $className;
public $className = "";

/** @var string $color */
private $color;
public $color = 'red';

/** @var string $breed */
private $breed;
public $breed = "";

}
Loading

0 comments on commit 76eddeb

Please sign in to comment.