-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Conversation
* | ||
* @export | ||
*/ | ||
export const JSON_VENDOR_MIME_PATTERN = new RegExp('application\\/vnd.(.*)+json(;.*)?', 'i'); |
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.
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.
maybe we could create a small function that does the check, similar to
Lines 135 to 148 in 9236d56
/** | |
* 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'); | |
} |
JSON.stringify
on JSON request bodies
@@ -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)); |
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 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.
@macjohnny Are you able to take a look at this? Thanks! |
@macjohnny Thanks for the approval. Is this good to merge or did you want me to address your comment? |
It would be great if you would refactor the check into a function |
Looks like a Scala Akka failure in circleci unrelated to my changes. I don't have permission to re-run it. |
@therockstorm I re-started the CI |
All green, @macjohnny! |
const api = new ApiV1({basePath, accessToken}); ☝️ wants me to define an Is the default implementation of Using |
@sgoodrow-zymergen you need to create an instance of |
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. :) |
So, this function is required by the type definitions when utilizing the typescript-axios generator:
Is Here is the error:
|
It looks like the types are messed up. I will be surprised if the generated client code compiles with strictNullChecks at all. |
PR checklist
./bin/generate-samples.sh
to 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.master
Fixes #7537.
@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov