OpenAPITools / OpenAPITools/openapi-generator

[BUG][python] string format password throws AttributeError: 'SecretStr' object has no attribute 'to_dict'

Open
#16,086 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [x ] Have you provided a full/minimal spec to reproduce the issue?
  • [ x] Have you validated the input using an OpenAPI validator (example)?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [ x] Have you searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Calling a method with a credential object holding a password attribute of type string with format: password fails in the generated Python client code.

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.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: ""
  version: ""
paths:
  /accounts/users/authenticate:
    post:
      operationId: authenticate
      summary: Authenticate user
      description: Authenticates user and returns bearer token
      tags:
        - account
      security: []
      requestBody:
        description: User credentials
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Credentials"
      responses:
        200:
            description: OK
components:
  schemas:
    Credentials:
      type: object
      properties:
        email:
          type: string
          description: email address
          example: me@work.com
        password:
          type: string
          format: password
          description: password
          example: secret
      required:
        - email
        - password
Generation Details
java -jar 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.AccountApi(api_client)
    credentials = openapi_client.Credentials(email="me@email.com", password="123")
    api_response = api_instance.authenticate(credentials)

fails with

Traceback (most recent call last):
  File "<stdin>", line 5, 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/account_api.py", line 73, in authenticate
    return self.authenticate_with_http_info(credentials, **kwargs)  # noqa: E501
[ cut ]
  File "./openapi_client/api_client.py", line 291, in sanitize_for_serialization
    obj_dict = obj.to_dict()
               ^^^^^^^^^^^
AttributeError: 'SecretStr' object has no attribute 'to_dict'

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

Generate the Python client from the OpenAPI declaration and reproduce the failure with Credentials in the generated account_api.py. Start in api_client.py at sanitize_for_serialization and inspect how the generated password field is represented; done means the authenticate call serializes the credentials without the SecretStr AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.