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
  • #11173
Closed
Open
Issue created Dec 21, 2021 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG] [go-server] GET Query enum incorrectly cast

Created by: kellrott

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating the query.Get commands for the go-server api controller the code incorrectly assumes that the results of query.get (which is a string) can be passed directly to the interface, which may be a subtype of string, ie something defined by type MyEnum string.

openapi-generator version

v5.3.1

OpenAPI declaration file content or url

openapi: 3.0.1

info:
  title: minimal example
  version: "0.1"

servers:
- url: /

paths:
  /list:
    get:
      operationId: DoStuff
      parameters:
      - name: state
        in: query
        schema:
          $ref: '#/components/schemas/myEnum'
      responses:
        200:
          description: ""
          content:
            application/json:
              schema:
                type: string

components:
  schemas:
    myEnum:
      type: string
      default: ENUM_1
      enum:
      - ENUM_1
      - ENUM_2
      - ENUM_3
      - ENUM_4
Generation Details

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i test.yaml -g go-server -o tmp

Steps to reproduce

Go to generated code directory, run the command go run ./main.go. The compiler should report

go/api_default.go:66:44: cannot use stateParam (type string) as type MyEnum in argument to c.service.DoStuff
Related issues/PRs
Suggest a fix

In modules/openapi-generator/src/main/resources/go-server/controller-api.mustache, line 136:

-       {{paramName}}Param := {{#isArray}}strings.Split({{/isArray}}query.Get("{{baseName}}"){{#isArray}}, ","){{/isArray}}
+       {{paramName}}Param := {{^isString}}{{dataType}}( {{/isString}}{{#isArray}}strings.Split({{/isArray}}query.Get("{{baseName}}"){{#isArray}}, ","){{/isArray}}{{^isString}} ){{/isString}}

This will change the output from

	stateParam := query.Get("state")
	result, err := c.service.DoStuff(r.Context(), stateParam)

to

	stateParam := MyEnum( query.Get("state") )
	result, err := c.service.DoStuff(r.Context(), stateParam)

wrapping the query.Get call in a conversion to correct the variable type if it is not a string.

Assignee
Assign to
Time tracking