Search code examples
goopenapiopenapi-generator

I'm testing with go openAPI


I'm testing with OpenApi and as an example with https://github.com/d-vignesh/Todo-App-with-OpenAPI but if i run this, i get the following error:

.\main.go:9:77: undefined: GetTodosParams
.\main.go:31:19: undefined: Handler

If i delete the GetTodosParams in API-Definition and generate new files, I'm able to reduce to the handler error - but i don't understand the problem that the handler is undefined:

type TodoServer struct{}

func (t TodoServer) GetTodos(w http.ResponseWriter, r *http.Request) {
    // our logic to retrieve all todos from a persistent layer
    w.WriteHeader(http.StatusOK)
}

func (t TodoServer) CreateTodo(w http.ResponseWriter, r *http.Request) {
    // our logic to store the todo into a persistent layer
}

func (t TodoServer) DeleteTodo(w http.ResponseWriter, r *http.Request, todoId int32) {
    // our logic to delete a todo from the persistent layer
}

func (t TodoServer) UpdateTodo(w http.ResponseWriter, r *http.Request, todoId int32) {
    // our logic to update the todo.
}

func main() {
    s := TodoServer{}
    h := Handler(s)

    http.ListenAndServe(":3000", h)
}

I'm able to link per handlefunc() a handlestring to a function, but i want to use the TodoServer because there are the similiar interface function of Serverinterface in Main Package

type ServerInterface interface {

    // (GET /todos)
    GetTodos(w http.ResponseWriter, r *http.Request)

    // (POST /todos)
    CreateTodo(w http.ResponseWriter, r *http.Request)

    // (DELETE /todos/{todoId})
    DeleteTodo(w http.ResponseWriter, r *http.Request, todoId int32)

    // (PUT /todos/{todoId})
    UpdateTodo(w http.ResponseWriter, r *http.Request, todoId int32)
}

Instead i can start it with standard Servrhttp of net/http but this didn't help to understand why the interfacefunctions doesn't work


Solution

  • I guess you are trying to run the file main.go in a deprecated non-modules way. Try the recipe below.

    Clone the repository:

    git clone https://github.com/d-vignesh/Todo-App-with-OpenAPI
    cd Todo-App-with-OpenAPI/
    

    Run it like this:

    go run github.com/d-vignesh/Openapi-todo-app
    

    Build it like this:

    go install github.com/d-vignesh/Openapi-todo-app
    
    # Then run the executable:
    Openapi-todo-app