wol-soft / php-json-schema-model-generator

Creates (immutable) PHP model classes from JSON-Schema files including all validation rules as PHP code
MIT License
61 stars 13 forks source link

Incorrect native parameter type on nullable setters in mutable models #50

Open dktapps opened 3 years ago

dktapps commented 3 years ago

Describe the bug Nullable field setters don't correctly account for null when generating typehints. image

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.

dktapps commented 3 years ago

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): image

wol-soft commented 3 years ago

I've added fixes for this issue and issue #51 to the master branch and tagged version 0.21.6 which includes the fixes.

dktapps commented 2 years ago

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

wol-soft commented 2 years ago

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.

dktapps commented 2 years ago

Alright, thanks for the update.