From 913b9d67499e1050e6cd5572b3d7f039f9dbc38f Mon Sep 17 00:00:00 2001 From: NickUfer Date: Sat, 14 Nov 2020 23:24:46 +0100 Subject: [PATCH 1/2] [php] Fixes problems with API's with too high date-time nanosecond precision --- .../src/main/resources/php/ObjectSerializer.mustache | 10 +++++++++- .../php/OpenAPIClient-php/lib/ObjectSerializer.php | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index 885de6822557..58200ac713d4 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -306,7 +306,15 @@ class ObjectSerializer // be interpreted as a missing field/value. Let's handle // this graceful. if (!empty($data)) { - return new \DateTime($data); + try { + return new \DateTime($data); + } catch (\Exception $exception) { + // Some API's return a date-time with too high nanosecond + // precision for php's DateTime to handle. This conversion + // (string -> unix timestamp -> DateTime) is a workaround + // for the problem. + return (new \DateTime())->setTimestamp(strtotime($data)); + } } else { return null; } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index e64b4cab3155..12bcbe791597 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -316,7 +316,15 @@ public static function deserialize($data, $class, $httpHeaders = null) // be interpreted as a missing field/value. Let's handle // this graceful. if (!empty($data)) { - return new \DateTime($data); + try { + return new \DateTime($data); + } catch (\Exception $exception) { + // Some API's return a date-time with too high nanosecond + // precision for php's DateTime to handle. This conversion + // (string -> unix timestamp -> DateTime) is a workaround + // for this problem. + return (new \DateTime())->setTimestamp(strtotime($data)); + } } else { return null; } From 2750aed2f02f85cbbcbcdfbd38416841f69590f1 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 23 Nov 2020 16:27:28 +0800 Subject: [PATCH 2/2] update samples --- .../petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index 12bcbe791597..9aaec99a5575 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -322,7 +322,7 @@ public static function deserialize($data, $class, $httpHeaders = null) // Some API's return a date-time with too high nanosecond // precision for php's DateTime to handle. This conversion // (string -> unix timestamp -> DateTime) is a workaround - // for this problem. + // for the problem. return (new \DateTime())->setTimestamp(strtotime($data)); } } else {