swagger-api / swagger-api/swagger-codegen
Confusing code generation for GoLang client
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
I have just generated an API client for a ASP .NET web app with the spec generated from Swashbuckle.
Browsing the generated code I came across an odd conditional that doesn't make much sense.
The conditional starts with >= 300 but includes an inner conditional with == 200?
if localVarHttpResponse.StatusCode >= 300 {
newErr := GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
if localVarHttpResponse.StatusCode == 200 {
var v MinimalArticleDto
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHttpResponse, newErr
}
newErr.model = v
return localVarReturnValue, localVarHttpResponse, newErr
}
if localVarHttpResponse.StatusCode == 404 {
var v ModelMap
err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
if err != nil {
newErr.error = err.Error()
return localVarReturnValue, localVarHttpResponse, newErr
}
newErr.model = v
return localVarReturnValue, localVarHttpResponse, newErr
}
return localVarReturnValue, localVarHttpResponse, newErr
}
I've stared at it for while thinking I am missing something but the check for a 200 seems pointless considering the previous check for >= 300?
If someone could explain the logic behind this it'd be great?
Swagger-codegen version
Forked from 3.0.0
Swagger declaration file content or url
Not relevant as this is an issue related to a template
Command line used for generation
Not relevant as this is an issue related to a template
Steps to reproduce
Not relevant as this is an issue related to a template
Related issues/PRs
Not relevant as this is an issue related to a template
Suggest a fix/enhancement
Not relevant as this is an issue related to a template
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 by locating the Go client template that generates the shown status-code conditional, then trace how the 200 and 404 response branches are emitted. Compare the generated output with the template logic and confirm that the resulting conditional is coherent, with a regression check covering the corrected generation behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100