openapi-generators / openapi-generators/openapi-python-client

Incorrect generation of `_parse_response `

Đang mở
#1,125 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
2k
Fork
293
Merge trung bình
34 phút
Pull request đã merge (30 ngày)
1

Mô tả

Lovely package BTW!

Describe the bug
Code generation for parsing API response does not generate the code that creates the Python model after the response is completed. This happens when using a $ref to define the response instead of just defining the response body inside the path definition.

OpenAPI Spec File

openapi: 3.0.3

info:
  title: Example
  description: |
    Example API definition.
  version: 1.0.0

security:
    - BearerAuth: []

paths:

  /v2/task/{task_id}:
    get:
      summary: Get the summary of a task.
      responses:
        '200':
          $ref: '#/components/responses/task_summary'
        '400':
          $ref: '#/components/responses/bad_request'
        '403':
          $ref: '#/components/responses/not_authorized'
      parameters:
        - $ref: '#/components/parameters/task_id'

components:

  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

  parameters:
    task_id:
      name: task_id
      in: path
      description: The unique identifier of the task targeted by the request.
      required: true
      schema:
        $ref: '#/components/schemas/TaskId'

  responses:

    task_summary:
      description: Summary of the task at the time the request was made.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/TaskSummary'

    bad_submit_task_request:
      description: |
        The request is invalid. This may indicate an error when parsing a parameter, or an error when parsing or validating the request body. The response body may contain a JSON array with a list of validation errors.
      content:
        application/json:
          schema:
            type: object
            properties:
              validation_errors:
                type: array
                minItems: 1
                items:
                  type: string

    not_authorized:
      description: |
        User is not authorized to perform this task. 

    bad_request:
      description: |
        The request is invalid. This may indicate an error when parsing a parameter.

  schemas:
  
    TaskId:
      type: string
      minLength: 1
      maxLength: 256
      pattern: "^[a-zA-Z0-9][a-zA-Z0-9-]*$"
      example: '80cf75f2-4700-4006-9203-4376b091ee4e'
      description: A unique identifier for a task. Must not be an empty string.

    TaskSummary:
      type: "object"
      description: Summary of existing task, including task status.
      properties:
        task_status:
          $ref: '#/components/schemas/TaskStatus'
      required:
        - task_status

    TaskStatus:
      type: string
      enum:
        - Created
        - PayloadProcessing
        - Scheduled
        - Completed
        - Cancelled
        - Failed
      description: |
        Reports status of the task. Lifecycle of a normal task is Created -> PayloadProcessing -> Scheduled -> Completed.
        where PayloadProcessing - the task is going trough additional compilation/validation/optimization, 
        Scheduled - the task is in the qpu queue, Completed - the task has been executed.

the code that gets generated here is:

def _parse_response(*, client: Union[AuthenticatedClient, Client], response: httpx.Response) -> Optional[Any]:
    if response.status_code == HTTPStatus.OK:
        return None
    if response.status_code == HTTPStatus.BAD_REQUEST:
        return None
    if response.status_code == HTTPStatus.FORBIDDEN:
        return None
    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None

which is not correct. The additional context shows the expected behavior.

Desktop (please complete the following information):

  • OS: macOS: 14.4.1
  • Python Version: 3.12.1
  • openapi-python-client: 0.21.5 [e.g. 0.1.0]

Additional context
Note if I modify the path:

  /v2/task/{task_id}:
    get:
      summary: Get the summary of a task.
      responses:
        '200':
          description: Summary of the task at the time the request was made.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TaskSummary'
        '400':
          $ref: '#/components/responses/bad_request'
        '403':
          $ref: '#/components/responses/not_authorized'
      parameters:
        - $ref: '#/components/parameters/task_id'

it generates the correct code:

def _parse_response(
    *, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[Union[Any, TaskSummary]]:
    if response.status_code == HTTPStatus.OK:
        response_200 = TaskSummary.from_dict(response.json())

        return response_200
    if response.status_code == HTTPStatus.BAD_REQUEST:
        response_400 = cast(Any, None)
        return response_400
    if response.status_code == HTTPStatus.FORBIDDEN:
        response_403 = cast(Any, None)
        return response_403
    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện client được tạo bằng cách sử dụng đặc tả OpenAPI được cung cấp, so sánh đường dẫn phân tích response khi response được định nghĩa inline thay vì thông qua một $ref. Theo dõi đường dẫn tạo mã xây dựng _parse_response; hoàn thành khi response 200 được tham chiếu tạo ra TaskSummary.from_dict(response.json()), trong khi các status khác vẫn giữ cách xử lý như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
openapi, python
Lĩnh vực
api, tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.