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

オープン
#358 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
go
領域
api, tooling

調査の方向性

まず、oneOf のバリアントと参照されたスキーマを処理する Go SDK ジェネレーターのロジックを特定し、次にこの issue で説明されているスキーマを使って例を再現します。生成される struct では、余分な中間値のネストを避け、マーシャリングとアンマーシャリングが、誤解を招く中間 struct を必要とせずに、API に示されている JSON 構造と一致するようにします。

索引モデルが 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分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

oxidecomputer/oxide.go のほかの issue

oxidecomputer/oxide.go の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。