Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Moshi and Gson JSON adapters fail for messages with a map<bool, V> field

Đang mở Phù hợp với người mới
#3,732 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@sebxt đang làm issue này rồi.

Từ ngày 19/10/2026.

  • #3733 của @sebxt — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
70/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
kotlin
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Bắt đầu bằng cách đọc JsonIntegration.mapKeyJsonFormatter() và fixture map_types.proto hiện có; issue nêu rõ phần xử lý ProtoAdapter.BOOL còn thiếu và kiểu khóa chưa được kiểm thử. Kiểm tra cách các JSON adapter định dạng và phân tích các khóa map khác, sau đó bổ sung kiểm thử cho khóa bool ở cả Moshi và Gson. Hoàn thành khi các khóa bool của map được tuần tự hóa thành "true" hoặc "false" và đọc lại thành công.

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

Mô tả

What happens

Any message that has a map field with a bool key cannot be used with WireJsonAdapterFactory (Moshi) or WireTypeAdapterFactory (Gson). Building the adapter throws, even when the map is empty:

java.lang.IllegalStateException: Unexpected map key type: class kotlin.Boolean
    at com.squareup.wire.internal.JsonIntegration.mapKeyJsonFormatter(JsonIntegration.kt)

What should happen

bool is a valid map key type in proto2 and proto3, and the schema accepts it. The proto3 JSON mapping encodes map keys as strings, so a bool key is written as "true" or "false" and read back from those strings.

Repro

syntax = "proto3";

message MapBoolKeys {
  map<bool, string> map_bool_string = 1;
}
val moshi = Moshi.Builder().add(WireJsonAdapterFactory()).build()
val value = MapBoolKeys(map_bool_string = mapOf(true to "yes", false to "no"))
moshi.adapter(MapBoolKeys::class.java).toJson(value) // throws IllegalStateException

The same happens with Gson through WireTypeAdapterFactory.

Root cause

JsonIntegration.mapKeyJsonFormatter() handles STRING and every integer key adapter but has no branch for ProtoAdapter.BOOL, so it falls through to error("Unexpected map key type: ..."). The existing map_types.proto fixture covers all key types except bool.

Ngôn ngữ chính
Kotlin
Star
4.4k
Fork
635
Merge trung bình
7 giờ 27 phút
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

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.

Issue khác của square/wire

Tất cả issue của square/wire

Issue tương tự

Thêm issue về Kotlin

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.