swagger-api / swagger-api/swagger-codegen

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

Open
#12,450 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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

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 with the supplied OpenAPI schema and the PHP generation command using swagger-codegen-cli-v3:3.0.61. Trace how referenced non-object schemas are handled during PHP model generation; done means UserId is not emitted as an empty model and User.id uses the underlying string type.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.