-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
[BUG]: PHP generator produces non-nullable DateTime for nullable date-time fields
Issue Type
Issue with quicktype output
Context (Environment, Version, Language)
- Input Format: JSON Schema
- Output Language: PHP
- CLI, npm, or app.quicktype.io: CLI (via npx)
- Version: 23.2.6
Description
When a JSON Schema field has "type": ["string", "null"] with "format": "date-time", the PHP generator produces non-nullable DateTime type hints for property declarations, constructor parameters, and getter/sample method return types. This causes PHP type errors when null values are passed at runtime, despite the schema explicitly allowing them.
The from* and to* method bodies correctly handle null (they have is_null checks), but the type declarations are wrong, so PHP's type system rejects null values before the method body is even reached.
Input Data
Minimal reproducing schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Example",
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"checkIn": {
"type": ["string", "null"],
"format": "date-time",
"description": "Check-in date and time"
}
},
"required": []
}
}
},
"required": ["items"]
}Command:
npx quicktype --src-lang schema --lang php --with-get --top-level Example --out Example.php Example.jsonExpected Behaviour / Output
Property declarations and method signatures should use nullable ?DateTime:
private ?DateTime $checkIn;
public function __construct(?DateTime $checkIn) { ... }
public static function fromCheckIn(?string $value): ?DateTime { ... }
public static function validateCheckIn(?DateTime $value): bool { ... }
public function getCheckIn(): ?DateTime { ... }
public static function sampleCheckIn(): ?DateTime { ... }Current Behaviour / Output
Property declarations and method signatures use non-nullable DateTime, contradicting the null handling in the method bodies:
private DateTime $checkIn;
public function __construct(DateTime $checkIn) { ... }
public static function fromCheckIn(?string $value): DateTime { // accepts ?string but returns DateTime
if (!is_null($value)) {
// ...
} else {
return null; // <-- type error: returning null from DateTime return type
}
}
public static function validateCheckIn(DateTime $value): bool { // can't pass null
if (!is_null($value)) { // <-- dead code: null can never reach here
// ...
}
}
public function getCheckIn(): DateTime { ... }
public static function sampleCheckIn(): DateTime { ... }Note that fromCheckIn has a return null in the else branch but declares a non-nullable DateTime return type, which is a PHP type error.
Steps to Reproduce
- Create a JSON Schema with a property using
"type": ["string", "null"]and"format": "date-time" - Run
npx quicktype --src-lang schema --lang php --with-get --top-level Example --out Example.php schema.json - Observe that
DateTimetype hints are non-nullable despite the schema allowing null
Possible Solution
When the PHP renderer encounters a union type that includes date-time and null, it should emit ?DateTime for property types, constructor parameters, and method return types, consistent with how it already handles other nullable types (e.g. nullable enums correctly produce ?EnumType).