OpenAPITools / OpenAPITools/openapi-generator

[BUG][PHP] Deserialization of object without properties leads to wrong data type

Open
#21,412 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I am generating the PHP code for an external proprietary API. The OpenAPI spec contains a schema of type object but without any properties.

The responses of the API, which contains the empty object as {} is casted to an empty array [] in the ObjectSerializer.
Now, I need to pass a part of the deserialized object to another endpoint of the API. But when the ObjectSerializer serializes the empty array, the result is []. As this is another data type the API refuses the request.

openapi-generator version

Version 7.11.0, but it also happens in master.

OpenAPI declaration file content or url

Here are two snippets of the OpenAPI spec:

{
  "openapi": "3.0.1",
  "components": {
    "schemas": {
      "Agreement": {
        "type": "object",
        "properties": {
          "schufaClause": {
            "$ref": "#/components/schemas/AgreementSchufaClause"
          }
        }
      },
      "AgreementSchufaClause": {
        "type": "object"
      }
    }
  }
}
Suggest a fix

I suggest to change this behaviour of the deserialization of empty objects. PHP's equivalent of an empty object is an instance of stdClass and not an array. So it should just be returned as is - without any casting.

Although this is a edge case that should rarely happen it could potentially break existing code. What do you think about that?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by inspecting the generated PHP ObjectSerializer deserialization path using the supplied OpenAPI snippets, focusing on the schema whose type is object without properties. Reproduce the conversion of {} and its subsequent serialization; done means an empty object retains the correct PHP object type and round-trips as {} without breaking the API request.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.