Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

fix(oas-to-har): clean up formatting of raw json bodies #1197

Merged
merged 2 commits into from
Feb 25, 2021

Conversation

Dashron
Copy link
Contributor

@Dashron Dashron commented Feb 24, 2021

[☁️   CI App][demo]

🧰 What's being changed?

When your request body has a type of string and format of json, 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.

@erunion erunion temporarily deployed to explorer-pr-1197 February 24, 2021 23:37 Inactive
@Dashron
Copy link
Contributor Author

Dashron commented Feb 24, 2021

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));
Copy link
Contributor Author

@Dashron Dashron Feb 24, 2021

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') {
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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

packages/oas-to-har/src/index.js Outdated Show resolved Hide resolved
@erunion erunion temporarily deployed to explorer-pr-1197 February 25, 2021 00:50 Inactive
@erunion erunion merged commit 0d4cead into next Feb 25, 2021
@erunion erunion deleted the fix/remove-json-req-body-formatting branch February 25, 2021 04:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants