forked from loafle/openapi-generator-original
[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:
parent
be3bd2e6c7
commit
c9047a6faa
@ -40,7 +40,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -39,7 +39,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
<li>\DateTime</li>
|
||||||
|
<li>\SplFileObject</li>
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -39,7 +39,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
<li>\DateTime</li>
|
||||||
|
<li>\SplFileObject</li>
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -40,7 +40,6 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -39,7 +39,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
<li>\DateTime</li>
|
||||||
|
<li>\SplFileObject</li>
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -40,7 +40,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
<li>\DateTime</li>
|
||||||
|
<li>\SplFileObject</li>
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -45,6 +45,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
|
<li>UploadedFile</li>
|
||||||
|
<li>\DateTime</li>
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>byte</li>
|
<li>byte</li>
|
||||||
|
@ -40,7 +40,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|
|||||||
## LANGUAGE PRIMITIVES
|
## LANGUAGE PRIMITIVES
|
||||||
|
|
||||||
<ul class="column-ul">
|
<ul class="column-ul">
|
||||||
<li>DateTime</li>
|
<li>\DateTime</li>
|
||||||
|
<li>\SplFileObject</li>
|
||||||
<li>array</li>
|
<li>array</li>
|
||||||
<li>bool</li>
|
<li>bool</li>
|
||||||
<li>boolean</li>
|
<li>boolean</li>
|
||||||
|
@ -90,7 +90,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
|||||||
"string",
|
"string",
|
||||||
"object",
|
"object",
|
||||||
"array",
|
"array",
|
||||||
"DateTime",
|
"\\DateTime",
|
||||||
|
"\\SplFileObject",
|
||||||
"mixed",
|
"mixed",
|
||||||
"number",
|
"number",
|
||||||
"void",
|
"void",
|
||||||
@ -111,6 +112,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
|||||||
typeMapping.put("long", "int");
|
typeMapping.put("long", "int");
|
||||||
typeMapping.put("number", "float");
|
typeMapping.put("number", "float");
|
||||||
typeMapping.put("float", "float");
|
typeMapping.put("float", "float");
|
||||||
|
typeMapping.put("decimal", "float");
|
||||||
typeMapping.put("double", "double");
|
typeMapping.put("double", "double");
|
||||||
typeMapping.put("string", "string");
|
typeMapping.put("string", "string");
|
||||||
typeMapping.put("byte", "int");
|
typeMapping.put("byte", "int");
|
||||||
@ -341,6 +343,12 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
|||||||
public String getSchemaType(Schema p) {
|
public String getSchemaType(Schema p) {
|
||||||
String openAPIType = super.getSchemaType(p);
|
String openAPIType = super.getSchemaType(p);
|
||||||
String type = null;
|
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)) {
|
if (typeMapping.containsKey(openAPIType)) {
|
||||||
type = typeMapping.get(openAPIType);
|
type = typeMapping.get(openAPIType);
|
||||||
if (languageSpecificPrimitives.contains(type)) {
|
if (languageSpecificPrimitives.contains(type)) {
|
||||||
@ -348,12 +356,18 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
|
|||||||
} else if (instantiationTypes.containsKey(type)) {
|
} else if (instantiationTypes.containsKey(type)) {
|
||||||
return 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 {
|
} else {
|
||||||
type = openAPIType;
|
type = openAPIType;
|
||||||
}
|
}
|
||||||
if (type == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return toModelName(type);
|
return toModelName(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
//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");
|
typeMapping.put("double", "float");
|
||||||
|
|
||||||
|
// remove these from primitive types to make the output works
|
||||||
|
languageSpecificPrimitives.remove("\\DateTime");
|
||||||
|
languageSpecificPrimitives.remove("\\SplFileObject");
|
||||||
|
|
||||||
apiTemplateFiles.clear();
|
apiTemplateFiles.clear();
|
||||||
apiTestTemplateFiles.clear();
|
apiTestTemplateFiles.clear();
|
||||||
apiDocTemplateFiles.clear();
|
apiDocTemplateFiles.clear();
|
||||||
|
@ -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
|
//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");
|
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";
|
embeddedTemplateDir = templateDir = "php-mezzio-ph";
|
||||||
invokerPackage = "App";
|
invokerPackage = "App";
|
||||||
srcBasePath = "src" + File.separator + "App";
|
srcBasePath = "src" + File.separator + "App";
|
||||||
|
@ -151,7 +151,9 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
"number",
|
"number",
|
||||||
"void",
|
"void",
|
||||||
"byte",
|
"byte",
|
||||||
"array"
|
"array",
|
||||||
|
"\\DateTime",
|
||||||
|
"UploadedFile"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -174,6 +176,7 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
|
|||||||
typeMapping = new HashMap<String, String>();
|
typeMapping = new HashMap<String, String>();
|
||||||
typeMapping.put("integer", "int");
|
typeMapping.put("integer", "int");
|
||||||
typeMapping.put("long", "int");
|
typeMapping.put("long", "int");
|
||||||
|
typeMapping.put("decimal", "float");
|
||||||
typeMapping.put("number", "float");
|
typeMapping.put("number", "float");
|
||||||
typeMapping.put("float", "float");
|
typeMapping.put("float", "float");
|
||||||
typeMapping.put("double", "double");
|
typeMapping.put("double", "double");
|
||||||
|
@ -76,7 +76,7 @@ public class PhpModelTest {
|
|||||||
|
|
||||||
final CodegenProperty property3 = cm.vars.get(2);
|
final CodegenProperty property3 = cm.vars.get(2);
|
||||||
Assert.assertEquals(property3.baseName, "createdAt");
|
Assert.assertEquals(property3.baseName, "createdAt");
|
||||||
Assert.assertEquals(property3.complexType, "\\DateTime");
|
Assert.assertEquals(property3.complexType, null);
|
||||||
Assert.assertEquals(property3.dataType, "\\DateTime");
|
Assert.assertEquals(property3.dataType, "\\DateTime");
|
||||||
Assert.assertEquals(property3.name, "created_at");
|
Assert.assertEquals(property3.name, "created_at");
|
||||||
Assert.assertEquals(property3.defaultValue, null);
|
Assert.assertEquals(property3.defaultValue, null);
|
||||||
|
@ -10,12 +10,12 @@ Name | Type | Description | Notes
|
|||||||
**number** | **float** | |
|
**number** | **float** | |
|
||||||
**float** | **float** | | [optional]
|
**float** | **float** | | [optional]
|
||||||
**double** | **double** | | [optional]
|
**double** | **double** | | [optional]
|
||||||
**decimal** | [**Decimal**](Decimal.md) | | [optional]
|
**decimal** | **float** | | [optional]
|
||||||
**string** | **string** | | [optional]
|
**string** | **string** | | [optional]
|
||||||
**byte** | **string** | |
|
**byte** | **string** | |
|
||||||
**binary** | [**\SplFileObject**](\SplFileObject.md) | | [optional]
|
**binary** | **\SplFileObject** | | [optional]
|
||||||
**date** | [**\DateTime**](\DateTime.md) | |
|
**date** | **\DateTime** | |
|
||||||
**date_time** | [**\DateTime**](\DateTime.md) | | [optional]
|
**date_time** | **\DateTime** | | [optional]
|
||||||
**uuid** | **string** | | [optional]
|
**uuid** | **string** | | [optional]
|
||||||
**password** | **string** | |
|
**password** | **string** | |
|
||||||
**pattern_with_digits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
|
**pattern_with_digits** | **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
Name | Type | Description | Notes
|
Name | Type | Description | Notes
|
||||||
------------ | ------------- | ------------- | -------------
|
------------ | ------------- | ------------- | -------------
|
||||||
**uuid** | **string** | | [optional]
|
**uuid** | **string** | | [optional]
|
||||||
**date_time** | [**\DateTime**](\DateTime.md) | | [optional]
|
**date_time** | **\DateTime** | | [optional]
|
||||||
**map** | [**array<string,\OpenAPI\Client\Model\Animal>**](Animal.md) | | [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)
|
[[Back to Model list]](../../README.md#models) [[Back to API list]](../../README.md#endpoints) [[Back to README]](../../README.md)
|
||||||
|
@ -8,8 +8,8 @@ Name | Type | Description | Notes
|
|||||||
**number_prop** | **float** | | [optional]
|
**number_prop** | **float** | | [optional]
|
||||||
**boolean_prop** | **bool** | | [optional]
|
**boolean_prop** | **bool** | | [optional]
|
||||||
**string_prop** | **string** | | [optional]
|
**string_prop** | **string** | | [optional]
|
||||||
**date_prop** | [**\DateTime**](\DateTime.md) | | [optional]
|
**date_prop** | **\DateTime** | | [optional]
|
||||||
**datetime_prop** | [**\DateTime**](\DateTime.md) | | [optional]
|
**datetime_prop** | **\DateTime** | | [optional]
|
||||||
**array_nullable_prop** | **object[]** | | [optional]
|
**array_nullable_prop** | **object[]** | | [optional]
|
||||||
**array_and_items_nullable_prop** | **object[]** | | [optional]
|
**array_and_items_nullable_prop** | **object[]** | | [optional]
|
||||||
**array_items_nullable** | **object[]** | | [optional]
|
**array_items_nullable** | **object[]** | | [optional]
|
||||||
|
@ -7,7 +7,7 @@ Name | Type | Description | Notes
|
|||||||
**id** | **int** | | [optional]
|
**id** | **int** | | [optional]
|
||||||
**pet_id** | **int** | | [optional]
|
**pet_id** | **int** | | [optional]
|
||||||
**quantity** | **int** | | [optional]
|
**quantity** | **int** | | [optional]
|
||||||
**ship_date** | [**\DateTime**](\DateTime.md) | | [optional]
|
**ship_date** | **\DateTime** | | [optional]
|
||||||
**status** | **string** | Order Status | [optional]
|
**status** | **string** | Order Status | [optional]
|
||||||
**complete** | **bool** | | [optional] [default to false]
|
**complete** | **bool** | | [optional] [default to false]
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class FormatTest implements ModelInterface, ArrayAccess, \JsonSerializable
|
|||||||
'number' => 'float',
|
'number' => 'float',
|
||||||
'float' => 'float',
|
'float' => 'float',
|
||||||
'double' => 'double',
|
'double' => 'double',
|
||||||
'decimal' => 'Decimal',
|
'decimal' => 'float',
|
||||||
'string' => 'string',
|
'string' => 'string',
|
||||||
'byte' => 'string',
|
'byte' => 'string',
|
||||||
'binary' => '\SplFileObject',
|
'binary' => '\SplFileObject',
|
||||||
@ -554,7 +554,7 @@ class FormatTest implements ModelInterface, ArrayAccess, \JsonSerializable
|
|||||||
/**
|
/**
|
||||||
* Gets decimal
|
* Gets decimal
|
||||||
*
|
*
|
||||||
* @return Decimal|null
|
* @return float|null
|
||||||
*/
|
*/
|
||||||
public function getDecimal()
|
public function getDecimal()
|
||||||
{
|
{
|
||||||
@ -564,7 +564,7 @@ class FormatTest implements ModelInterface, ArrayAccess, \JsonSerializable
|
|||||||
/**
|
/**
|
||||||
* Sets decimal
|
* Sets decimal
|
||||||
*
|
*
|
||||||
* @param Decimal|null $decimal decimal
|
* @param float|null $decimal decimal
|
||||||
*
|
*
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
|
@ -86,7 +86,7 @@ class ObjectSerializer
|
|||||||
foreach ($data::openAPITypes() as $property => $openAPIType) {
|
foreach ($data::openAPITypes() as $property => $openAPIType) {
|
||||||
$getter = $data::getters()[$property];
|
$getter = $data::getters()[$property];
|
||||||
$value = $data->$getter();
|
$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'];
|
$callable = [$openAPIType, 'getAllowableEnumValues'];
|
||||||
if (is_callable($callable)) {
|
if (is_callable($callable)) {
|
||||||
/** array $callable */
|
/** array $callable */
|
||||||
@ -330,7 +330,7 @@ class ObjectSerializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @psalm-suppress ParadoxicalCondition */
|
/** @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);
|
settype($data, $class);
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ class FormatTest {
|
|||||||
/** @var double $double */
|
/** @var double $double */
|
||||||
private $double;
|
private $double;
|
||||||
|
|
||||||
/** @var Decimal $decimal */
|
/** @var float $decimal */
|
||||||
private $decimal;
|
private $decimal;
|
||||||
|
|
||||||
/** @var string $string */
|
/** @var string $string */
|
||||||
|
@ -6,7 +6,7 @@ Name | Type | Description | Notes
|
|||||||
**id** | **int** | | [optional]
|
**id** | **int** | | [optional]
|
||||||
**petId** | **int** | | [optional]
|
**petId** | **int** | | [optional]
|
||||||
**quantity** | **int** | | [optional]
|
**quantity** | **int** | | [optional]
|
||||||
**shipDate** | [**\DateTime**](\DateTime.md) | | [optional]
|
**shipDate** | **\DateTime** | | [optional]
|
||||||
**status** | **string** | Order Status | [optional]
|
**status** | **string** | Order Status | [optional]
|
||||||
**complete** | **bool** | | [optional] [default to false]
|
**complete** | **bool** | | [optional] [default to false]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user