-
Notifications
You must be signed in to change notification settings - Fork 23
fix(oas-to-har): clean up formatting of raw json bodies #1197
Conversation
Note: these fields still have a strange error. If you empty out a request body of type="string" and format="json" it's replaced immediately with the default value, making it difficult to work with. We should tackle that in the future. |
// In the UI this is represented by an arbitrary text input | ||
// This ensures we remove any newlines or tabs and use a clean json block in the example | ||
if (requestBody.schema.type === 'string') { | ||
har.postData.text = JSON.stringify(JSON.parse(cleanBody)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff sucks on this. Really all I added was this line, and indented everything else that existed under the else.
The problem was that line 318 in the right hand file was failing because these schemas have no properties, and Object.keys would error on undefined. Then we would fall back to line 338 where we stringify the value in the text area.
// with a schema type=string, format=json. | ||
// In the UI this is represented by an arbitrary text input | ||
// This ensures we remove any newlines or tabs and use a clean json block in the example | ||
if (requestBody.schema.type === 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to add a check to make sure type
exists in requestBody.schema
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This essentially works that way right? The value will be undefined which isn't equal to string
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been burned so often on this shit so I'm always nervous about it, but you're right.
let tktk = {schema: {}}; tktk.schema.type === 'string'
> false
🧰 What's being changed?
When your request body has a type of
string
and format ofjson
, the exact value was showing up as the user inputted string (with any included formatting (such as newlines)), passed through JSON.stringify .e.g.
--data='"{\n \"foo\": \"bar\"\n}"'
This changes that field to now appear as a JSON object passed thorugh JSON.stringify
e.g.
--data='{"foo":"bar"}'
🧬 Testing
Check the tests, or message me and I'll send you an example OAS file.