Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed property must not be accessed before initialization #3

Closed
oojacoboo opened this issue Sep 7, 2024 · 1 comment
Closed

Typed property must not be accessed before initialization #3

oojacoboo opened this issue Sep 7, 2024 · 1 comment

Comments

@oojacoboo
Copy link

oojacoboo commented Sep 7, 2024

@frodeborli thanks for putting together this serializer! While giving it a shot, I'm running into an issue with uninitialized properties. Here is a stacktrace, when processing the Webonyx GraphQL schema:

{
    ...

    {
      "file": "/srv/www/vendor/frodeborli/serializor/Serializor.php",
      "line": 74,
      "function": "serialize",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": [{ "astNode": null, "extensionASTNodes": [ ] }]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 143,
      "function": "transform",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": [[{ "astNode": null, "extensionASTNodes": [ ] }], [ ], null]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 205,
      "function": "transform",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": [{ "astNode": null, "extensionASTNodes": [ ] }, [0], 0]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 261,
      "function": "transform",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": [
        {
          "astNode": null,
          "extensionASTNodes": [ ],
          "config": {
            "query": "Query",
            "mutation": "Mutation",
            "subscription": "Subscription",
            "types": { },
            "directives": null,
            "typeLoader": { },
            "assumeValid": false,
            "astNode": null,
            "extensionASTNodes": [ ]
          },
          "resolvedTypes": [ ],
          "implementationsMap": [ ],
          "fullyLoaded": false,
          "validationErrors": [ ]
        },
        [0, "p"],
        "p"
      ]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 205,
      "function": "transform",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": [
        {
          "query": "Query",
          "mutation": "Mutation",
          "subscription": "Subscription",
          "types": { },
          "directives": null,
          "typeLoader": { },
          "assumeValid": false,
          "astNode": null,
          "extensionASTNodes": [ ]
        },
        [0, "p", "config"],
        "config"
      ]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 261,
      "function": "transform",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": [
        {
          "query": "Query",
          "mutation": "Mutation",
          "subscription": "Subscription",
          "types": { },
          "directives": null,
          "typeLoader": { },
          "assumeValid": false,
          "astNode": null,
          "extensionASTNodes": [ ]
        },
        [0, "p", "config", "p"],
        "p"
      ]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 205,
      "function": "transform",
      "class": "Serializor\\Codec",
      "type": "->",
      "args": ["Query", [0, "p", "config", "p", "query"], "query"]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Codec.php",
      "line": 255,
      "function": "from",
      "class": "Serializor\\Stasis",
      "type": "::",
      "args": ["Query"]
    },
    {
      "file": "/srv/www/vendor/frodeborli/serializor/src/Stasis.php",
      "line": 221,
      "function": "getValue",
      "class": "ReflectionProperty",
      "type": "->",
      "args": ["Query"]
    }
  ],
  "message": "Typed property GraphQL\\Type\\Definition\\ObjectType::$fields must not be accessed before initialization",
  "file": "/srv/www/vendor/frodeborli/serializor/src/Stasis.php",
  "line": 221,
  "class": "Error",
  "user": [ ],
  "tags": [ ],
  "extra": [ ],
  "timestamp": "2024-09-07 03:14:08 UTC",
  "level": "critical",
  "environment": "test",
  "app_version": "6.1.21"
}

I tried setting some default values on some properties in various classes, but it looks like it's going to be a lot of them. PHP's ReflectionProperty has the isInitialized method (https://www.php.net/manual/en/reflectionproperty.isinitialized.php).

I took a quick look into this. However, I'm not sure how the $frozen array is going to hold uninitialized properties. I guess there will need to be a special value assigned to it that can be translated during the unserialization. I didn't get a chance to look into the unserialize in detail to see how that might be done. I suspect it might be tricky to dynamically create uninitialized properties on an object at runtime.

@frodeborli
Copy link
Owner

Thanks! I made a simple test case and fixed the issue. If the value is uninitialized it won't be added to the Stasis::$p array, and then it won't be set on the new instance - leaving it uninitialized. Let me know if there are other problems and perhaps I can fix them too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants