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

Regression: Response serializer no longer replaces response #657

Closed
roni-frantchi opened this issue Apr 19, 2021 · 10 comments · Fixed by #665
Closed

Regression: Response serializer no longer replaces response #657

roni-frantchi opened this issue Apr 19, 2021 · 10 comments · Fixed by #665

Comments

@roni-frantchi
Copy link
Contributor

roni-frantchi commented Apr 19, 2021

What are the steps to reproduce this issue?

Create a serializer that returns an object.

What happens?

The serializer returned object is set on the body.

What were you expecting to happen?

The returned object should replace the entire response object as the documentation states:

"...If an object is returned, the entire response object is replaced. This is useful if you want to manipulate headers or add additional attributes in the Lambda response."

(and as it was in v1)

Any other comments?

Seems to me like the behavior had changed since this change

I think the only reason the pre-existing unit test did not break was because the response object itself was mutated.

What versions of software are you using?

Node.js Version: 14

Middy Version: 2.0.1

@willfarrell
Copy link
Member

How did you get 7 +1 in 3h? Anyways, I should be able to take a look later today.

@willfarrell
Copy link
Member

I see the regression,

v1.x

const result = s.serializer(handler.response)

    if (typeof result === 'object') {
      // replace response object if result is object
      handler.response = result
    } else {
      // otherwise only replace the body attribute
      handler.response.body = result
    }

v2.x

request.response.body = s.serializer(request.response)

I do recall simplifying because any object doesn't mean it will be an acceptable response for http. How about this:

    const result = s.serializer(handler.response)
    if (result?.body) {
      handler.response = result
    } else {
      // otherwise only replace the body attribute
      handler.response.body = result
    }

This should give you the flexibility you need while ensuring the correct response structure is maintained. Let me know if this works for you, I can add it in.

@roni-frantchi
Copy link
Contributor Author

@willfarrell that will work just as well I think, thank you.

@willfarrell
Copy link
Member

willfarrell commented Apr 20, 2021

Will be in the next release. v2.1.0
Thanks for reporting

@RaeesBhatti
Copy link

It would be great if this is released soon because it's currently breaking our flow. Thanks!

@willfarrell
Copy link
Member

It will likely be released this week. I'm just waiting on feedback for a new middleware.

@roni-frantchi
Copy link
Contributor Author

Thanks for the prompt release @willfarrell.
Seems like there's one case I've missed which I think what you've suggested won't be good enough.
I'm thinking of cases where the response would be something like 204 - in that case I'd expect the response not to have a body.
Maybe we'd do better to be looking for statusCode rather than body?...

@roni-frantchi
Copy link
Contributor Author

Or perhaps - check if hasOwnProperty('body') so that one may body: undefined

@roni-frantchi
Copy link
Contributor Author

OK so it's worse than that @willfarrell - if one has false as their body, the behavior is also not what you would expect.

I think we would have to check for hasOwnProperty('body')

@willfarrell
Copy link
Member

v2.1.1 is on it's way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants