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

[bug] types with same name mixed up #506

Closed
bokunodev opened this issue Jul 31, 2022 · 6 comments
Closed

[bug] types with same name mixed up #506

bokunodev opened this issue Jul 31, 2022 · 6 comments

Comments

@bokunodev
Copy link

type SekResponse struct {
	Meta    datatable.Response
	Results []*model.Sek `json:"results"`
}

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.

@bokunodev
Copy link
Author

i think this should be in the openapi repo. but i'm not sure.

@bokunodev
Copy link
Author

bokunodev commented Jul 31, 2022

i found the problem, it generate only one definition when two types has the same package name but different import path.

main.go
package 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.go
package d

import "local.pc/exp/c"

type Response struct {
    c.Response
    B int `json:"b"`
}
a/d/d.go
package d

import "local.pc/exp/c"

type Response struct {
    c.Response
    A int `json:"a"`
}

it only generate definition from a/d/d.go

@bokunodev
Copy link
Author

bokunodev commented Jul 31, 2022

restful.WebService as .TypeNameHandler method. but the handler only invoked for sample passed to.Reads and not .Returns and it only change type name in the ref and not the definition.
i add this to main.go

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
})

@emicklei
Copy link
Owner

emicklei commented Aug 1, 2022

i think this should be in the openapi repo. but i'm not sure.

yes , better have it there

@emicklei
Copy link
Owner

emicklei commented Aug 1, 2022

restful.WebService as .TypeNameHandler method. but the handler only invoked for sample passed to.Reads and not .Returns and it only change type name in the ref and not the definition. i add this to main.go

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
})

Without looking into the details, I think a handler invocation for Returns is possible. The definition change is a bigger challenge.

@emicklei
Copy link
Owner

emicklei commented Nov 11, 2022

Moved to go-restful-openapi repo. emicklei/go-restful-openapi#100

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

2 participants