Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Code generates unreachable branches #5525

Open
iondiode opened this issue Mar 4, 2020 · 1 comment
Open

[BUG] Code generates unreachable branches #5525

iondiode opened this issue Mar 4, 2020 · 1 comment

Comments

@iondiode
Copy link

iondiode commented Mar 4, 2020

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.

@auto-labeler
Copy link

auto-labeler bot commented Mar 4, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant