swagger-api / swagger-api/swagger-codegen

Confusing code generation for GoLang client

Open
#10,221 0 comments 1 reaction 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.