DoclerLabs / api-client-generator

API client generator is a console application capable of generating an API client based on OpenAPI(Swagger) specification.
MIT License
31 stars 19 forks source link

Wrong nullable typehint with nullable object #72

Closed vsouz4 closed 2 years ago

vsouz4 commented 2 years ago

The following openapi spec:

openapi: 3.0.0
info:
  title: Order API
  version: '1'
paths:
  /v1/orders:
    post:
      summary: Place Order
      operationId: placeOrder
      responses:
        '201':
          $ref: '#/components/responses/OrderResponse'
components:
  responses:
    OrderResponse:
      description: Order information
      content:
        application/json:
          schema:
            type: object
            properties:
              data:
                $ref: '#/components/schemas/Order'
            required:
              - data
  schemas:
    Order:
      type: object
      properties:
        id:
          type: string
        domain:
          type: string
          nullable: true
        group:
          type: object
          nullable: true
          properties:
            id:
              type: string
          required:
            - id

generates a wrong Mapper object for the "group" property. The nullable: true attribute on it makes the Mapper::toSchema return a nullable object which is wrong:

// src/Schema/Mapper/OrderMapper.php
        if (isset($payload['group'])) {
            $schema->setGroup($this->orderGroupMapper->toSchema($payload['group']));
        }
...
// src/Schema/Mapper/OrderGroupMapper.php
    public function toSchema(array $payload): ?OrderGroup       // << here should be OrderGroup instead
    {
        $missingFields = \implode(', ', \array_diff(['id'], \array_keys($payload)));
        if (! empty($missingFields)) {
            throw new UnexpectedResponseBodyException('Required attributes for `OrderGroup` missing in the response body: ' . $missingFields);
        }

        return new OrderGroup($payload['id']);
    }

This ::toSchema() always return the OrderGroup so return type hint with ? is wrong.