Skip to content

Cannot create optional file type in openapi-generator #24

@grokify

Description

@grokify
in spec results
formData optional array of files doesn't work
formData optional single file doesn't work
formData required array of files doesn't work
formData required single file works

The openapi-generator spec does have an optional single file endpoint:

https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/go/go-petstore/api/swagger.yaml

Optional Array of Files

YAML spec

          -
            name: "attachment"
            in: "formData"
            description: "File to upload"
            required: false
            type: "array"
            items:
              type: "file"

Generated Code

     * @param "Attachment" (optional.Interface of []*os.File) -  File to upload
type SendFaxMessageOpts struct {
	Attachment    optional.Interface
	FaxResolution optional.String
	SendTime      optional.Time
	IsoCode       optional.String
	CoverIndex    optional.Int32
	CoverPageText optional.String
}
	if localVarOptionals != nil && localVarOptionals.Attachment.IsSet() {
		localVarFormParams.Add("attachment", parameterToString(localVarOptionals.Attachment.Value(), "csv"))
	}

Runtime Error

./fax_send.go:130:21: cannot use files (type []*os.File) as type optional.Interface in assignment

Optional Single File

YAML Spec

          -
            name: "attachment"
            in: "formData"
            description: "File to upload"
            required: false
            type: "file"
     * @param "Attachment" (optional.Interface of *os.File) -  File to upload
type SendFaxMessageOpts struct {
	Attachment    optional.Interface
	FaxResolution optional.String
	SendTime      optional.Time
	IsoCode       optional.String
	CoverIndex    optional.Int32
	CoverPageText optional.String
}
	var localVarFile *os.File
	if localVarOptionals != nil && localVarOptionals.Attachment.IsSet() {
		localVarFileOk := false
		localVarFile, localVarFileOk = localVarOptionals.Attachment.Value().(*os.File)
		if !localVarFileOk {
			return localVarReturnValue, nil, reportError("attachment should be *os.File")
		}
	}
	if localVarFile != nil {
		fbs, _ := ioutil.ReadAll(localVarFile)
		localVarFileBytes = fbs
		localVarFileName = localVarFile.Name()
		localVarFile.Close()
	}

Runtime Error

./fax_send.go:130:21: cannot use files[0] (type *os.File) as type optional.Interface in assignment

Required Array of Files

Compile time error

../../client/api_messages.go:633:131: undefined: os

After resolving the "os" import:

	localVarFormParams.Add("attachment", parameterToString(attachment, "csv"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions