Skip to content

Commit

Permalink
typescript-node: form data file (#3967)
Browse files Browse the repository at this point in the history
* [typescript][node]: Add options to specify a from 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

Fix file return type

* [typescript][node]: Fix use of applyLocalTypeMapping

Previous call to applyLocalTypeMapping didn't had any effect.

* [typescript][node]: Update samples
  • Loading branch information
clemens-smartparking authored and macjohnny committed Oct 4, 2019
1 parent f13043e commit 711a210
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
Expand All @@ -40,8 +42,12 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
public TypeScriptNodeClientCodegen() {
super();

typeMapping.put("file", "Buffer");
typeMapping.put("file", "RequestFile");
// RequestFile is defined as: `type RequestFile = string | Buffer | ReadStream | RequestDetailedFile;`
languageSpecificPrimitives.add("Buffer");
languageSpecificPrimitives.add("ReadStream");
languageSpecificPrimitives.add("RequestDetailedFile");
languageSpecificPrimitives.add("RequestFile");

// clear import mapping (from default generator) as TS does not use it
// at the moment
Expand Down Expand Up @@ -70,19 +76,35 @@ public String getHelp() {

@Override
public boolean isDataTypeFile(final String dataType) {
return "Buffer".equals(dataType);
return dataType != null && dataType.equals("RequestFile");
}

@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isFileSchema(p)) {
return "Buffer";
// There are two file types:
// 1) RequestFile: the parameter for the request lib when uploading a file
// (https://github.com/request/request#multipartform-data-multipart-form-uploads)
// 2) Buffer: for downloading files.
// Use RequestFile as a default. The return type is fixed to Buffer in handleMethodResponse.
return "RequestFile";
} else if (ModelUtils.isBinarySchema(p)) {
return "Buffer";
}
return super.getTypeDeclaration(p);
}


@Override
protected void handleMethodResponse(Operation operation, Map<String, Schema> schemas, CodegenOperation op,
ApiResponse methodResponse) {
super.handleMethodResponse(operation, schemas, op, methodResponse);

// see comment in getTypeDeclaration
if (op.isResponseFile) {
op.returnType = "Buffer";
}
}

@Override
public String toApiName(String name) {
if (name.length() == 0) {
Expand Down Expand Up @@ -239,13 +261,12 @@ public String getSchemaType(Schema p) {
if (isLanguagePrimitive(openAPIType) || isLanguageGenericType(openAPIType)) {
return openAPIType;
}
applyLocalTypeMapping(openAPIType);
return openAPIType;
return applyLocalTypeMapping(openAPIType);
}

private String applyLocalTypeMapping(String type) {
if (typeMapping.containsKey(type)) {
type = typeMapping.get(type);
return typeMapping.get(type);
}
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ import { {{ classname }} } from './{{ classFilename }}';
export * from './{{ classFilename }}Interface'
{{/withInterfaces}}
{{/apis}}
import http = require('http');
import * as fs from 'fs';
import * as http from 'http';

export class HttpError extends Error {
constructor (public response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}, public body: any, public statusCode?: number) {
super('HTTP request failed');
this.name = 'HttpError';
}
}

export interface RequestDetailedFile {
value: Buffer;
options?: {
filename?: string;
contentType?: string;
}
}

export type RequestFile = string | Buffer | fs.ReadStream | RequestDetailedFile;

export const APIS = [{{#apis}}{{#operations}}{{ classname }}{{/operations}}{{^-last}}, {{/-last}}{{/apis}}];
{{/apiInfo}}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';
{{/hasAuthMethods}}

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = '{{{basePath}}}';

Expand Down
15 changes: 14 additions & 1 deletion samples/client/petstore/typescript-node/default/api/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ export * from './storeApi';
import { StoreApi } from './storeApi';
export * from './userApi';
import { UserApi } from './userApi';
import http = require('http');
import * as fs from 'fs';
import * as http from 'http';

export class HttpError extends Error {
constructor (public response: http.ClientResponse, public body: any, public statusCode?: number) {
super('HTTP request failed');
this.name = 'HttpError';
}
}

export interface RequestDetailedFile {
value: Buffer;
options?: {
filename?: string;
contentType?: string;
}
}

export type RequestFile = string | Buffer | fs.ReadStream | RequestDetailedFile;

export const APIS = [PetApi, StoreApi, UserApi];
4 changes: 2 additions & 2 deletions samples/client/petstore/typescript-node/default/api/petApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Pet } from '../model/pet';
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = 'http://petstore.swagger.io/v2';

Expand Down Expand Up @@ -528,7 +528,7 @@ export class PetApi {
* @param additionalMetadata Additional data to pass to server
* @param file file to upload
*/
public async uploadFile (petId: number, additionalMetadata?: string, file?: Buffer, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: ApiResponse; }> {
public async uploadFile (petId: number, additionalMetadata?: string, file?: RequestFile, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: ApiResponse; }> {
const localVarPath = this.basePath + '/pet/{petId}/uploadImage'
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
let localVarQueryParameters: any = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Order } from '../model/order';
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = 'http://petstore.swagger.io/v2';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { User } from '../model/user';

import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = 'http://petstore.swagger.io/v2';

Expand Down
15 changes: 14 additions & 1 deletion samples/client/petstore/typescript-node/npm/api/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ export * from './storeApi';
import { StoreApi } from './storeApi';
export * from './userApi';
import { UserApi } from './userApi';
import http = require('http');
import * as fs from 'fs';
import * as http from 'http';

export class HttpError extends Error {
constructor (public response: http.ClientResponse, public body: any, public statusCode?: number) {
super('HTTP request failed');
this.name = 'HttpError';
}
}

export interface RequestDetailedFile {
value: Buffer;
options?: {
filename?: string;
contentType?: string;
}
}

export type RequestFile = string | Buffer | fs.ReadStream | RequestDetailedFile;

export const APIS = [PetApi, StoreApi, UserApi];
4 changes: 2 additions & 2 deletions samples/client/petstore/typescript-node/npm/api/petApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Pet } from '../model/pet';
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = 'http://petstore.swagger.io/v2';

Expand Down Expand Up @@ -528,7 +528,7 @@ export class PetApi {
* @param additionalMetadata Additional data to pass to server
* @param file file to upload
*/
public async uploadFile (petId: number, additionalMetadata?: string, file?: Buffer, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: ApiResponse; }> {
public async uploadFile (petId: number, additionalMetadata?: string, file?: RequestFile, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: ApiResponse; }> {
const localVarPath = this.basePath + '/pet/{petId}/uploadImage'
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
let localVarQueryParameters: any = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Order } from '../model/order';
import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';
import { HttpBasicAuth, ApiKeyAuth, OAuth } from '../model/models';

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = 'http://petstore.swagger.io/v2';

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/typescript-node/npm/api/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { User } from '../model/user';

import { ObjectSerializer, Authentication, VoidAuth } from '../model/models';

import { HttpError } from './apis';
import { HttpError, RequestFile } from './apis';

let defaultBasePath = 'http://petstore.swagger.io/v2';

Expand Down

0 comments on commit 711a210

Please sign in to comment.