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? #53

Closed
forrestsun opened this issue Sep 30, 2013 · 3 comments
Closed

Bug? #53

forrestsun opened this issue Sep 30, 2013 · 3 comments
Labels

Comments

@forrestsun
Copy link

I define a struct File include file array.Cpu and memory is 100% when run this program.

I find if File struct not include []File the program is right.But I can't find proberlem point :(

type File struct {
    Id, Name   string
    HitoryFile []File
}

type FileService struct {
    // normally one would use DAO (data access object)
    files map[string]File
}

func (f FileService) Register() {
    ws := new(restful.WebService)
    ws.
        Path("/files").
        Consumes(restful.MIME_XML, restful.MIME_JSON).
        Produces(restful.MIME_JSON, restful.MIME_XML) // you can specify this per route as well

    ws.Route(ws.GET("/{file-id}").To(f.findFile).
        // docs
        Doc("get a file").
        Param(ws.PathParameter("file-id", "identifier of the file").DataType("string")).
        Writes(File{})) // on the response
    restful.Add(ws)
}

// GET http://localhost:8080/users/1
//
func (f FileService) findFile(request *restful.Request, response *restful.Response) {
    id := request.PathParameter("file-id")
    file := f.files[id]
    if len(file.Id) == 0 {
        response.WriteErrorString(http.StatusNotFound, "File could not be found.")
    } else {
        response.WriteEntity(file)
    }
}

func main() {
    f := FileService{map[string]File{}}
    f.Register()

    // Optionally, you can install the Swagger Service which provides a nice Web UI on your REST API
    // You need to download the Swagger HTML5 assets and change the FilePath location in the config below.
    // Open http://localhost:8080/apidocs and enter http://localhost:8080/apidocs.json in the api input field.
    config := swagger.Config{
        WebServices:    restful.RegisteredWebServices(), // you control what services are visible
        WebServicesUrl: "http://localhost:8080",
        ApiPath:        "/apidocs.json",

        // Optionally, specifiy where the UI is located
        SwaggerPath:     "/apidocs/",
        SwaggerFilePath: "./apiview"}
    swagger.InstallSwaggerService(config)

    log.Printf("start listening on localhost:8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}
@emicklei
Copy link
Owner

So you defined a recursive data structure. Does the problem only happen when asking for the user interface, i.e. when the Swagger model is constructed? I will create a test to verify that recursive structures are handled properly just in case.

@emicklei
Copy link
Owner

I just created a test that confirms your problem and it is indeed related to creating a Swagger Model from your datastructure.

@emicklei
Copy link
Owner

Fixed

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

No branches or pull requests

2 participants