Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global type override in .swaggo file not working #1131

Closed
bmpotter opened this issue Feb 9, 2022 · 4 comments
Closed

Global type override in .swaggo file not working #1131

bmpotter opened this issue Feb 9, 2022 · 4 comments

Comments

@bmpotter
Copy link

bmpotter commented Feb 9, 2022

Describe the bug
First, thank you for this excellent swaggo project! It is a great productivity enhancement.

The replace statement in .swaggo does not seem to be honored for an external type. I can successfully use swaggertype for every use of that external type in all of my structs, but i want to be able to define the alternate type once in the .swaggo file instead.

To Reproduce
Use this main.go file:

package main

import (
	"fmt"
	"github.com/lib/pq"
)

type MyStruct struct {
	Foo    string         `json:"foo"`    // some field
	Labels pq.StringArray `json:"labels"` // external type not found in .swaggo file

	//Labels pq.StringArray `json:"labels" swaggertype:"array,string"` // this works
}

// @Router /myroute [get]
// @Summary Get something
// @id get
// @Tags MyStruct
// @Produce json
// @Success 200 {object} MyStruct "Resource returned"
func Get() {
	fmt.Println("getting something")
}

// @title My test case
// @version 0.1
// @tag.name MyStruct
func main() {
	fmt.Println("hello")
}

And use this .swaggo file:

// swag does not pick this up, still get msg: cannot find type definition: pq.StringArray
replace github.com/lib/pq.StringArray []string

// Also tried these:
// replace lib/pq.StringArray []string
// replace pq.StringArray []string
// skip github.com/lib/pq.StringArray

Then run $GOPATH/bin/swag init and you'll get this output:

2022/02/09 06:32:08 Using overrides from .swaggo
2022/02/09 06:32:08 Generate swagger docs....
2022/02/09 06:32:08 Generate general API Info, search dir:./
2022/02/09 06:32:08 Generating main.MyStruct
2022/02/09 06:32:08 ParseComment error in file /Users/bp/src/POCs/swaggo-file-bug/main.go :cannot find type definition: pq.StringArray

Expected behavior
It should run without error, and in the generated docs/swagger.* files it should define the labels field as a string array.

Your swag version
The same problem occurs in both:
v1.7.8
v1.7.9-p1

Your go version
1.17.5

Desktop:
MacOS Big Sur 11.6

@ubogdan
Copy link
Contributor

ubogdan commented Feb 23, 2022

Hi, this is a quite new feature and for sure it's not perfect. This is also an open-source project and everything was realized with the community contribution.
If you find this feature useful and you have some spare time feel free to contribute and I will be more than happy to assist you with a code review.

@ubogdan
Copy link
Contributor

ubogdan commented Jun 18, 2022

duplicate #1208

implemented in #1209 and you will be able to benefit from it in v1.8.4.

@bmpotter
Copy link
Author

bmpotter commented Aug 6, 2022

It works in v1.8.4, thx! This issue can be closed.

@ubogdan ubogdan closed this as completed Aug 9, 2022
@Vishvajeet590
Copy link

@bmpotter how did it work for you? I get

:cannot find type definition: []string

when I have replace pq.StringArray []string in .swaggo file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants