Avoid generating intermediate structs if `oneOf` value is a `$ref`
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Bắt đầu bằng cách xác định logic của trình tạo Go SDK xử lý các biến thể oneOf và các schema được tham chiếu, sau đó tái hiện các ví dụ bằng cách sử dụng các schema được mô tả trong issue này. Các struct được tạo ra không nên có thêm lớp lồng nhau của các giá trị trung gian, và việc marshal và unmarshal phải khớp với cấu trúc JSON được API hiển thị mà không yêu cầu các struct trung gian gây hiểu nhầm.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Overview
Some endpoints have a spec structure where an oneOf variant value is hoisted up.
For example, InstanceNetworkInterfaceCreate has the following schema:
"InstanceNetworkInterfaceCreate": {
"description": "Create-time parameters for an `InstanceNetworkInterface`",
"type": "object",
"properties": {
"description": {
"type": "string"
},
"ip_config": {
"description": "The IP stack configuration...",
"allOf": [
{
"$ref": "#/components/schemas/PrivateIpStackCreate"
}
]
},
...
}
}
...
"PrivateIpStackCreate": {
"description": "Create parameters for a network interface's IP stack.",
"oneOf": [
{
"description": "The interface has only an IPv4 stack.",
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"v4"
]
},
"value": {
"$ref": "#/components/schemas/PrivateIpv4StackCreate"
}
},
"required": [
"type",
"value"
]
},
...
]
}
...
"PrivateIpv4StackCreate": {
"description": "Configuration for a network interface's IPv4 addressing.",
"type": "object",
"properties": {
"ip": {
"description": "The VPC-private address to assign to the interface.",
"allOf": [
{
"$ref": "#/components/schemas/Ipv4Assignment"
}
]
},
"transit_ips": {
"description": "Additional IP networks the interface can send / receive on.",
"default": [],
"type": "array",
"items": {
"$ref": "#/components/schemas/Ipv4Net"
}
}
},
"required": [
"ip"
]
}
With this spec, the JSON structure expected by the API is:
{
"type": "v4",
"value": {
"ip": {
"type": "auto"
},
"transit_ips": []
}
}
For this spec, the Go SDK generates the following structs:
type InstanceNetworkInterfaceCreate struct {
// ...
IpConfig PrivateIpStackCreate `json:"ip_config,omitempty" yaml:"ip_config,omitempty"`
// ...
}
type PrivateIpStackCreate struct {
Type PrivateIpStackCreateType `json:"type,omitempty" yaml:"type,omitempty"`
Value any `json:"value,omitempty" yaml:"value,omitempty"`
}
type PrivateIpStackCreateV4 struct {
Type PrivateIpStackCreateType `json:"type" yaml:"type"`
Value PrivateIpv4StackCreate `json:"value" yaml:"value"`
}
type PrivateIpv4StackCreate struct {
Ip Ipv4Assignment `json:"ip" yaml:"ip"`
TransitIps []Ipv4Net `json:"transit_ips,omitempty" yaml:"transit_ips,omitempty"`
}
A Go SDK user would reasonably expect to build a struct like this to make this request:
netIfCreate := oxide.InstanceNetworkInterfaceCreate{
IpConfig: oxide.PrivateIpStackCreate{
Type: oxide.PrivateIpStackCreateTypeV4,
Value: oxide.PrivateIpStackCreateV4{
Type: oxide.PrivateIpStackCreateTypeV4,
Value: oxide.PrivateIpv4StackCreate{
Ip: oxide.Ipv4Assignment{/* ... */},
},
},
},
}
But this creates a JSON with an unnecessary additional nesting:
{
"type": "v4",
"value": {
- "value": {
"ip": {
"type": "auto"
},
"transit_ips": []
- }
}
}
A similar issue happens when parsing API responses.
type InstanceNetworkInterface struct {
// ...
IpStack PrivateIpStack `json:"ip_stack" yaml:"ip_stack"`
// ...
}
type PrivateIpStack struct {
Type PrivateIpStackType `json:"type,omitempty" yaml:"type,omitempty"`
Value any `json:"value,omitempty" yaml:"value,omitempty"`
}
type PrivateIpStackV4 struct {
Type PrivateIpStackType `json:"type" yaml:"type"`
Value PrivateIpv4Stack `json:"value" yaml:"value"`
}
type PrivateIpv4Stack struct {
Ip string `json:"ip" yaml:"ip"`
TransitIps []Ipv4Net `json:"transit_ips" yaml:"transit_ips"`
}
Users need to unmarshal the returned JSON into a PrivateIpv4Stack instead of PrivateIpStackV4:
switch stack.Type {
case oxide.PrivateIpStackTypeV4:
// Correct.
var parsedStack oxide.PrivateIpv4Stack
if err := json.Unmarshal(marshalled, &parsedStack); err != nil {
// ...
}
// Incorrect.
var parsedStack oxide.PrivateIpStackV4
if err := json.Unmarshal(marshalled, &parsedStack); err != nil {
// ...
}
// ...
So effectively PrivateIpStackCreateV4 and PrivateIpStackV4 should never be used, but this is not clear from the generate Go code. The names os the structs that should and should not be used being so similar, and the use of any to represent enums, add a little more to the confusion.
Implementation details
No response
Anything else you would like to add?
No response
- Ngôn ngữ chính
- Go
- Star
- 26
- Fork
- 6
- Merge trung bình
- 17 giờ 27 phút
- Pull request đã merge (30 ngày)
- 2
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của oxidecomputer/oxide.go
-
Release: Go SDKĐang mởGo SDK Task
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
oxidecomputer/oxide.go#462 ·
-
Validate enum typesĐang mởEnhancement Go SDK
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
oxidecomputer/oxide.go#421 ·
-
Release Process Cleanup: Go SDKĐang mởGo SDK Task
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
oxidecomputer/oxide.go#392 ·
-
Go SDK Task
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
oxidecomputer/oxide.go#372 · 2 bình luận ·
-
Remove `exceptions.go`Đang mởEnhancement Go SDK
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
oxidecomputer/oxide.go#351 · 1 bình luận ·
Tất cả issue của oxidecomputer/oxide.go
Issue tương tự
-
Go - Inconsistent Logs StabilityĐang mởdocs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
open-telemetry/opentelemetry.io#12846 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
hivecommons/hive#19912 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Validate --fetch-interval on startup to prevent panic in time.NewTickerCó thể đã có người làm @Vanshkhandelwal28 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
OneBusAway/watchdog#418 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày