oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Strict Echo Server broken response

Open el-j opened this issue 1 year ago • 1 comments

image

i am currently tinkering around with the code gen. i have a minimal swagger.yaml with a simple GET ping that should return a Pong or a { resp: Pong } or anything like that.

when i manipulate the generated ping function with something like:

func (w *ServerInterfaceWrapper) Ping(ctx echo.Context) error {
	// returns a pong:
	return ctx.String(http.StatusOK, "pong")

        // generated code returns first screenshot:
	// var err error
	// Invoke the callback with all the unmarshaled arguments
	// err = w.Handler.Ping(ctx)
}
image

generation config, with strict-server:

package: api
generate:
  echo-server: true
  strict-server: true
  embedded-spec: true
  models: true
output: api/api.gen.go

i am a bit confused why the generated Handler itself not returns a "pong"

 /ping:
    get:
      tags:
        - pingpong
      summary: request /api/ping get a pong response
      description: get a pong by the server
      operationId: ping
      responses:
        200:
          description: ok
          content:
            text/plain:
              schema:
                pong:
                  type: string
                  example: pong

i am using it with:

var myApi api.StrictServerInterface

swagger, err := api.GetSwagger()
if err != nil {
  fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
  os.Exit(1)
}
swagger.Servers = nil
myStrictApiHandler := api.NewStrictHandler(myApi, nil)

e := echo.New()
e.Use(middleware2.OapiRequestValidator(swagger))
e.Pre(echomiddleware.Rewrite(map[string]string{
	"/api/v1/*": "/$1",
}))

e.Use(echomiddleware.Logger())
api.RegisterHandlers(e, myStrictApiHandler)

e.Logger.Fatal(e.Start("localhost:5000"))

Or do i get the generator wrong? shouldn't there be a simple - "useable" - get response from the server? tried with browser and postman, both just failing...

help very much appreciated. cheers

el-j avatar Mar 08 '24 16:03 el-j

Is it just a matter of passing an implementation that satisfies api.StrictServerInterface to api.NewStrictHandler?

type ServerInterfaceWrapper struct{}

func (w ServerInterfaceWrapper) Ping(ctx context.Context, request api.PingRequestObject) (api.PingResponseObject, error) {
	// returns a pong:
	return api.Ping200TextResponse("pong"), nil
}

func main() {
	var myApi ServerInterfaceWrapper

	swagger, err := api.GetSwagger()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
		os.Exit(1)
	}
	swagger.Servers = nil
	myStrictApiHandler := api.NewStrictHandler(myApi, nil)

	// ...

eyasy1217 avatar May 06 '24 12:05 eyasy1217