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

[typescript-axios]: Only call JSON.stringify on JSON request bodies #7580

Merged
merged 3 commits into from
Oct 8, 2020
Merged

[typescript-axios]: Only call JSON.stringify on JSON request bodies #7580

merged 3 commits into from
Oct 8, 2020

Conversation

therockstorm
Copy link
Contributor

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

Fixes #7537.

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov

*
* @export
*/
export const JSON_VENDOR_MIME_PATTERN = new RegExp('application\\/vnd.(.*)+json(;.*)?', 'i');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could create a small function that does the check, similar to

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}

@therockstorm therockstorm changed the title Only serialize JSON request bodies [typescript-axios]: Only serialize JSON request bodies Oct 2, 2020
@therockstorm therockstorm changed the title [typescript-axios]: Only serialize JSON request bodies [typescript-axios]: Only call JSON.stringify on JSON request bodies Oct 2, 2020
@@ -203,7 +203,8 @@ export const {{classname}}AxiosParamCreator = function (configuration?: Configur
localVarRequestOptions.data = localVarFormParams{{#vendorExtensions}}{{^multipartFormData}}.toString(){{/multipartFormData}}{{/vendorExtensions}};
{{/hasFormParams}}
{{#bodyParam}}
const needsSerialization = (typeof {{paramName}} !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
const contentType = localVarRequestOptions.headers['Content-Type'];
const needsSerialization = (typeof {{paramName}} !== "string") && (contentType.match(JSON_MIME_PATTERN) || contentType.match(JSON_VENDOR_MIME_PATTERN));
Copy link
Contributor Author

@therockstorm therockstorm Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a breaking change if people were relying on this JSON.stringify with either an empty or non-JSON Content-Type. However, I didn't want to just add an exclusion for application/octet-stream since others (e.g., image/jpeg, etc.) wouldn't want JSON.stringify called either.

@therockstorm
Copy link
Contributor Author

@macjohnny Are you able to take a look at this? Thanks!

@macjohnny macjohnny added this to the 5.0.0 milestone Oct 6, 2020
@therockstorm
Copy link
Contributor Author

@macjohnny Thanks for the approval. Is this good to merge or did you want me to address your comment?

@macjohnny
Copy link
Member

It would be great if you would refactor the check into a function

@therockstorm
Copy link
Contributor Author

therockstorm commented Oct 7, 2020

Looks like a Scala Akka failure in circleci unrelated to my changes. I don't have permission to re-run it.

@macjohnny
Copy link
Member

@therockstorm I re-started the CI

@therockstorm
Copy link
Contributor Author

All green, @macjohnny!

@macjohnny macjohnny merged commit 8d0053f into OpenAPITools:master Oct 8, 2020
@therockstorm therockstorm deleted the data-1327 branch October 8, 2020 13:22
@sgoodrow-zymergen
Copy link

sgoodrow-zymergen commented Oct 20, 2020

const api = new ApiV1({basePath, accessToken});

☝️ wants me to define an isJsonMime function.

Is the default implementation of isJsonMime being shipped as the fallback configuration? I don't think it is, I'm seeing the function as undefined in generated clients when configuring a new API class, and as expected this is now marked as a required field which I don't think was the intention?

Using typescript-axios to test.

@macjohnny
Copy link
Member

@sgoodrow-zymergen you need to create an instance of Configuration. But I agree, it would be better if the default implementation was separated from the Configuration class and the isJsonMime would be optional in the configuration. Would you like to fix this?

@sgoodrow-zymergen
Copy link

Ah, shoot, I overlooked that Configuration is a class (thought it was an interface)! My mistake, thanks for the correction.

Will see what time I can scrounge up to factor this down -- no promises but I appreciate the intention. :)

@denodaeus
Copy link

denodaeus commented May 4, 2021

So, this function is required by the type definitions when utilizing the typescript-axios generator:

"@openapitools/openapi-generator-cli": "^2.2.6",
"generate:client:javascript": "npx @openapitools/openapi-generator-cli generate -g typescript-axios -i ./swagger.json -o ./src",

Is isJsonMime a required function, or does it have some default behavior? It seems really dumb to have to define an implementation for this every time I want to use the generated client.

Here is the error:

src/api/tradeblotter.api.ts:4:34 - error TS2345: Argument of type '{ basePath: string; baseOptions: { withCredentials: boolean; }; }' is not assignable to parameter of type 'Configuration'.
  Property 'isJsonMime' is missing in type '{ basePath: string; baseOptions: { withCredentials: boolean; }; }' but required in type 'Configuration'.

@amakhrov
Copy link
Contributor

amakhrov commented May 4, 2021

It looks like the types are messed up. I will be surprised if the generated client code compiles with strictNullChecks at all.

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

Successfully merging this pull request may close these issues.

[BUG][typescript-axios] attempts to serialize Node Buffer using JSON.stringify and corrupts the file upload
5 participants