openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [go-server] "undefined" string defaults due to unquoted string literal

Open chrisjpalmer opened this issue 1 year ago • 4 comments

Bug Report Checklist

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

When using go-server to generate code in v7.2.0+, if a parameter includes a string default, the default value is not rendered in the generated go code with quotes. This leads to the following error when compiling:

out/openapi/go/api_default.go:67:12: undefined: ASC
openapi-generator version

v7.2.0-v7.5.0 and master - has the bug v7.1.0 - doesn't have the bug

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: API
  version: 1.0.0
servers:
  - url: https://example.com
paths:
  /foo:
    get:
      parameters:
      - in: query
        name: order
        schema:
          default: ASC
          type: string
      responses:
        "200":
          description: "OK"
Generation Details

Example repository here: https://github.com/chrisjpalmer/openapi-defaults-bug/commit/8b3833273db6eab42e8fb12cea21100d00dd41d5

Expected:

var orderParam string
if query.Has("order") {
	param := query.Get("order")

	orderParam = param
} else {
	param := "ASC"
	orderParam = param
}

Actual:

var orderParam string
if query.Has("order") {
	param := query.Get("order")

	orderParam = param
} else {
	param := ASC
	orderParam = param
}
Steps to reproduce
  1. create a string parameter with a default
  2. generate code with go-server
Related issues/PRs

Can't find any directly related.

Suggest a fix

I believe the bug was introduced here: https://github.com/OpenAPITools/openapi-generator/pull/17321/files#diff-e77c0a7e68c4132e7b3156262b74ee2c92aed8560d1e170ad0df249bc9d5a7fdR515

The solution would be to put quotes around the default value if it is a string... I believe.

chrisjpalmer avatar Apr 24 '24 02:04 chrisjpalmer

thanks for reporting the issue

@lwj5 I wonder if you can take a look as it seems to be related to https://github.com/OpenAPITools/openapi-generator/pull/17321

(@lwj5 please also PM me via Slack when you've time. thank you)

wing328 avatar Apr 24 '24 02:04 wing328

Im tied up this week.

I’ll take a look, and also contact you on Monday.

lwj5 avatar Apr 24 '24 02:04 lwj5

ok, take your time.

i may take a look later this week if i can find the time

wing328 avatar Apr 24 '24 02:04 wing328

Thanks for looking into this :)

chrisjpalmer avatar Apr 24 '24 03:04 chrisjpalmer