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-node: Add options to specify a form data file #3967

Merged
merged 3 commits into from Oct 4, 2019
Merged

typescript-node: Add options to specify a form data file #3967

merged 3 commits into from Oct 4, 2019

Conversation

ghost
Copy link

@ghost ghost commented Sep 27, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.
    @akehir @macjohnny

Description of the PR

Add options to specify a form data file

Be able to specify file options as described on
https://github.com/request/request in the 'multipart/form-data
(Multipart Form Uploads)' section).

Related to #3944

I am new to the OpenApiGenerator and unsure if I did the java part correctly...

*/
public async addPet (body: Pet, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
public async addPet (pet: Pet, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
Copy link
Member

Choose a reason for hiding this comment

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

please run the bin/typescript-node-petstore-all.sh script only, not the openapi3 version

@@ -70,13 +71,13 @@ public String getHelp() {

@Override
public boolean isDataTypeFile(final String dataType) {
return "Buffer".equals(dataType);
return dataType != null && (dataType.equals("Buffer") || dataType.equals("FormDataFile"));
Copy link
Member

Choose a reason for hiding this comment

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

I guess dataType should be split by |, right?

@macjohnny
Copy link
Member

thanks for the PR

@@ -15,5 +15,14 @@ export class HttpError extends Error {
this.name = 'HttpError';
}
}

export interface FormDataFile {
Copy link
Member

Choose a reason for hiding this comment

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

}

@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isFileSchema(p)) {
return "Buffer";
return "Buffer|FormDataFile";
Copy link
Member

Choose a reason for hiding this comment

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

I think this also affects the api response type, so it would be a breaking change, right?

@macjohnny macjohnny added this to the 4.1.3 milestone Sep 27, 2019
@macjohnny macjohnny changed the title Ts form data file typescript-node: form data file Sep 27, 2019
@ghost
Copy link
Author

ghost commented Sep 30, 2019

I moved the type details to the Typescript mustache definitions. I hope I handle the java part correctly now. This made it also possible to add string and ReadStream types.

What do you mean with I think this also affects the api response type? Do you have an example? As far as I see the changes should be transparent to current users (but I also don't know all the swagger details ;) ).

@macjohnny
Copy link
Member

what is the result if you have something like

openapi: '3.0.0'
info:
  version: 1.0.0
  title: Petstore
paths:
  /download:
    get:
      responses:
        '200':
          description: 'An array of pets'
          content:
            application/octet-stream:
              schema:
                type: file

@ghost
Copy link
Author

ghost commented Oct 1, 2019

Ok see what you mean. For OpenApi 3 this shouldn't be a problem (https://swagger.io/docs/specification/describing-responses/#response-that-returns-a-file)

However, how to handle it for swagger? Is there a way to find out if a Schema comes from a parameter or a return value? i.e. if it is a parameter then use RequestFile otherwise its a return value and Buffer should be used. Couldn't find a way to do that. Don't like to return RequestFile as a return type as it's always a Buffer. Any suggestions?

@macjohnny
Copy link
Member

@wing328 any idea?

@macjohnny
Copy link
Member

@clemens-smartparking maybe you could add the decision logic here:

public async {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {

and set Buffer if the response type is a file.
Or find the method that returns the API Response type and override it there.

@ghost
Copy link
Author

ghost commented Oct 3, 2019

I think I can't do it in the mustache file (no ifs). However, this pointed me in the right direction. I now overwrite the returnType in handleMethodResponse(). This seems to work and I hope it is the right way to do it. However, there is no example in the samples for returning a file. Should, and if yes, how can I add a sample for that?

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

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

LGTM

@macjohnny
Copy link
Member

@clemens-smartparking can you please merge the latest master into your branch?

Be able to specify file options as described on
https://github.com/request/request in the 'multipart/form-data
(Multipart Form Uploads)' section).

Related to #3944

Fix file return type
Previous call to applyLocalTypeMapping didn't had any effect.
@ghost
Copy link
Author

ghost commented Oct 3, 2019

I rebased on the current master

@wing328 wing328 modified the milestones: 4.1.3, 4.2.0 Oct 4, 2019
@macjohnny macjohnny merged commit 711a210 into OpenAPITools:master Oct 4, 2019
@macjohnny macjohnny modified the milestones: 4.2.0, 4.1.3 Oct 4, 2019
@wing328 wing328 changed the title typescript-node: form data file typescript-node: Add options to specify a form data file Oct 4, 2019
@wing328 wing328 modified the milestones: 4.2.0, 4.1.3 Oct 4, 2019
@wing328
Copy link
Member

wing328 commented Oct 4, 2019

@clemens-smartparking thanks for the PR, which has been included in the v4.1.3 release: https://twitter.com/oas_generator/status/1180123829626003456

jimschubert added a commit that referenced this pull request Oct 6, 2019
* master: (35 commits)
  [haskell-http-client] update samples (#4073)
  [haskell-http-client] Bump deps to LTS 14.7 (#4068)
  update release for 4.2.0
  [typescript-axios] Fix api generating incorrect seralization type check (#4051)
  prepare 4.1.3 release (#4052)
  typescript-node: form data file (#3967)
  Add a link to blog post on vertx and openapi (#4048)
  better wording for apiNameSuffix option description (#4045)
  [Ruby] fix ruby test, update error message (#4041)
  [PHP] Correctly format JSON in headers (#4024)
  [haskell-http-client] add dateTimeParseFormat cli option - overrides the format string used to parse a datetime (#4037)
  Add frankyjuang to the C# technical committee (#4036)
  Feature/api name suffix (#3918)
  [F#] minor improvements to the generators (#3968)
  Repaired Checkstyle (#4029)
  mockito 3.1.0 (#4035)
  typescript-fetch: fix return type of primitive value (#4028)
  [typescript][node]: Add accept header if produces is not empty (#3966)
  [haskell-http-client] disable unused import warning in Core.hs (#4020)
  Add a link to the tutorial in http4k (#4019)
  ...
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.

3 participants