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

Client generation error when property names differ only by underscore prefix / suffix

Đang mở
#602 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.

🐞bug 🥚breaking
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ả

Originally predicted in https://github.com/openapi-generators/openapi-python-client/issues/383#issuecomment-830839559

Since re.findall(rf"[^{DELIMITERS}]+", value) trims prefix and suffix _ characters while generating python property identifiers, 2+ schema properties on a single model that differ only by such underscores causes CLI generate and update commands to fail.

Given it would be problematic to carry through those untrimmed identifiers due to the semantic meaning of prefix and suffix _ on identifiers in python, it makes sense to have a fallback value to use like a title property or a configurable prefix.

Resulting error:

$ openapi-python-client generate --path schema-underscore-collision.yaml --fail-on-warning

Generating schema-underscore-collision-client
Error(s) encountered while generating, client was not created

black failed

reformatted schema_underscore_collision_client/api/__init__.py
reformatted schema_underscore_collision_client/__init__.py
reformatted schema_underscore_collision_client/types.py
error: cannot format schema_underscore_collision_client/models/get_response_200.py: cannot use --safe with this file; failed to parse source file AST: keyword argument repeated (<unknown>, line 55)
This could be caused by running Black with an older Python version that does not support new syntax used in your source file.
reformatted schema_underscore_collision_client/client.py
reformatted schema_underscore_collision_client/api/default/get.py

Oh no! 💥 💔 💥
5 files reformatted, 2 files left unchanged, 1 file failed to reformat.

Using an example schema-underscore-collision.yaml:

openapi: 3.0.2
info:
  title: schema-underscore-collision 
  version: 1.0.0
paths:
  /:
    get:
      description: 
      responses:
        '200':
          description: test
          content:
            application/json:
              schema:
                type: object
                properties:
                  _a: {}
                  a: {}
                  a_: {}

The problematic schema_underscore_collision_client/models/get_response_200.py file, reported error is in the from_dict method but to_dict also suffers from the ambiguity:

from typing import Any, BinaryIO, Dict, List, Optional, TextIO, Tuple, Type, TypeVar, Union

import attr

from ..types import UNSET, Unset

T = TypeVar("T", bound="GetResponse200")

@attr.s(auto_attribs=True)
class GetResponse200:
    """
    Attributes:
        a (Union[Unset, Any]):
        a (Union[Unset, Any]):
        a (Union[Unset, Any]):
    """

    a: Union[Unset, Any] = UNSET
    a: Union[Unset, Any] = UNSET
    a: Union[Unset, Any] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)


    def to_dict(self) -> Dict[str, Any]:
        a = self.a
        a = self.a
        a = self.a

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({
        })
        if a is not UNSET:
            field_dict["_a"] = a
        if a is not UNSET:
            field_dict["a"] = a
        if a is not UNSET:
            field_dict["a_"] = a

        return field_dict



    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        d = src_dict.copy()
        a = d.pop("_a", UNSET)

        a = d.pop("a", UNSET)

        a = d.pop("a_", UNSET)

        get_response_200 = cls(
            a=a,
            a=a,
            a=a,
        )

        get_response_200.additional_properties = d
        return get_response_200

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties

Versions

  • Python: 3.7.12
  • openapi-python-client: 0.11.1

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

Bắt đầu trong openapi_python_client/utils.py, tại khu vực quanh lệnh gọi findall được liên kết, sau đó lần theo các điểm vào CLI generate và update. Tái hiện xung đột với schema-underscore-collision.yaml và kiểm tra các phương thức to_dict và from_dict của model được tạo. Hoàn tất khi các định danh Python hợp lệ và khác nhau được tạo cho những property như a, a và a, mà không có field hoặc keyword argument trùng lặp.

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ó
4/5
Thời gian dự kiến
3-5 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
35/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.