question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[GO] Problem with generating go client code, too many structs re-declared

See original GitHub issue
Description

I’m unable to run the following:

java -jar ~/Downloads/openapi-generator-cli-4.0.0-20180728.030217-16.jar generate -i https://products.solace.com/download/PUBSUB_SEMPV2_SCHEMA_JSON -g go
go get -v ./...                                                                                                                                                                                    
github.com/SolaceDev/semp-v2-go
# github.com/SolaceDev/semp-v2-go
./api_msg_vpn.go:149:6: CreateMsgVpnAclProfileOpts redeclared in this block
        previous declaration at ./api_acl_profile.go:41:6
./api_msg_vpn.go:260:6: CreateMsgVpnAclProfileClientConnectExceptionOpts redeclared in this block
        previous declaration at ./api_acl_profile.go:152:6
./api_msg_vpn.go:372:6: CreateMsgVpnAclProfilePublishExceptionOpts redeclared in this block
        previous declaration at ./api_acl_profile.go:264:6
./api_msg_vpn.go:484:6: CreateMsgVpnAclProfileSubscribeExceptionOpts redeclared in this block
        previous declaration at ./api_acl_profile.go:376:6
./api_msg_vpn.go:595:6: CreateMsgVpnAuthorizationGroupOpts redeclared in this block
        previous declaration at ./api_authorization_group.go:41:6
./api_msg_vpn.go:705:6: CreateMsgVpnBridgeOpts redeclared in this block
        previous declaration at ./api_bridge.go:41:6
./api_msg_vpn.go:817:6: CreateMsgVpnBridgeRemoteMsgVpnOpts redeclared in this block
        previous declaration at ./api_bridge.go:153:6
./api_msg_vpn.go:931:6: CreateMsgVpnBridgeRemoteSubscriptionOpts redeclared in this block
        previous declaration at ./api_bridge.go:267:6
./api_msg_vpn.go:1045:6: CreateMsgVpnBridgeTlsTrustedCommonNameOpts redeclared in this block
        previous declaration at ./api_bridge.go:381:6
./api_msg_vpn.go:1157:6: CreateMsgVpnClientProfileOpts redeclared in this block
        previous declaration at ./api_client_profile.go:41:6
./api_msg_vpn.go:1157:6: too many errors
openapi-generator version

openapi-generator-cli-4.0.0-20180728.030217-16.jar including earlier release versions (and swagger 2.4.0 as well)

OpenAPI declaration file content or url

https://products.solace.com/download/PUBSUB_SEMPV2_SCHEMA_JSON

Command line used for generation
java -jar ~/Downloads/openapi-generator-cli-4.0.0-20180728.030217-16.jar generate -i https://products.solace.com/download/PUBSUB_SEMPV2_SCHEMA_JSON -g go
Steps to reproduce
java -jar ~/Downloads/openapi-generator-cli-4.0.0-20180728.030217-16.jar generate -i https://products.solace.com/download/PUBSUB_SEMPV2_SCHEMA_JSON -g go
go get -v ./...
Related issues/PRs

Might be similar: https://github.com/OpenAPITools/openapi-generator/issues/535

Suggest a fix/enhancement

None I can think of at the moment

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
iafiliuscommented, Aug 3, 2021

Hi, a me too report and some additional info.

Using mvn version openapi-generator-cli-5.2.0.jar and latest build openapi-generator-cli-5.2.1-20210719.023544-9.jar and have same (and more ) issues.

4.2.2 works BUT to prevent duplicate structs need to use -p enumClassPrefix=true

5.2.0 or 20210719.023544-9 versions seem to ignore it or some other mechanism generates lots of duplicates.

Can’t remember i’ve seen it with an early 5.0.1 version, but that was almost a year ago when tested.

[vendor provided swagger file used] (https://vdc-download.vmware.com/vmwb-repository/dcr-public/d8e1559c-3fd9-4d48-a895-b1036479cea6/9e2aee74-1a4d-46ba-ac14-8694815b8ce2/swagger-4.4.0-dist.json)

java  -jar ../openapi-generator-cli-5.2.1-20210719.023544-9.jar generate --generate-alias-as-model -i ./swagger440_untagged.json -g go -o velocloudsdkgo440openapi521unt \
      --additional-properties=packageName="velocloudsdkgo440openapi521unt",packageVersion="0.0.0.1" \
      -p enumClassPrefix=true \
      --enable-post-process-file

To make it compilable without duplicates I need to modify the vendor provider with the suggested jq statement:

cat swagger-4.4.0-dist.json| jq '. | select(((.paths[][].tags| type=="array"), length) > 1).paths[][].tags |= [.[0]]' > swagger440_untagged.json

And with that it compiles.

Not sure if it affects anything else at this point. update:“Functions are no longer available in respective api collection (defined by tags), which again could be workaround by hardcoding all tags to same value, instead of first tag found. I found another open issue (#9500) which states based on tags different packages should be created”.

PS: i need the -p enumClassPrefix=true to hide the issue with generating ModMap’s which are not defined anywhere (which happens about just 6 times or so in the swagger spec file), this is for version 4.2.2, 5.2.0 and 5.2.1-20210917

Regards

1reaction
akutzcommented, Dec 30, 2019

FWIW, I wrote a quick Makefile target to remove the duplicate options and fix any vestigial imports:

remove-dupe-opts: ## Removes duplicate Opt structs from the generated code
	@for struct in $$(grep -h 'type .\{1,\} struct' $(OUTPUT_DIR)/*.go | grep Opts  | sort | uniq -c | grep -v '^      1' | awk '{print $$3}'); do \
	  for f in $$(/bin/ls $(OUTPUT_DIR)/*.go); do \
	    if grep -qF "type $${struct} struct" "$${f}"; then \
	      if eval "test -z \$${$${struct}}"; then \
	        echo "skipping first appearance of $${struct} in file $${f}"; \
	        eval "export $${struct}=1"; \
	      else \
	        echo "removing dupe $${struct} from file $${f}"; \
	        tr '\n' '\r' <"$${f}" | sed 's~// '"$${struct}"'.\{1,\}type '"$${struct}"' struct {[^}]\{1,\}}~~' | tr '\r' '\n' >"$${f}.tmp"; \
	        mv -f "$${f}.tmp" "$${f}"; \
	      fi; \
	    fi \
	  done \
	done
	@$(GOLANGCI_LINT) run -v --no-config --fast=false --fix --disable-all --enable goimports $(OUTPUT_DIR)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Go: "instance" redeclared in this block - Stack Overflow
Obviously, the cause is that instance variable is declared in both files. I'm beggining in Go programming and I don't know how should...
Read more >
Effective Go - The Go Programming Language
Introduction. Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different ......
Read more >
Real world advice for writing maintainable Go programs
In Go each variable has a purpose because each variable we declare has to be used within the same scope. Due to Go's...
Read more >
Go: Dependency injection with Wire - Tit Petric
Runtime errors are an unwanted side effect when reflection is used in such a way. The structure of the application doesn't have any...
Read more >
50 Shades of Go: Traps, Gotchas, and Common Mistakes for ...
Another option is to comment out or remove the unused variables :-) Unused Imports. level: beginner. Your code will fail to compile if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found