From 6f6822a1b7937ad937a61aeac62a40bfa03dd046 Mon Sep 17 00:00:00 2001 From: NickUfer Date: Mon, 23 Nov 2020 15:30:57 +0100 Subject: [PATCH] [php][bug] Fixes exceptions with API's with too high date-time nanosecond precision (#7943) * [php] Fixes problems with API's with too high date-time nanosecond precision * update samples Co-authored-by: William Cheng --- .../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 885de682255..58200ac713d 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 e64b4cab315..9aaec99a557 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 @@ 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; }