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
59 stars 12 forks source link

Inconsistency of generated code when using different methods to declare nullable structures #52

Open dktapps opened 2 years ago

dktapps commented 2 years ago

Describe the bug

The following diff:

diff --git a/schema/PluginListYml.json b/schema/PluginListYml.json
index 4500662..7d5fdea 100644
--- a/schema/PluginListYml.json
+++ b/schema/PluginListYml.json
@@ -14,17 +14,13 @@
                        "description": "The listed plugins will either be allowed or disallowed based on this setting"
                },
                "plugins": {
-                       "anyOf": [
-                               {
-                                       "type": "array",
-                                       "items": {
-                                               "type": "string"
-                                       }
-                               },
-                               {
-                                       "type": "null"
-                               }
+                       "type": [
+                               "array",
+                               "null"
                        ],
+                       "items": {
+                               "type": "string"
+                       },
                        "default": [],
                        "description": "List of plugins to allow or disallow"
                }

produces the following change: https://github.com/pmmp/DataModels/commit/46eab454f5e3c2498f3a84e8065ed9702f27a58f

You can see that there's much less generated code for some reason, but what bothers me more is that the ?array native types disappeared from both getters and setters.

Expected behavior The code should be the same in both cases, since they are semantically equivalent to the best of my understanding.

However, more importantly, the native types should not be missing after the change: the calculated types are exactly the same.

Schema https://github.com/pmmp/DataModels/blob/46eab454f5e3c2498f3a84e8065ed9702f27a58f/schema/PluginListYml.json

https://github.com/pmmp/DataModels/blob/46eab454f5e3c2498f3a84e8065ed9702f27a58f/generate-schemas.php

Version: 0.12.7.

wol-soft commented 2 years ago

see https://github.com/wol-soft/php-json-schema-model-generator/issues/50#issuecomment-981798975