[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.