Skip to content

Commit 6f6822a

Browse files
NickUferwing328
andauthored
[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 <[email protected]>
1 parent 33f4827 commit 6f6822a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,15 @@ class ObjectSerializer
306306
// be interpreted as a missing field/value. Let's handle
307307
// this graceful.
308308
if (!empty($data)) {
309-
return new \DateTime($data);
309+
try {
310+
return new \DateTime($data);
311+
} catch (\Exception $exception) {
312+
// Some API's return a date-time with too high nanosecond
313+
// precision for php's DateTime to handle. This conversion
314+
// (string -> unix timestamp -> DateTime) is a workaround
315+
// for the problem.
316+
return (new \DateTime())->setTimestamp(strtotime($data));
317+
}
310318
} else {
311319
return null;
312320
}

samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,15 @@ public static function deserialize($data, $class, $httpHeaders = null)
316316
// be interpreted as a missing field/value. Let's handle
317317
// this graceful.
318318
if (!empty($data)) {
319-
return new \DateTime($data);
319+
try {
320+
return new \DateTime($data);
321+
} catch (\Exception $exception) {
322+
// Some API's return a date-time with too high nanosecond
323+
// precision for php's DateTime to handle. This conversion
324+
// (string -> unix timestamp -> DateTime) is a workaround
325+
// for the problem.
326+
return (new \DateTime())->setTimestamp(strtotime($data));
327+
}
320328
} else {
321329
return null;
322330
}

0 commit comments

Comments
 (0)