php-json-schema-model-generator icon indicating copy to clipboard operation
php-json-schema-model-generator copied to clipboard

Incorrect native parameter type on nullable setters in mutable models

Open dktapps opened this issue 4 years ago • 5 comments

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 avatar Oct 14 '21 20:10 dktapps

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

dktapps avatar Oct 14 '21 20:10 dktapps

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

wol-soft avatar Oct 22 '21 15:10 wol-soft

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

dktapps avatar Nov 15 '21 23:11 dktapps

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.

wol-soft avatar Nov 29 '21 16:11 wol-soft

Alright, thanks for the update.

dktapps avatar Nov 29 '21 17:11 dktapps