swagger-api / swagger-api/swagger-codegen

[PHP] nullable property not added to docblock typehint

Open
#10,321 0 comments 1 reaction 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

Properties on objects that are defined as nullable do not receive a nullable typehint in the docblock.

This is the code that is generated with a nullable property:

    /**
     * Gets activation_date
     *
     * @return \DateTime
     */
    public function getActivationDate()
    {
        return $this->container['activation_date'];
    }

But because it is nullable, it should add |null in the docblock:

    /**
     * Gets activation_date
     *
     * @return \DateTime|null
     */
    public function getActivationDate()
    {
        return $this->container['activation_date'];
    }

Because modern IDEs will check the typehint, there should be warnings about a property that might be nullable. Those warnings are never shown to the developers. As well as Static code analysis tools not being able to catch possible errors.

Swagger-codegen version

https://editor.swagger.io/ -> Online codegen: 3.0.20

Swagger declaration file content or url
openapi: 3.0.1
info:
  title: Some API
  version: 1.0.0
servers:
  - url: https://example.com/api
paths:
  /activations/users/{userId}:
    get:
      operationId: getActivationsForUser
      parameters:
        - name: userId
          in: path
          required: true
          schema:
            type: string
      responses:
        200:
          description: Retrieved activations for User
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ActivationsTO'
components:
  schemas:
    ActivationsTO:
      title: ActivationsTO
      type: object
      properties:
        activations:
          type: array
          items:
            $ref: '#/components/schemas/ActivationTO'
          nullable: true
      description: Object containing multiple ActivationTO
    ActivationTO:
      title: ActivationTO
      required:
        - id
      type: object
      properties:
        activationDate:
          type: string
          format: date-time
          nullable: true
        id:
          type: string
          format: uuid
          nullable: false
      description: Activation object

Command line used for generation

(Unknown, using online generator of https://editor.swagger.io/)

Steps to reproduce

Paste the above code into the editor and generate a PHP-Client.

Related issues/PRs

https://github.com/swagger-api/swagger-codegen/pull/8162 does this for non-required parameters, but not for nullable parameters. But this PR is not merged as well

Suggest a fix/enhancement

Use the PR from above, but instead of using required use nullable

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 PHP client generation from the supplied OpenAPI example and compare its nullable-property handling with pull request 8162. Done means generated PHP docblocks include |null for nullable properties such as activationDate and nullable arrays, while non-nullable properties remain unchanged.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.