php-json-schema-model-generator
php-json-schema-model-generator copied to clipboard
Incorrect native parameter type on nullable setters in mutable models
Describe the bug
Nullable field setters don't correctly account for null when generating typehints.

Again, found by PHPStan.
Expected behavior
setDisableBlockTicking() should accept null.
Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"disable-block-ticking": {
"oneOf": [
{
"type": "array",
"items": {
"type": "integer"
}
},
{
"type": "null"
}
],
"description": "IDs of blocks to disallow ticking.",
"default": null
}
},
"required": [
"chunk-ticking"
]
}
Config: https://github.com/pmmp/DataModels/blob/3f398d966d5180d68e33c8f4054ab592094ea54c/generate-schemas.php
Version: 0.21.5.
This seems to appear in a few other places where anyOf has been used in my schemas, like this one (both getters and setters are wrong in this case):

I've added fixes for this issue and issue #51 to the master branch and tagged version 0.21.6 which includes the fixes.
https://github.com/wol-soft/php-json-schema-model-generator/issues/50#issuecomment-943716585
Unfortunately, this is still reproducible for non-nullable unions. https://github.com/pmmp/DataModels/blob/3825e7a99210302b0b3a2d94641631710f782f6d/src/immutable/PluginManifest.php#L528 https://github.com/pmmp/DataModels/blob/3825e7a99210302b0b3a2d94641631710f782f6d/src/mutable/PluginManifest.php#L582 https://github.com/pmmp/DataModels/blob/3825e7a99210302b0b3a2d94641631710f782f6d/schema/PluginManifest.json#L41-L47
Hi @dktapps,
thanks for the report, again. I'm currently working on a larger refactoring to support various JSON-Schema versions which also includes how types are evaluated to fix issues with composed types. This will fix the issue, when completed.
Cheers.
Alright, thanks for the update.