You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Server returns 501: operation PostResource has not yet been implemented response despite the fact that the handler is indeed implemented.
Technical details
This is caused by the if-handler-is-nil check in the initially auto-generated restapi/configure_service.go file (specifically the configureAPI function):
The main() function in cmd/service-server/main.go calls api := operations.NewServiceAPI prior to calling server.ConfigureServiceAPI.
operations.NewServiceAPI generates a ServiceAPI object with non-nil handlers that return a middleware.NotImplemented("operation PostResource has not yet been implemented") response. Since each of these handlers are not nil, they are never configured in the configureAPI function in restapi/configure_service.go, so the server always returns the 501: operation PostResource has not yet been implemented response.
In an empty folder, write a swagger.yaml spec with an endpoint specified in paths. The swagger specification above is sufficient.
Write a handler for the endpoint and place it in the configureAPI function in the restapi/configure_service.go file. The handler could just print a "hello world", return a 202 Accepted, etc. Example:
if api.PostResourceHandler == nil {
api.PostResourceHandler = operations.PostResourceHandlerFunc(func(params operations.PostResourceParams) middleware.Responder {
fmt.Println("Handling the Resource...")
return operations.NewPostResourceAccepted()
})
}
Generate the server with: swagger generate server -A service swagger.yaml
Build with: go build -o main cmd/service-server/main.go
Run the server with ./main --port=3000
Send a valid request to the endpoint.
The expected result would be whatever behaviour you set the handler to carry out. Instead, I am seeing a 501: operation PostResource has not yet been implemented response.
Environment
swagger versions: observed in 0.24.0 and 0.25.0
go version: 1.14
OS: Ubuntu 20.04
Quick fix
If anyone else is having a similar problem, simply remove the if-handler-is-nil check for all of your endpoints.
ie. For every endpoint, replace the following code block in the configureAPI function in the restapi/configure_service.go file:
if api.PostResourceHandler == nil {
api.PostResourceHandler = operations.PostResourceHandlerFunc(func(params operations.PostResourceParams) middleware.Responder {
fmt.Println("Handling the Resource...")
return operations.NewPostResourceAccepted()
})
}
Yes, this is an actual bug that was driving me crazy. Did they even try to generate the server and test out anything other than this method was not implemented? Not even one mention in the outdated documentation either.
Problem statement
Server returns
501: operation PostResource has not yet been implemented
response despite the fact that the handler is indeed implemented.Technical details
This is caused by the
if-handler-is-nil
check in the initially auto-generatedrestapi/configure_service.go
file (specifically theconfigureAPI
function):The
main()
function incmd/service-server/main.go
callsapi := operations.NewServiceAPI
prior to callingserver.ConfigureServiceAPI
.operations.NewServiceAPI
generates aServiceAPI
object with non-nil handlers that return amiddleware.NotImplemented("operation PostResource has not yet been implemented")
response. Since each of these handlers are not nil, they are never configured in theconfigureAPI
function inrestapi/configure_service.go
, so the server always returns the501: operation PostResource has not yet been implemented
response.Swagger specification
Steps to reproduce
swagger.yaml
spec with an endpoint specified inpaths
. The swagger specification above is sufficient.configureAPI
function in therestapi/configure_service.go
file. The handler could just print a "hello world", return a202 Accepted
, etc. Example:swagger generate server -A service swagger.yaml
go build -o main cmd/service-server/main.go
./main --port=3000
The expected result would be whatever behaviour you set the handler to carry out. Instead, I am seeing a
501: operation PostResource has not yet been implemented
response.Environment
swagger versions: observed in 0.24.0 and 0.25.0
go version: 1.14
OS: Ubuntu 20.04
Quick fix
If anyone else is having a similar problem, simply remove the
if-handler-is-nil
check for all of your endpoints.ie. For every endpoint, replace the following code block in the
configureAPI
function in therestapi/configure_service.go
file:with this block:
The text was updated successfully, but these errors were encountered: