[php] enhance type mapping (#10457)

* map BigDecimal to float

* enhance type mapping in php generators

* update tests

* update doc, samples

* remove primitive types from phpdt, mezzio
This commit is contained in:
William Cheng 2021-09-25 12:09:34 +08:00 committed by GitHub
parent be3bd2e6c7
commit c9047a6faa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 58 additions and 28 deletions

View File

@ -40,7 +40,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -39,7 +39,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>\DateTime</li>
<li>\SplFileObject</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -39,7 +39,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>\DateTime</li>
<li>\SplFileObject</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -40,7 +40,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -39,7 +39,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>\DateTime</li>
<li>\SplFileObject</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -40,7 +40,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>\DateTime</li>
<li>\SplFileObject</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -45,6 +45,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>UploadedFile</li>
<li>\DateTime</li>
<li>array</li>
<li>bool</li>
<li>byte</li>

View File

@ -40,7 +40,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
## LANGUAGE PRIMITIVES
<ul class="column-ul">
<li>DateTime</li>
<li>\DateTime</li>
<li>\SplFileObject</li>
<li>array</li>
<li>bool</li>
<li>boolean</li>

View File

@ -90,7 +90,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
"string",
"object",
"array",
"DateTime",
"\\DateTime",
"\\SplFileObject",
"mixed",
"number",
"void",
@ -111,6 +112,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
typeMapping.put("long", "int");
typeMapping.put("number", "float");
typeMapping.put("float", "float");
typeMapping.put("decimal", "float");
typeMapping.put("double", "double");
typeMapping.put("string", "string");
typeMapping.put("byte", "int");
@ -341,6 +343,12 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p);
String type = null;
if (openAPIType == null) {
LOGGER.error("OpenAPI Type for {} is null. Default to UNKNOWN_OPENAPI_TYPE instead.", p.getName());
openAPIType = "UNKNOWN_OPENAPI_TYPE";
}
if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type)) {
@ -348,12 +356,18 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
} else if (instantiationTypes.containsKey(type)) {
return type;
}
/*
// comment out the following as php-dt, php-mezzio still need to treat DateTime, SplFileObject as objects
} else {
throw new RuntimeException("OpenAPI type `" + openAPIType + "` defined but can't mapped to language type." +
" Please report the issue via OpenAPI Generator github repo." +
" (if you're not using custom format with proper type mappings provided to openapi-generator)");
}
*/
} else {
type = openAPIType;
}
if (type == null) {
return null;
}
return toModelName(type);
}

View File

@ -95,6 +95,10 @@ public class PhpDataTransferClientCodegen extends AbstractPhpCodegen {
//no point to use double - http://php.net/manual/en/language.types.float.php , especially because of PHP 7+ float type declaration
typeMapping.put("double", "float");
// remove these from primitive types to make the output works
languageSpecificPrimitives.remove("\\DateTime");
languageSpecificPrimitives.remove("\\SplFileObject");
apiTemplateFiles.clear();
apiTestTemplateFiles.clear();
apiDocTemplateFiles.clear();

View File

@ -87,6 +87,10 @@ public class PhpMezzioPathHandlerServerCodegen extends AbstractPhpCodegen {
//no point to use double - http://php.net/manual/en/language.types.float.php , especially because of PHP 7+ float type declaration
typeMapping.put("double", "float");
// remove these from primitive types to make the output works
languageSpecificPrimitives.remove("\\DateTime");
languageSpecificPrimitives.remove("\\SplFileObject");
embeddedTemplateDir = templateDir = "php-mezzio-ph";
invokerPackage = "App";
srcBasePath = "src" + File.separator + "App";

View File

@ -151,7 +151,9 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
"number",
"void",
"byte",
"array"
"array",
"\\DateTime",
"UploadedFile"
)
);
@ -174,6 +176,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
typeMapping = new HashMap<String, String>();
typeMapping.put("integer", "int");
typeMapping.put("long", "int");
typeMapping.put("decimal", "float");
typeMapping.put("number", "float");
typeMapping.put("float", "float");
typeMapping.put("double", "double");

View File

@ -76,7 +76,7 @@ public class PhpModelTest {
final CodegenProperty property3 = cm.vars.get(2);
Assert.assertEquals(property3.baseName, "createdAt");
Assert.assertEquals(property3.complexType, "\\DateTime");
Assert.assertEquals(property3.complexType, null);
Assert.assertEquals(property3.dataType, "\\DateTime");
Assert.assertEquals(property3.name, "created_at");
Assert.assertEquals(property3.defaultValue, null);

View File

@ -10,12 +10,12 @@ Name | Type | Description | Notes
**number** | **float** | |
**float** | **float** | | [optional]
**double** | **double** | | [optional]
**decimal** | [**Decimal**](Decimal.md) | | [optional]
**decimal** | **float** | | [optional]
**string** | **string** | | [optional]
**byte** | **string** | |
**binary** | [**\SplFileObject**](\SplFileObject.md) | | [optional]
**date** | [**\DateTime**](\DateTime.md) | |
**date_time** | [**\DateTime**](\DateTime.md) | | [optional]
**binary** | **\SplFileObject** | | [optional]
**date** | **\DateTime** | |
**date_time** | **\DateTime** | | [optional]
**uuid** | **string** | | [optional]
**password** | **string** | |
**pattern_with_digits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]

View File

@ -5,7 +5,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**uuid** | **string** | | [optional]
**date_time** | [**\DateTime**](\DateTime.md) | | [optional]
**date_time** | **\DateTime** | | [optional]
**map** | [**array<string,\OpenAPI\Client\Model\Animal>**](Animal.md) | | [optional]
[[Back to Model list]](../../README.md#models) [[Back to API list]](../../README.md#endpoints) [[Back to README]](../../README.md)

View File

@ -8,8 +8,8 @@ Name | Type | Description | Notes
**number_prop** | **float** | | [optional]
**boolean_prop** | **bool** | | [optional]
**string_prop** | **string** | | [optional]
**date_prop** | [**\DateTime**](\DateTime.md) | | [optional]
**datetime_prop** | [**\DateTime**](\DateTime.md) | | [optional]
**date_prop** | **\DateTime** | | [optional]
**datetime_prop** | **\DateTime** | | [optional]
**array_nullable_prop** | **object[]** | | [optional]
**array_and_items_nullable_prop** | **object[]** | | [optional]
**array_items_nullable** | **object[]** | | [optional]

View File

@ -7,7 +7,7 @@ Name | Type | Description | Notes
**id** | **int** | | [optional]
**pet_id** | **int** | | [optional]
**quantity** | **int** | | [optional]
**ship_date** | [**\DateTime**](\DateTime.md) | | [optional]
**ship_date** | **\DateTime** | | [optional]
**status** | **string** | Order Status | [optional]
**complete** | **bool** | | [optional] [default to false]

View File

@ -65,7 +65,7 @@ class FormatTest implements ModelInterface, ArrayAccess, \JsonSerializable
'number' => 'float',
'float' => 'float',
'double' => 'double',
'decimal' => 'Decimal',
'decimal' => 'float',
'string' => 'string',
'byte' => 'string',
'binary' => '\SplFileObject',
@ -554,7 +554,7 @@ class FormatTest implements ModelInterface, ArrayAccess, \JsonSerializable
/**
* Gets decimal
*
* @return Decimal|null
* @return float|null
*/
public function getDecimal()
{
@ -564,7 +564,7 @@ class FormatTest implements ModelInterface, ArrayAccess, \JsonSerializable
/**
* Sets decimal
*
* @param Decimal|null $decimal decimal
* @param float|null $decimal decimal
*
* @return self
*/

View File

@ -86,7 +86,7 @@ class ObjectSerializer
foreach ($data::openAPITypes() as $property => $openAPIType) {
$getter = $data::getters()[$property];
$value = $data->$getter();
if ($value !== null && !in_array($openAPIType, ['DateTime', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
$callable = [$openAPIType, 'getAllowableEnumValues'];
if (is_callable($callable)) {
/** array $callable */
@ -330,7 +330,7 @@ class ObjectSerializer
}
/** @psalm-suppress ParadoxicalCondition */
if (in_array($class, ['DateTime', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
if (in_array($class, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
settype($data, $class);
return $data;
}

View File

@ -27,7 +27,7 @@ class FormatTest {
/** @var double $double */
private $double;
/** @var Decimal $decimal */
/** @var float $decimal */
private $decimal;
/** @var string $string */

View File

@ -6,7 +6,7 @@ Name | Type | Description | Notes
**id** | **int** | | [optional]
**petId** | **int** | | [optional]
**quantity** | **int** | | [optional]
**shipDate** | [**\DateTime**](\DateTime.md) | | [optional]
**shipDate** | **\DateTime** | | [optional]
**status** | **string** | Order Status | [optional]
**complete** | **bool** | | [optional] [default to false]