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

[BUG] [Go-server] invalid composite literal type string

Open Tomer-L opened this issue 2 years 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

Generating the following yaml spec you will get code that does not compile with the following errors:

Error 1: invalid composite literal type string

api_test_api.go on line 63

bodyParam := string{}

Expected code ->

bodyParam := ""

Error 2: "github.com/gorilla/mux" imported and not used

Second compilation error is on file api_test_api.go line 17 There are at least 3 open issues: #8847 #5153 #11097

openapi-generator version

latest stable 7.1.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: test
  description: API specification
  version: 1.0.0
servers:
  - url: /api/v1
paths:
  /collection/test:
    post:
      summary: POST a test batch
      operationId: postTest
      tags:
        - test api
      requestBody:
        $ref: '#/components/requestBodies/TestBody'
      responses:
        '200':
          $ref: '#/components/responses/SuccessfulOp'

components:
  responses:
    SuccessfulOp:
      description: Successful Operation
      content:
        application/json:
          schema:
            type: bool

  requestBodies:
    TestBody:
      description: Test body
      required: true
      content:
        text/plain:
          schema:
            type: string
            format: byte
Generation Details

I used the following command:

java -jar  generate --additional-properties=packageName=test,sourceFolder=test -i collection_test.yaml -g go-server
Steps to reproduce
  1. copy yaml content into a new file
  2. generate using -i file_name.yaml -g go-server
Related issues/PRs

Could not find issues related to "invalid composite literal type string" error

Suggest a fix

Tomer-L avatar Dec 11 '23 14:12 Tomer-L

Encountered this issue as well...

kfircohen123 avatar Jan 29 '24 09:01 kfircohen123

tested with the spec again and looks like the issue has been resolved

please give it a try with the latest master

friendly reminder: OpenAPI 3.1 support is still beta

wing328 avatar Mar 24 '24 03:03 wing328

Is this issue really resolved?

We face the same problem and I tried to test it with the latest master, but I still have the following line generated:

bodyParam := string{}

and the Invalid composite literal type error.

From what I see, this comes from controller-api.mustache on line 594

{{paramName}}Param := {{dataType}}{}

I don't really know how Mustache works, but I think there needs to be a special handling of literal types for body parameters?

merterpam avatar May 22 '24 07:05 merterpam

what about using {{#isPrimitveType}} ... {{/isPrimitiveType}} check ?

wing328 avatar May 22 '24 08:05 wing328

The bug seems to be still in the master branch.

I'm super new, but would maybe this replacement line work better?

var {{paramName}}Param {{dataType}}

This should be equivalent with the := initialization, yet we won't have to deal with a moustache "if" for checking if dataType is a Go "composite" data type.

Did a PR here https://github.com/OpenAPITools/openapi-generator/pull/20467

vingarzan avatar Jan 14 '25 15:01 vingarzan

Since templates are user-replaceable, as temp solution until that PR is accepted/merged/released/etc is to read this https://openapi-generator.tech/docs/templating and then

  1. extract the templates
openapi-generator-cli author template -g go-server
  1. patch controller-api.mustache like I did in the PR
-	{{paramName}}Param := {{dataType}}{}
+	var {{paramName}}Param {{dataType}}
  1. generate with the custom templates using the -t directory-where-templates-are option

vingarzan avatar Jan 14 '25 15:01 vingarzan

thanks for the fix by https://github.com/OpenAPITools/openapi-generator/pull/20467, which has been merged.

please give it a try with the latest master: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-test-with-the-latest-master-of-openapi-generator

wing328 avatar Jan 15 '25 08:01 wing328

@wing328 Many many thanks for your help! I will definitely come back and help in the future!

I tested with the last master and it works as expected.

vingarzan avatar Jan 15 '25 11:01 vingarzan