Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #5525
Closed
Open
Issue created Mar 04, 2020 by Administrator@rootContributor

[BUG] Code generates unreachable branches

Created by: iondiode

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.

Assignee
Assign to
Time tracking