OpenAPITools / OpenAPITools/openapi-generator

[BUG] Code generates unreachable branches

Open
#5,525 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When reviewing client go code generated with this tool, we noticed that for status code responses < 300 the client generates unreachable code paths

openapi-generator version

4.2.2 pulled from maven

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: API
  version: 1.0.0
  description:
    Xx
  contact:
    name: XXX
    url: http://www.xxxs.com/
    email: support@xxx.com

servers:
   - url: http://{url}:{port}/XXX/v1
     variables:
       url:
         default: 'localhost'
       port:
         default: '7999'

tags:
  - name: XXX
    description: Operations 


paths:
  /status:
    get:
      tags:
         - XXX
      summary: Gets the status of the system time service.
      operationId: getStatus
      responses:
        200:
          $ref: '#/components/responses/200Status'
        400:
          $ref: '#/components/responses/400BadRequest'
components:
  schemas:
    Status:
      description:
        Example Status
      type: object
      properties:
        server_ref:
          type: string
          description:
            example
      required:
        - server_ref
  responses:
    400BadRequest:
      description: Bad Request
    200Status:
      description: Successful Status Request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Status'

Command line used for generation
	java -jar tools/${OPENAPIGEN_JAR} generate \
		-i bug.yaml \
		-o bug \
		-g go
Steps to reproduce

Run tool as above.

snippet from api_xxx.go


	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		if localVarHTTPResponse.StatusCode == 200 {
			var v Status
			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
	}
Suggest a fix

This looks to be in the original mustache file in the swagger repo. Minimially those branches should be not generated if it is expected that they will be handled by other code. I'm not sure how this is affected by wildcards.

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 with the Go generator and the original Mustache template mentioned in the issue, then reproduce the output using bug.yaml and the provided Java command. Compare the generated api_xxx.go branch for the 200 response with the error handling around it. Done means the generated client no longer contains unreachable response branches while preserving handling for error responses.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.