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

Proposal: ResponseCode #84

Closed
karitham opened this issue Mar 19, 2022 · 2 comments · Fixed by #86
Closed

Proposal: ResponseCode #84

karitham opened this issue Mar 19, 2022 · 2 comments · Fixed by #86
Labels
proposal new feature/change to the generated code

Comments

@karitham
Copy link
Collaborator

Context

For methods such as DELETE, PATCH, PUT and POST, we are often met with the need of just returning a response code.

Having a helper method landing after #80 (returning a *Response) directly means that we are obliged to either return something like

(we will go with openapi as the generated package name)

var r openapi.Response
r.Status(http.StatusNoContent)
return &r

Or to write the status code directly, similarly to this code @diamondburned refactored.

w.WriteHeader(http.StatusNoContent)
return nil

Proposal

Generating a function to help with this

func ResponseCode(code int) *Response {
	return &Response{
		statusCode: code,
	}
}

Which would turn this situation into

return openapi.ResponseCode(http.StatusNoContent)

I think this would make every single return meaningful, such that developers could identify code paths that actually respond to the request simpler, rather than having them search for status code being set, or not, earlier.

@karitham karitham added the proposal new feature/change to the generated code label Mar 19, 2022
@diamondburned
Copy link
Contributor

I think alternatively, we can just export field inside *Response so the user can do return &oapi.Response{StatusCode: http.StatusNoContent}.

Ideally, though, the schema should describe the status code, and the generator should generate something like

func DeletePetJSON204Response() *Response {
    return &Response{statusCode: 204}
}

@karitham
Copy link
Collaborator Author

I guess exporting responseCode is fine, especially since it's unlikely to really change much later on, and isn't exactly subject to breaking change.

karitham added a commit to karitham/goapi-gen that referenced this issue Mar 20, 2022
karitham added a commit that referenced this issue Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal new feature/change to the generated code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants