You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using res.json() to create a response while the configuration validator.responseValidation is true, the content of the response gets double encoded in JSON.
To Reproduce
Steps to reproduce the behavior:
Create a controller with res.json(), such as:
exportfunctionlistPets(req,res){res.json({message: 'This is a mockup controller'});}
Set the validator.responseValidation configuration to false
Call the API endpoint. The result is a valid JSON object:
{
"message": "This is a mockup controller"
}
Set the validator.responseValidation configuration to true
Call the API endpoint again. The JSON object got double-encoded:
"{\"message\":\"This is a mockup controller\"}"
Expected behavior
The result should not be JSON encoded twice when using res.json()
Notes
The problem only occurs when using res.json() to generate the response, not when using res.send().
This problem leads to validation errors, because the double encoded string is not matching the provided schema:
[oas-tools] WARN: Wrong data in response.
Validation failed at #/type > must be object
The text was updated successfully, but these errors were encountered:
The problem seems to come from the interception of the res.send function done in oas-validator.js, line 102
/* Intercepts response */constoldSend=res.send;res.send=functionsend(data){// here, data parameter can be either object or string
...
}
The data parameter can either be of type object or string, depending on the way the response is generated:
when using res.send(object), data will be an object
when using res.json(object), data will be a JSON encoded string
It seems that there is not enough information in the current interceptor to differenciate these cases. Testing if data looks like a JSON encoded string is not a solution because someone could want to use res.send() to send an already JSON encoded string.
In my opinion, the interception should be made at a lower level, to avoid these problems. The interception itself could be made with an external library, such as express-interceptor. Using a low-level interceptor also has the benefit to allow more use cases of response generation, such as calling res.write() directly.
Hello, Mick, I agree with what you say about intercepting responses at a lower level. Ideally, it is the res.write( ) method what should be overriden, since it is what writes the data to the stream before calling res.end( ). I'll study how express-interceptor works and try to fix that.
Describe the bug
When using
res.json()
to create a response while the configurationvalidator.responseValidation
istrue
, the content of the response gets double encoded in JSON.To Reproduce
Steps to reproduce the behavior:
res.json()
, such as:validator.responseValidation
configuration tofalse
validator.responseValidation
configuration totrue
"{\"message\":\"This is a mockup controller\"}"
Expected behavior
The result should not be JSON encoded twice when using res.json()
Notes
res.json()
to generate the response, not when usingres.send()
.The text was updated successfully, but these errors were encountered: