Skip to content

Cannot create optional file type in openapi-generator #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
grokify opened this issue Jun 17, 2018 · 0 comments
Open

Cannot create optional file type in openapi-generator #24

grokify opened this issue Jun 17, 2018 · 0 comments
Labels

Comments

@grokify
Copy link
Owner

grokify commented Jun 17, 2018

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"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant