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

[8.1] No serialization of the enum values for path parameters #113

Closed curious-teapot closed 3 months ago

curious-teapot commented 3 months ago

Hello. I'm having trouble using enums as path parameters. The issue arises when making a request.

Error:

Object of class Group\SomeApiClient\Schema\ResourceTypeEnum could not be converted to string

As far I can tell the problem is that the strtr function cannot accept enum value as an argument

strtr('v1/{resource-type}/resource', ['{resource-type}' => $this->resourceType]);

Example schema

openapi: 3.0.0
info:
  version: 1.0.0
  title: Example API
paths:
  '/v1/{resource-type}/resource':
    parameters:
      - $ref: '#/components/parameters/ResourceType'
    get:
      operationId: getResource
      responses:
        '201':
          description: OK

components:
  parameters:
    ResourceType:
      name: resource-type
      in: path
      required: true
      schema:
        type: string
        enum:
          - one
          - two

client generation parameters

docker run -it \
-v ./openapi.yaml:/openapi.yaml:ro \
-v ./client:/client \
-e NAMESPACE=Group\\SomeApiClient \
-e OPENAPI=/openapi.yaml \
-e OUTPUT_DIR=/client \
-e PACKAGE=group/some-api-client \
-e CLIENT_PHP_VERSION=8.1 \
dhlabs/api-client-generator:10.8

Generated request

<?php

declare(strict_types=1);

/*
 * This file was generated by docler-labs/api-client-generator.
 *
 * Do not edit it manually.
 */

namespace Group\SomeApiClient\Request;

use Group\SomeApiClient\Schema\ResourceTypeEnum;

class GetResourceRequest implements RequestInterface
{
    private string $contentType = '';

    public function __construct(private readonly ResourceTypeEnum $resourceType)
    {
    }

    public function getContentType(): string
    {
        return $this->contentType;
    }

    public function getMethod(): string
    {
        return 'GET';
    }

    public function getRoute(): string
    {
        return strtr('v1/{resource-type}/resource', ['{resource-type}' => $this->resourceType]);
    }

    public function getQueryParameters(): array
    {
        return [];
    }

    public function getRawQueryParameters(): array
    {
        return [];
    }

    public function getCookies(): array
    {
        return [];
    }

    public function getHeaders(): array
    {
        return [];
    }

    public function getBody()
    {
        return null;
    }
}
vsouz4 commented 3 months ago

thx @curious-teapot for reporting the issue, fix is released!