OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.96k stars 6.59k forks source link

[BUG][python] Query Param of type String restricted by Enum triggers AttributeError (str has no attribute value) #16071

Open MAKOMO opened 1 year ago

MAKOMO commented 1 year ago

Bug Report Checklist

Description

The python generator produces stubs which fail with an AttributeError on a query parameter of type string restricted by an enum.

openapi-generator version

I tested this on the current v7.0.0-beta as well as with todays trunk using the python (was python-nextgen) generator. This worked with the python and python-experimental generators of v6.0.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: ""
  version: ""
paths:
  /items:
    get:
      operationId: getHidden
      summary: Get all hidden
      description: Returns all hidden entries
      parameters:
        - $ref: "#/components/parameters/hidden"
      responses:
        200:
            description: OK
components:
  parameters:
    hidden:
      name: hidden
      in: query
      schema:
        type: string
        enum:
          - only
          - 'true'
Generation Details
# java -jar openapi-generator-7.0.0-beta/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i openapi-schema.yaml -g python

# java -jar openapi-generator-master-2023-07-11/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i openapi-schema.yaml -g python
Steps to reproduce
import time
import os
import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint

configuration = openapi_client.Configuration(
    host = "http://localhost"
)

with openapi_client.ApiClient(configuration) as api_client:
    api_instance = openapi_client.DefaultApi(api_client)
    api_instance.get_hidden(hidden='only')

results in

... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "pydantic/decorator.py", line 40, in pydantic.decorator.validate_arguments.validate.wrapper_function
  File "pydantic/decorator.py", line 134, in pydantic.decorator.ValidatedFunction.call
  File "pydantic/decorator.py", line 206, in pydantic.decorator.ValidatedFunction.execute
  File "./openapi_client/api/default_api.py", line 74, in get_hidden
    return self.get_hidden_with_http_info(hidden, **kwargs)  # noqa: E501
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/decorator.py", line 40, in pydantic.decorator.validate_arguments.validate.wrapper_function
  File "pydantic/decorator.py", line 134, in pydantic.decorator.ValidatedFunction.call
  File "pydantic/decorator.py", line 206, in pydantic.decorator.ValidatedFunction.execute
  File "./openapi_client/api/default_api.py", line 149, in get_hidden_with_http_info
    _query_params.append(('hidden', _params['hidden'].value))
                                    ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'value'
Related issues/PRs

Possible related to Issue #14524

Suggest a fix

Sorry

mofr commented 1 year ago

Faced the same problem. The type hint for parameter is StrictStr, but the code inside tries to access value attribute. I would expect the type hint to be a enum.