swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[PHP] Codegen generates models for schemas that are not objects

Open javaDeveloperKid opened this issue 1 year ago • 0 comments

Description

I have an endpoint that returns a User object:

paths:
  /users/{id}:
      # ...
      responses:
        200:
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'

And models:

components:
  schemas:
    User:
      type: object
      properties:
        id: 
          $ref: '#/components/schemas/UserId'
        # other properties
    UserId:
      type: string
      format: uuid

This tool generates an empty model class for UserId, i.e. a class without properties. This makes things unusable because when deserializing API response to User object it sets id property to empty UserId object. IMO we should not generate classes for models that are not objects. If a property is a reference to the model that is not an object then we should inherit a type from this model.

Example from PHP: Current behaviour

class User implements ModelInterface, ArrayAccess
{
    protected static $swaggerTypes = [
        'id' => '\Example\Model\UserId'
    ];

    // ...
}

class UserId implements ModelInterface, ArrayAccess
{
    protected static $attributeMap = [
    ];

    public function __construct(array $data = null)
    {
    }

    // ...
}

Expected behaviour

class User implements ModelInterface, ArrayAccess
{
    protected static $swaggerTypes = [
        'id' => 'string'
    ];

    // ...
}
Swagger-codegen version

swaggerapi/swagger-codegen-cli-v3:3.0.61

Swagger declaration file content or url
Command line used for generation
docker run --rm -v "${PWD}" swaggerapi/swagger-codegen-cli-v3:3.0.61 generate -i /swagger.yml -l php
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

javaDeveloperKid avatar Aug 18 '24 17:08 javaDeveloperKid