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

Add an option to reset the buffer before sending. #1551

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mbarnach
Copy link
Member

Allow to reset the buffer before sending the response.

Description

When sending a response using .send, the body content is inaccessible.
But sometimes, it makes sense to reset the buffer before sending,
eg. an error handler that remove internal details,
sending a JSON payload, not kowing if there is already something inside, etc.

Motivation and Context

When capturing internal error and making them more explicit, eg. RFC 7807,
we need to reset the buffer to avoid appending internal content
or sending an invalid (JSON) payload.

How Has This Been Tested?

Add tests to check the resetted content for String, String? and Data
which is used by all underlying methods.

Checklist:

  • [-] If applicable, I have updated the documentation accordingly.
  • If applicable, I have added tests to cover my changes.

@sonarcloud
Copy link

sonarcloud bot commented Apr 16, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 7 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mbarnach mbarnach requested a review from dannys42 April 16, 2021 10:01
@dannys42
Copy link
Contributor

dannys42 commented Apr 22, 2021

Thanks for the motivation and context! As an alternative, what do you think about adding a separate function call, like this:

\@discardableResult
public func removeBody() -> RouterResponse {
...
}

I think this would allow you to have syntax like:

removeBody()
send(...)

Or:

removeBody().send(...)

I'm not necessarily pushing for this, just exploring. From an API perspective, I guess it's largely a question fo whether these are separate actions. i.e. is there ever any use-case to "reset" without having to send? Or are the two functions really coupled?

If the latter, then the parameter approach that you have makes sense. But I'm not sure the "reset" name fully/effectively conveys the intent of the flag. An alternative name might be "overwrite". Or perhaps even an inverted flag called "append" (defaulting to false). (i.e. noting that the "default" behavior is to append to the body, but that you can make calls to explicitly overwrite it).

@mbarnach
Copy link
Member Author

It looks like a good idea indeed.
I like the initial syntax until I had to change so many functions... I will give it a try and get back here, either with an update of the PR or my comments if I come across some un-expected changes.

@dannys42
Copy link
Contributor

dannys42 commented Apr 22, 2021

@mbarnach Oops, you're too fast... I had more thoughts. (see edit) :-)

@mbarnach
Copy link
Member Author

@dannys42 I'm working with the following ideas:

  • use a dedicated function, like mentioned above;
  • call it reset to be more generic.
    Then, I'm wondering if this function should also reset:
  • the state;
  • the lifecycle;
  • the cookies;
  • the error;
  • the statusCode (to .unknown);
  • the userInfo
  • and even the headers.

It seems to be quite a lot, but that would make sense in the case of error reporting.
A solution would then be that we use the parameters for the reset function, in the form of an OptionSet where we can define all of these parameters, and expose to the user the ability to reset any of them.

What are your thoughts on that approach?

@mbarnach
Copy link
Member Author

@dannys42 A idea of it could be find in #1552

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.

None yet

2 participants