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

Auto-generated API configuration file causes inappropriate "501: Not Implemented" responses #2380

Open
sh-ura opened this issue Aug 12, 2020 · 4 comments

Comments

@sh-ura
Copy link

sh-ura commented Aug 12, 2020

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-generated restapi/configure_service.go file (specifically the configureAPI function):

func configureAPI(api *operations.ServiceAPI) http.Handler {
	// ...

	if api.PostResourceHandler == nil {
		api.PostResourceHandler = operations.PostResourceHandlerFunc(func(params operations.PostResourceParams) middleware.Responder {
			return handlers.PostResource(params)
	})

	// ...
}

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.

Swagger specification

consumes:
- application/json
info:
  title: A sample API
  version: 0.0.0
basePath: /v0
produces:
- application/json
schemes:
- http
swagger: "2.0"

paths:
  /participants:
    post:
      operationId: post_resource
      summary: Add a resource
      parameters:
        - name: resource
          in: body
          schema:
            $ref: '#/definitions/Resource'
            example:
              code: 1
      responses:
        202:
        	description: Resource accepted

definitions:
  Resource:
    type: object
    required:
      - code
    properties: 
      code:
        type: integer
        description: An integer code
        example: 1

Steps to reproduce

  1. In an empty folder, write a swagger.yaml spec with an endpoint specified in paths. The swagger specification above is sufficient.
  2. 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()
	})
}
  1. Generate the server with: swagger generate server -A service swagger.yaml
  2. Build with: go build -o main cmd/service-server/main.go
  3. Run the server with ./main --port=3000
  4. 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()
	})
}

with this block:

api.PostResourceHandler = operations.PostResourceHandlerFunc(func(params operations.PostResourceParams) middleware.Responder {
			fmt.Println("Handling the Resource...")
			return operations.NewPostResourceAccepted()
})
@s8mathur
Copy link

s8mathur commented Sep 5, 2020

This is an annoying regression in the out-of-the-box usability of the generate server behaviour. Request prioritization for a fix please, thank you.

This is a bug, not a question @fredbi 🙏🏼

@peroxy
Copy link

peroxy commented Sep 26, 2020

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.

@ajanssens
Copy link

Fast forward to 2022, not fixed?

@casualjim
Copy link
Member

feel free to fix it, it's a community project.

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

No branches or pull requests

6 participants