Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: allow response error handler to set status code#1963

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
sean-cunniffe wants to merge2 commits intooapi-codegen:main
base:main
Choose a base branch
Loading
fromsean-cunniffe:fix/allow-response-err-handler-set-status-code

Conversation

sean-cunniffe
Copy link

@sean-cunniffesean-cunniffe commentedMay 4, 2025
edited
Loading

As mentioned in#1964

Version: 2.4.1

Issue: Status code is set to 200 before marshalling data into body inside visit functions, so when an error occurs marshalling the ResponseErrorHandlerFunc cannot set the status code.

Reproduce:

openapi: 3.0.1info:title: error-handler-exampleversion: 1.0.0paths:/api/example:post:operationId: exampleHandlerresponses:200:description: examplecontent:application/json:schema:type: objectproperties:example:type: stringformat: email
package: apigenerate:  gorilla-server: true  strict-server: true  models: trueoutput: generated/gen.go
package mainimport ("context""net/http"api "example.go/generated"openapi_types "github.com/oapi-codegen/runtime/types")type ExampleHandler struct {}func main() {e := &ExampleHandler{}h := api.NewStrictHandlerWithOptions(e, nil, api.StrictHTTPServerOptions{ResponseErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) {w.WriteHeader(500)},})handler := api.Handler(h)err := http.ListenAndServe(":8080", handler)panic(err)}func (*ExampleHandler) ExampleHandler(ctx context.Context,request api.ExampleHandlerRequestObject) (api.ExampleHandlerResponseObject, error) {email := openapi_types.Email("invalid email")return api.ExampleHandler200JSONResponse{Example: &email,}, nil}

Generated visit function

func (response ExampleHandler200JSONResponse) VisitExampleHandlerResponse(w http.ResponseWriter) error {w.Header().Set("Content-Type", "application/json")w.WriteHeader(200)return json.NewEncoder(w).Encode(response)}

Proposed changes would have the output

func (response ExampleHandler200JSONResponse) VisitExampleHandlerResponse(w http.ResponseWriter) error {if err := json.NewEncoder(w).Encode(response); err != nil {return err}w.Header().Set("Content-Type", "application/json")w.WriteHeader(200)return nil}

bkleef reacted with thumbs up emojithumbnail reacted with rocket emoji
change the order the visit response body function sets the status codeand content type so if an error occurs during json marshalling theresponse error handler can set the status code
@sean-cunniffesean-cunniffe requested a review froma team as acode ownerMay 4, 2025 02:46
@jamietannajamietanna added server:strict bugSomething isn't working labelsMay 4, 2025
@jamietannajamietanna added this to thev2.5.0 milestoneMay 4, 2025
@jamietanna
Copy link
Member

Thanks for reporting + raising a suggested fix! Will try to look at it soon 🤞🏼

sean-cunniffe reacted with heart emoji

@jamietannajamietanna modified the milestones:v2.5.0,v2.6.0May 11, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
bugSomething isn't workingserver:strict
Projects
None yet
Milestone
v2.6.0
Development

Successfully merging this pull request may close these issues.

2 participants
@sean-cunniffe@jamietanna

[8]ページ先頭

©2009-2025 Movatter.jp