-
Notifications
You must be signed in to change notification settings - Fork 689
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
[bug] types with same name mixed up #506
Comments
i think this should be in the openapi repo. but i'm not sure. |
i found the problem, it generate only one definition when two types has the same package name but different import path. main.gopackage main
import (
"io"
"net/http"
"net/http/httptest"
"os"
ad "local.pc/exp/a/d"
bd "local.pc/exp/b/d"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
)
func main() {
c := restful.NewContainer()
ws := &restful.WebService{}
ws.Route(ws.GET("/a").
To(func(r1 *restful.Request, r2 *restful.Response) {}).
Produces(restful.MIME_JSON).
Returns(http.StatusOK, "success", []ad.Response{}))
ws.Route(ws.GET("/b").
To(func(r1 *restful.Request, r2 *restful.Response) {}).
Produces(restful.MIME_JSON).
Returns(http.StatusOK, "success", []bd.Response{}))
c.Add(ws)
wsdocs := restfulspec.NewOpenAPIService(restfulspec.Config{
APIPath: "/apidocs",
WebServices: c.RegisteredWebServices(),
})
c.Add(wsdocs)
rr := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "http://localhost.localdomain/apidocs", http.NoBody)
c.ServeHTTP(rr, r)
res := rr.Result()
p, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
os.WriteFile("apidocs.json", p, 0o644)
} b/d/d.gopackage d
import "local.pc/exp/c"
type Response struct {
c.Response
B int `json:"b"`
} a/d/d.gopackage d
import "local.pc/exp/c"
type Response struct {
c.Response
A int `json:"a"`
} it only generate definition from |
ws.TypeNameHandler(func(sample interface{}) string {
t := reflect.TypeOf(sample)
name := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
name = strings.ReplaceAll(name, "/", "_")
log.Println(name)
return name
}) |
yes , better have it there |
Without looking into the details, I think a handler invocation for Returns is possible. The definition change is a bigger challenge. |
Moved to go-restful-openapi repo. emicklei/go-restful-openapi#100 |
i've go structs like above, for some reason the generated doc contain fields that does not belongs to that type.
change the type name does fix the problem.
same type name with different import path get mixed up for some reason.
i'll make a reproducible example later since the original codebase is quite large.
The text was updated successfully, but these errors were encountered: