@@ -40,6 +40,8 @@ const (
4040 Enum ModelKind = "enum"
4141 // Value is a value type
4242 Value ModelKind = "value"
43+ // OneOf is a oneof value
44+ OneOf ModelKind = "oneof"
4345)
4446
4547// Model is a template model for rendering Go code for a given API schema
@@ -55,6 +57,8 @@ type Model struct {
5557 Kind ModelKind
5658 // Properties is a list of type's property descriptors
5759 Properties []PropSpec
60+ // ConvertSpecs contains a list of convert functions for this model
61+ ConvertSpecs []ConvertSpec
5862 // GoType is a string that represents the Go type that follows the model type name.
5963 // For example, `type Name GoType`.
6064 GoType string
@@ -66,6 +70,12 @@ type Model struct {
6670 PackageName string
6771}
6872
73+ // ConvertSpec holds all info to build one As{Type}() function
74+ type ConvertSpec struct {
75+ // TargetGoType is the target type of the conversion
76+ TargetGoType string
77+ }
78+
6979// PropSpec is a Go property descriptor
7080type PropSpec struct {
7181 // Name is a property name in structs, variable name in enums, etc
@@ -131,6 +141,9 @@ func NewModelFromRef(ref *openapi3.SchemaRef) (model *Model, err error) {
131141 model .GoType = "map[string]" + goTypeFromSpec (ref .Value .AdditionalProperties )
132142 }
133143 }
144+ case len (ref .Value .OneOf ) > 0 :
145+ model .Kind = OneOf
146+ model .fillConvertSpecs (ref )
134147 default :
135148 model .Kind = Value
136149 model .GoType = goTypeFromSpec (ref )
@@ -176,6 +189,15 @@ func NewModelFromParameters(params openapi3.Parameters) (model *Model, err error
176189 return model , nil
177190}
178191
192+ func (m * Model ) fillConvertSpecs (ref * openapi3.SchemaRef ) {
193+ for _ , oneOf := range ref .Value .OneOf {
194+ m .ConvertSpecs = append (m .ConvertSpecs , ConvertSpec {
195+ TargetGoType : goTypeFromSpec (oneOf ),
196+ })
197+ }
198+
199+ }
200+
179201// Render renders the model to a Go file
180202func (m * Model ) Render (ctx context.Context , writer io.Writer ) error {
181203 var tpl * template.Template
@@ -187,6 +209,8 @@ func (m *Model) Render(ctx context.Context, writer io.Writer) error {
187209 tpl = enumTemplate
188210 case Value :
189211 tpl = valueTemplate
212+ case OneOf :
213+ tpl = oneOfTemplate
190214 }
191215
192216 err := tpl .Execute (writer , m )
0 commit comments