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

Require handlers to return *Response directly #80

Merged
merged 1 commit into from
Mar 19, 2022

Conversation

diamondburned
Copy link
Contributor

This commit changes the generated handler methods inside ServerInterface
to directly return a *Response value instead of requiring it to call
render.Render manually. This results in much cleaner handler code.

While this change breaks all existing code, it is not a significant
change by any means, so most changes can be regexed away to work.

As an example, petstore's FindPetByID is now

func (p *PetStore) FindPetByID(w http.ResponseWriter, r *http.Request, id int64) *Response {
	p.Lock.Lock()
	defer p.Lock.Unlock()

	pet, found := p.Pets[id]
	if !found {
		return FindPetByIDJSONDefaultResponse(Error{fmt.Sprintf(petNotFoundMsg, id)}).Status(http.StatusNotFound)
	}

	return FindPetByIDJSON200Response(pet)
}

compared to before

func (p *PetStore) FindPetByID(w http.ResponseWriter, r *http.Request, id int64) {
	p.Lock.Lock()
	defer p.Lock.Unlock()

	pet, found := p.Pets[id]
	if !found {
		render.Render(
			w, r,
			FindPetByIDJSONDefaultResponse(Error{fmt.Sprintf(petNotFoundMsg, id)}).Status(http.StatusNotFound),
		)
		return
	}

	render.Render(w, r, FindPetByIDJSON200Response(pet))
}

Ideally, the above piece of code would look something like

func (p *PetStore) FindPetByID(w http.ResponseWriter, r *http.Request, id int64) *Response {
	p.Lock.Lock()
	defer p.Lock.Unlock()

	pet, found := p.Pets[id]
	if !found {
		return FindPetByIDJSON404Response(Error{fmt.Sprintf(petNotFoundMsg, id)})
	}

	return FindPetByIDJSON200Response(pet)
}

where a FindPetByIDJSON404Response is properly generated by the OpenAPI schema having the 404 status code documented, but it isn't in the example schema for some reason.

This commit changes the generated handler methods inside ServerInterface
to directly return a *Response value instead of requiring it to call
`render.Render` manually. This results in much cleaner handler code.

While this change breaks all existing code, it is not a significant
change by any means, so most changes can be regexed away to work.
Copy link
Collaborator

@karitham karitham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, waiting on another review before merge since it's a big breaking change.

@karitham karitham mentioned this pull request Mar 19, 2022
Copy link
Member

@zacharyburkett zacharyburkett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great improvement to overall usage. Good stuff! 🔥

@karitham karitham merged commit 62fb88b into discord-gophers:main Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants