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][GO] openapi-generator generates invalid Go code for response code ranges (e.g. 5XX) #6829

Open
5 of 6 tasks
edganiukov opened this issue Jul 1, 2020 · 5 comments
Open
5 of 6 tasks

Comments

@edganiukov
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
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
Copy link
Member

wing328 commented Jul 2, 2020

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

@edganiukov
Copy link
Author

edganiukov commented Jul 2, 2020

@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
Copy link
Member

wing328 commented Jul 2, 2020

👌

Would you have time to contribute a fix?

I can show you some good starting points.

@aljacob
Copy link

aljacob commented Jul 3, 2020

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
Copy link
Member

wing328 commented Sep 9, 2020

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.

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

3 participants