Avoid generating intermediate structs if `oneOf` value is a `$ref`

未关闭
#358 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
go
领域
api, tooling

调研方向

首先定位处理 oneOf 变体和引用 schema 的 Go SDK 生成器逻辑,然后使用此 issue 中描述的 schema 重现这些示例。生成的 struct 应避免额外的中间值嵌套,并且序列化和反序列化应与 API 展示的 JSON 结构一致,而不需要容易误导的中间 struct。

由索引模型根据 Issue 内容生成。

描述

Enhancement Go SDK
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

主要语言
Go
星标
26
派生
6
平均合并
17 小时 27 分钟
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

oxidecomputer/oxide.go 的其他 Issue

查看 oxidecomputer/oxide.go 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。