swagger-api / swagger-api/swagger-codegen
[Go] Invalid code generation in version 2 with two files in multipart/form-data
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Swagger-codegen version
Version 2.4.12 against https://github.com/evankanderson/harbor/raw/walk-with-a/docs/build-customize-contribute/swagger.yaml
(corrected version of the harbor project's swagger definition)
Swagger declaration file content or url
The relevant part is this post (the entire file is 6000 lines long)
post:
summary: Upload a chart file to the specified project.
description: 'Upload a chart file to the specified project. With this API, the corresponding provance file can be uploaded together with chart file at once.'
tags:
- Products
- Chart Repository
consumes:
- multipart/form-data
parameters:
- name: repo
in: path
type: string
required: true
description: The project name
- name: chart
in: formData
type: file
required: true
description: The chart file
- name: prov
in: formData
type: file
required: false
description: The provance file
Command line used for generation
swagger-codegen generate -i https://github.com/evankanderson/harbor/raw/master/docs/build-customize-contribute/swagger.yaml -l go -o pkg/harbor -DpackageName=harbor
Steps to reproduce
Attempt to include the generated library; the build will fail with:
../pkg/harbor/api_chart_repository.go:85:6: localVarFile redeclared in this block
previous declaration at ../pkg/harbor/api_chart_repository.go:78:6
../pkg/harbor/api_chart_repository.go:911:6: localVarFile redeclared in this block
previous declaration at ../pkg/harbor/api_chart_repository.go:904:6
(and another 8 errors)
Note that this error may be masked by missing includes of "github.com/antihax/optional" in several files; I added these manually but the generated code is clearly incorrect:
var localVarFile *os.File
if localVarFile != nil { // this is a no-op, but it's fixed by #10132
fbs, _ := ioutil.ReadAll(localVarFile)
localVarFileBytes = fbs // Declared at the top of the block (singleton)
localVarFileName = localVarFile.Name()
localVarFile.Close()
}
var localVarFile *os.File // Hey, look ma! I declared this above
if localVarOptionals != nil && localVarOptionals.Prov.IsSet() {
localVarFileOk := false // Sort of odd, since this is reassigned immediately
localVarFile, localVarFileOk = localVarOptionals.Prov.Value().(*os.File)
if !localVarFileOk {
return nil, reportError("prov should be *os.File")
}
}
if localVarFile != nil {
fbs, _ := ioutil.ReadAll(localVarFile)
localVarFileBytes = fbs
localVarFileName = localVarFile.Name()
localVarFile.Close()
}
Related issues/PRs
Related to:
- https://github.com/swagger-api/swagger-codegen/pull/8051
- https://github.com/swagger-api/swagger-codegen/pull/10132
Suggest a fix/enhancement
I'm not quite sure the best choice here, but I suspect it is to use the mime/multipart package in this case.
Also, it seems like api.mustache needs to conditionally import "github.com/antihax/optional".
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/swagger-codegen/src/main/resources/go/api.mustache and reproduce generation with the command and Swagger definition in the issue. Inspect the generated api_chart_repository.go and run the Go build; done means multipart/form-data with two files generates without localVarFile redeclarations and includes the optional dependency only when needed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100