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

What's the difference between StdHTTPServerOptions and StrictHTTPServerOptions (path param and body validation in two places)

Open pixelsoccupied opened this issue 1 year ago • 0 comments

Hello! I'm trying to override the default error with a custom struct (ProblemDetails) when the input is invalid in strict-server mode. But it seems like I have to apply the same override in two places?

	r := http.NewServeMux()

	// Override default validation error 
	reqErr := func(w http.ResponseWriter, r *http.Request, err error) {
		hErr := http.StatusBadRequest
		out, _ := json.Marshal(generated.ProblemDetails{
			Detail: err.Error(),
			Status: int32(hErr),
		})
		http.Error(w, string(out), hErr)
	}

	myServerStrict := generated.NewStrictHandlerWithOptions(server.MyServer{}, nil, generated.StrictHTTPServerOptions{
		RequestErrorHandlerFunc: reqErr,  ---------------------------------------------------> 1 
	})

	generated.StrictHTTPServerOptions{
		RequestErrorHandlerFunc:  nil,
		ResponseErrorHandlerFunc: nil,
	}

	opt := generated.StdHTTPServerOptions{
		BaseRouter: r,
		Middlewares: []generated.MiddlewareFunc{
			middleware.OapiValidation(),
		},
		ErrorHandlerFunc: reqErr,  -----------------------------------------------------------> 2
	}

	// Create the handler
	handler := generated.HandlerWithOptions(myServerStrict, opt)

With (2) I can not validate the path param and without (1) I can not validate POST body.

Was wondering if I'm doing something wrong here? Or is this expected?

Really appreciate this project! Thanks for the great work!

pixelsoccupied avatar Oct 21 '24 21:10 pixelsoccupied