Skip to content

[BUG] Code generates unreachable branches #5525

Open
@iondiode

Description

@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: [email protected]

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions