OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

[BUG][GO] openapi-generator generates invalid Go code for response code ranges (e.g. 5XX) #6829

Open edganiukov opened 4 years ago

edganiukov commented 4 years ago

Bug Report Checklist

Description

For response code ranges (e.g. 1XX, 2XX, 5XX) it generate invalid Go code, for example:

        if localVarHTTPResponse.StatusCode == 404 {
            // ...
        }
        if localVarHTTPResponse.StatusCode == 5XX {  // <-- BUG is here
            // ...
        }
openapi-generator version

v4.3.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: '2.0.0'
  title: API
  description: Description
servers:
- {url: 'https://api-server/v2'}
security:
- bearerAuth: []
paths:
  /login:
    post:
      tags:
        - auth
      summary: Authenticate
      operationId: authenticate
      responses:
        200:
          description: OK
        400:
          description: Bad request. User ID must be an integer and larger than 0.
        401:
          description: Authorization information is missing or invalid.
        404:
          description: A user with the specified ID was not found.
        5XX:
          description: Unexpected error.
      security:
        - basicAuth: []
Command line used for generation

openapi-generator-cli generate --generator-name go \ --input-spec ${OAS_SPEC_FILE} \ --api-package ${GO_API_PACKAGE} --package-name ${GO_PACKAGE} \ --git-host github.com --git-user-id exampleuser --git-repo-id ${GO_ARTIFACT} \ --output "${GO_ARTIFACT}"

wing328 commented 4 years ago

I don't think 5xx is a valid status code. You need to provide something like 500 instead.

edganiukov commented 4 years ago

@wing328 according to the doc it is a valid response status code for openapi3 - https://swagger.io/docs/specification/describing-responses or here http://spec.openapis.org/oas/v3.0.3#patterned-fields-0

Under responses, each response definition starts with a status code, such as 200 or 404. An operation typically returns one successful status code and one or more error statuses. To define a range of response codes, you may use the following range definitions: 1XX, 2XX, 3XX, 4XX, and 5XX.

wing328 commented 4 years ago

👌

Would you have time to contribute a fix?

I can show you some good starting points.

aljacob commented 4 years ago

the same issue is present when generating any java server version. 4XX and 5XX are valid in openapi 3 but not in java. I typically have to replace all occurence with e.g. 400 or 500. I am running the latest 5 version of the generator.

wing328 commented 4 years ago

I've filed #7381 to add wildcardFirstChar to store the first character of the response code (implicit), e.g. it will store 4 if the response code is 4XX. Then we use wildcardFirstChar in different generators to better handle implicit response code.