forked from loafle/openapi-generator-original
Fixed file uploads
This commit is contained in:
parent
2468b748d3
commit
4720467cbb
@ -107,6 +107,7 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
|
|||||||
typeMapping.put("date", "string");
|
typeMapping.put("date", "string");
|
||||||
typeMapping.put("DateTime", "Date");
|
typeMapping.put("DateTime", "Date");
|
||||||
typeMapping.put("binary", "any");
|
typeMapping.put("binary", "any");
|
||||||
|
// TODO: allow other types for file e.g. Blob
|
||||||
typeMapping.put("File", "any");
|
typeMapping.put("File", "any");
|
||||||
typeMapping.put("ByteArray", "string");
|
typeMapping.put("ByteArray", "string");
|
||||||
typeMapping.put("UUID", "string");
|
typeMapping.put("UUID", "string");
|
||||||
@ -677,7 +678,8 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
|
|||||||
inner = (Schema) p.getAdditionalProperties();
|
inner = (Schema) p.getAdditionalProperties();
|
||||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||||
} else if (ModelUtils.isFileSchema(p)) {
|
} else if (ModelUtils.isFileSchema(p)) {
|
||||||
return "any";
|
// TODO: Change type declaration
|
||||||
|
return "HttpFile";
|
||||||
} else if (ModelUtils.isBinarySchema(p)) {
|
} else if (ModelUtils.isBinarySchema(p)) {
|
||||||
return "any";
|
return "any";
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// TODO: better import syntax?
|
// TODO: better import syntax?
|
||||||
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
||||||
import { RequestContext, HttpMethod, ResponseContext} from '../http/http';
|
import { RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
|
||||||
import * as FormData from "form-data";
|
import * as FormData from "form-data";
|
||||||
import {ObjectSerializer} from '../models/ObjectSerializer';
|
import {ObjectSerializer} from '../models/ObjectSerializer';
|
||||||
import {ApiException} from './exception';
|
import {ApiException} from './exception';
|
||||||
@ -68,7 +68,12 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {
|
|||||||
{{^isListContainer}}
|
{{^isListContainer}}
|
||||||
if ({{paramName}} !== undefined) {
|
if ({{paramName}} !== undefined) {
|
||||||
// TODO: replace .append with .set
|
// TODO: replace .append with .set
|
||||||
|
{{^isFile}}
|
||||||
localVarFormParams.append('{{baseName}}', {{paramName}} as any);
|
localVarFormParams.append('{{baseName}}', {{paramName}} as any);
|
||||||
|
{{/isFile}}
|
||||||
|
{{#isFile}}
|
||||||
|
localVarFormParams.append('{{baseName}}', {{paramName}}.data, { "filename": {{paramName}}.name });
|
||||||
|
{{/isFile}}
|
||||||
}
|
}
|
||||||
{{/isListContainer}}
|
{{/isListContainer}}
|
||||||
{{/formParams}}
|
{{/formParams}}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ResponseContext } from './http/http';
|
import { ResponseContext, HttpFile } from './http/http';
|
||||||
import * as models from './models/all';
|
import * as models from './models/all';
|
||||||
import { Configuration} from './configuration'
|
import { Configuration} from './configuration'
|
||||||
|
|
||||||
|
@ -15,6 +15,10 @@ export enum HttpMethod {
|
|||||||
TRACE = "TRACE",
|
TRACE = "TRACE",
|
||||||
PATCH = "PATCH"
|
PATCH = "PATCH"
|
||||||
}
|
}
|
||||||
|
export interface HttpFile {
|
||||||
|
data: Buffer;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface FormEntry {
|
export interface FormEntry {
|
||||||
contentDisposition: string;
|
contentDisposition: string;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ResponseContext } from './http/http';
|
import { ResponseContext, HttpFile } from './http/http';
|
||||||
import * as models from './models/all';
|
import * as models from './models/all';
|
||||||
import { Configuration} from './configuration'
|
import { Configuration} from './configuration'
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ export class PetApi {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public uploadFile(petId: number, additionalMetadata?: string, file?: any, options?: any): Promise<ApiResponse> {
|
public uploadFile(petId: number, additionalMetadata?: string, file?: HttpFile, options?: any): Promise<ApiResponse> {
|
||||||
const requestContext = this.requestFactory.uploadFile(petId, additionalMetadata, file, options);
|
const requestContext = this.requestFactory.uploadFile(petId, additionalMetadata, file, options);
|
||||||
|
|
||||||
// build promise chain
|
// build promise chain
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// TODO: better import syntax?
|
// TODO: better import syntax?
|
||||||
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
||||||
import { RequestContext, HttpMethod, ResponseContext} from '../http/http';
|
import { RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
|
||||||
import * as FormData from "form-data";
|
import * as FormData from "form-data";
|
||||||
import {ObjectSerializer} from '../models/ObjectSerializer';
|
import {ObjectSerializer} from '../models/ObjectSerializer';
|
||||||
import {ApiException} from './exception';
|
import {ApiException} from './exception';
|
||||||
@ -273,7 +273,7 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory {
|
|||||||
return requestContext;
|
return requestContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public uploadFile(petId: number, additionalMetadata?: string, file?: any, options?: any): RequestContext {
|
public uploadFile(petId: number, additionalMetadata?: string, file?: HttpFile, options?: any): RequestContext {
|
||||||
// verify required parameter 'petId' is not null or undefined
|
// verify required parameter 'petId' is not null or undefined
|
||||||
if (petId === null || petId === undefined) {
|
if (petId === null || petId === undefined) {
|
||||||
throw new RequiredError('Required parameter petId was null or undefined when calling uploadFile.');
|
throw new RequiredError('Required parameter petId was null or undefined when calling uploadFile.');
|
||||||
@ -301,7 +301,7 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory {
|
|||||||
}
|
}
|
||||||
if (file !== undefined) {
|
if (file !== undefined) {
|
||||||
// TODO: replace .append with .set
|
// TODO: replace .append with .set
|
||||||
localVarFormParams.append('file', file as any);
|
localVarFormParams.append('file', file.data, { "filename": file.name });
|
||||||
}
|
}
|
||||||
requestContext.setBody(localVarFormParams);
|
requestContext.setBody(localVarFormParams);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// TODO: better import syntax?
|
// TODO: better import syntax?
|
||||||
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
||||||
import { RequestContext, HttpMethod, ResponseContext} from '../http/http';
|
import { RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
|
||||||
import * as FormData from "form-data";
|
import * as FormData from "form-data";
|
||||||
import {ObjectSerializer} from '../models/ObjectSerializer';
|
import {ObjectSerializer} from '../models/ObjectSerializer';
|
||||||
import {ApiException} from './exception';
|
import {ApiException} from './exception';
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// TODO: better import syntax?
|
// TODO: better import syntax?
|
||||||
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
import { BaseAPIRequestFactory, RequiredError } from './baseapi';
|
||||||
import { RequestContext, HttpMethod, ResponseContext} from '../http/http';
|
import { RequestContext, HttpMethod, ResponseContext, HttpFile} from '../http/http';
|
||||||
import * as FormData from "form-data";
|
import * as FormData from "form-data";
|
||||||
import {ObjectSerializer} from '../models/ObjectSerializer';
|
import {ObjectSerializer} from '../models/ObjectSerializer';
|
||||||
import {ApiException} from './exception';
|
import {ApiException} from './exception';
|
||||||
|
@ -15,6 +15,10 @@ export enum HttpMethod {
|
|||||||
TRACE = "TRACE",
|
TRACE = "TRACE",
|
||||||
PATCH = "PATCH"
|
PATCH = "PATCH"
|
||||||
}
|
}
|
||||||
|
export interface HttpFile {
|
||||||
|
data: Buffer;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface FormEntry {
|
export interface FormEntry {
|
||||||
contentDisposition: string;
|
contentDisposition: string;
|
||||||
|
@ -1313,14 +1313,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"mime-db": {
|
"mime-db": {
|
||||||
"version": "1.36.0",
|
"version": "1.37.0",
|
||||||
"bundled": true
|
"bundled": true
|
||||||
},
|
},
|
||||||
"mime-types": {
|
"mime-types": {
|
||||||
"version": "2.1.20",
|
"version": "2.1.21",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"mime-db": "~1.36.0"
|
"mime-db": "~1.37.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node-fetch": {
|
"node-fetch": {
|
||||||
@ -1343,10 +1343,6 @@
|
|||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
"bundled": true
|
"bundled": true
|
||||||
},
|
},
|
||||||
"typescript": {
|
|
||||||
"version": "2.9.2",
|
|
||||||
"bundled": true
|
|
||||||
},
|
|
||||||
"url-parse": {
|
"url-parse": {
|
||||||
"version": "1.4.3",
|
"version": "1.4.3",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import {PetApi, Configuration, Pet, ApiException, Tag} from 'ts-petstore-client'
|
import {PetApi, Configuration, Pet, ApiException, Tag, HttpFile} from 'ts-petstore-client'
|
||||||
import { expect, assert } from "chai";
|
import { expect, assert } from "chai";
|
||||||
|
import * as fs from 'fs';
|
||||||
|
|
||||||
const configuration = new Configuration()
|
const configuration = new Configuration()
|
||||||
const petApi = new PetApi(configuration)
|
const petApi = new PetApi(configuration)
|
||||||
@ -18,8 +19,6 @@ pet.category = undefined
|
|||||||
|
|
||||||
describe("PetApi", () =>{
|
describe("PetApi", () =>{
|
||||||
it("addPet", (done) => {
|
it("addPet", (done) => {
|
||||||
|
|
||||||
|
|
||||||
petApi.addPet(pet).then(() => {
|
petApi.addPet(pet).then(() => {
|
||||||
return petApi.getPetById(pet.id)
|
return petApi.getPetById(pet.id)
|
||||||
}).then((createdPet) => {
|
}).then((createdPet) => {
|
||||||
@ -38,7 +37,7 @@ describe("PetApi", () =>{
|
|||||||
}).then((pet: Pet) => {
|
}).then((pet: Pet) => {
|
||||||
done("Pet with id " + pet.id + " was not deleted!");
|
done("Pet with id " + pet.id + " was not deleted!");
|
||||||
}).catch((err: any) => {
|
}).catch((err: any) => {
|
||||||
if (err instanceof ApiException && err.code == 404) {
|
if (err.code && err.code == 404) {
|
||||||
done();
|
done();
|
||||||
} else {
|
} else {
|
||||||
done(err)
|
done(err)
|
||||||
@ -118,8 +117,14 @@ describe("PetApi", () =>{
|
|||||||
})
|
})
|
||||||
})*/
|
})*/
|
||||||
|
|
||||||
/* it("", (done) => {
|
it("uploadFile", (done) => {
|
||||||
file
|
const image = fs.readFileSync(__dirname + "/pet.png")
|
||||||
petApi.uploadFile(pet.id, "", file)
|
petApi.uploadFile(pet.id, "Metadata", { name: "pet.png", data: image}).then((response: any) => {
|
||||||
})*/
|
expect(response.code).to.be.gte(200).and.lt(300);
|
||||||
|
expect(response.message).to.contain("pet.png");
|
||||||
|
done();
|
||||||
|
}).catch((err) => {
|
||||||
|
done(err);
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
Loading…
x
Reference in New Issue
Block a user