Skip to content

Allow DateTime mapping under bStrictObjectTypeChecking when input is …#254

Closed
djairhogeuens wants to merge 1 commit into
cweiske:masterfrom
djairhogeuens:fix/issue-240-datetime-strict-mode
Closed

Allow DateTime mapping under bStrictObjectTypeChecking when input is …#254
djairhogeuens wants to merge 1 commit into
cweiske:masterfrom
djairhogeuens:fix/issue-240-datetime-strict-mode

Conversation

@djairhogeuens
Copy link
Copy Markdown
Contributor

…a strict ISO 8601 string

Closes #240.

bStrictObjectTypeChecking exists to refuse scalar-to-object conversion because PHP constructors that trust their input can be exploited. DateTime, however, is the canonical case where a JSON scalar IS the natural wire form, so users currently have to disable the flag globally to map dates -- which exposes every other class.

Add a narrow exemption: when the target class is a \DateTimeInterface subclass AND the value is a strict-shape ISO 8601 / RFC 3339 / date-only string that round-trips through new \DateTimeImmutable() without warnings, fall through to the existing constructor path.

Loose strings the DateTime constructor accepts ("now", "+1month", "yesterday", "2pm") are intentionally still rejected -- they would defeat the security intent of the flag. Semantically impossible inputs ("2024-13-40", "2024-02-30") are caught by DateTime::getLastErrors().

Adds tests/DateTimeStrictTypes_Test (13 tests) and a JsonMapperTest_DateTimes fixture covering both the map() and mapArray() fix sites.

…a strict ISO 8601 string

Closes cweiske#240.

bStrictObjectTypeChecking exists to refuse scalar-to-object conversion
because PHP constructors that trust their input can be exploited.
DateTime, however, is the canonical case where a JSON scalar IS the
natural wire form, so users currently have to disable the flag globally
to map dates -- which exposes every other class.

Add a narrow exemption: when the target class is a \DateTimeInterface
subclass AND the value is a strict-shape ISO 8601 / RFC 3339 / date-only
string that round-trips through new \DateTimeImmutable() without
warnings, fall through to the existing constructor path.

Loose strings the DateTime constructor accepts ("now", "+1month",
"yesterday", "2pm") are intentionally still rejected -- they would
defeat the security intent of the flag. Semantically impossible inputs
("2024-13-40", "2024-02-30") are caught by DateTime::getLastErrors().

Adds tests/DateTimeStrictTypes_Test (13 tests) and a JsonMapperTest_DateTimes
fixture covering both the map() and mapArray() fix sites.
@cweiske
Copy link
Copy Markdown
Owner

cweiske commented May 12, 2026

AI generated code. I won't look at it; see #253.

@cweiske cweiske closed this May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateTime mapping is not very intuitive while it is a common type

2 participants