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("DateTime", "Date");
|
||||
typeMapping.put("binary", "any");
|
||||
// TODO: allow other types for file e.g. Blob
|
||||
typeMapping.put("File", "any");
|
||||
typeMapping.put("ByteArray", "string");
|
||||
typeMapping.put("UUID", "string");
|
||||
@ -677,7 +678,8 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
inner = (Schema) p.getAdditionalProperties();
|
||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||
} else if (ModelUtils.isFileSchema(p)) {
|
||||
return "any";
|
||||
// TODO: Change type declaration
|
||||
return "HttpFile";
|
||||
} else if (ModelUtils.isBinarySchema(p)) {
|
||||
return "any";
|
||||
} else {
|
||||
|
@ -1,6 +1,6 @@
|
||||
// TODO: better import syntax?
|
||||
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 {ObjectSerializer} from '../models/ObjectSerializer';
|
||||
import {ApiException} from './exception';
|
||||
@ -68,7 +68,12 @@ export class {{classname}}RequestFactory extends BaseAPIRequestFactory {
|
||||
{{^isListContainer}}
|
||||
if ({{paramName}} !== undefined) {
|
||||
// TODO: replace .append with .set
|
||||
{{^isFile}}
|
||||
localVarFormParams.append('{{baseName}}', {{paramName}} as any);
|
||||
{{/isFile}}
|
||||
{{#isFile}}
|
||||
localVarFormParams.append('{{baseName}}', {{paramName}}.data, { "filename": {{paramName}}.name });
|
||||
{{/isFile}}
|
||||
}
|
||||
{{/isListContainer}}
|
||||
{{/formParams}}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ResponseContext } from './http/http';
|
||||
import { ResponseContext, HttpFile } from './http/http';
|
||||
import * as models from './models/all';
|
||||
import { Configuration} from './configuration'
|
||||
|
||||
|
@ -15,6 +15,10 @@ export enum HttpMethod {
|
||||
TRACE = "TRACE",
|
||||
PATCH = "PATCH"
|
||||
}
|
||||
export interface HttpFile {
|
||||
data: Buffer;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface FormEntry {
|
||||
contentDisposition: string;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ResponseContext } from './http/http';
|
||||
import { ResponseContext, HttpFile } from './http/http';
|
||||
import * as models from './models/all';
|
||||
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);
|
||||
|
||||
// build promise chain
|
||||
|
@ -1,6 +1,6 @@
|
||||
// TODO: better import syntax?
|
||||
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 {ObjectSerializer} from '../models/ObjectSerializer';
|
||||
import {ApiException} from './exception';
|
||||
@ -273,7 +273,7 @@ export class PetApiRequestFactory extends BaseAPIRequestFactory {
|
||||
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
|
||||
if (petId === null || petId === undefined) {
|
||||
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) {
|
||||
// TODO: replace .append with .set
|
||||
localVarFormParams.append('file', file as any);
|
||||
localVarFormParams.append('file', file.data, { "filename": file.name });
|
||||
}
|
||||
requestContext.setBody(localVarFormParams);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
// TODO: better import syntax?
|
||||
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 {ObjectSerializer} from '../models/ObjectSerializer';
|
||||
import {ApiException} from './exception';
|
||||
|
@ -1,6 +1,6 @@
|
||||
// TODO: better import syntax?
|
||||
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 {ObjectSerializer} from '../models/ObjectSerializer';
|
||||
import {ApiException} from './exception';
|
||||
|
@ -15,6 +15,10 @@ export enum HttpMethod {
|
||||
TRACE = "TRACE",
|
||||
PATCH = "PATCH"
|
||||
}
|
||||
export interface HttpFile {
|
||||
data: Buffer;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface FormEntry {
|
||||
contentDisposition: string;
|
||||
|
@ -1313,14 +1313,14 @@
|
||||
}
|
||||
},
|
||||
"mime-db": {
|
||||
"version": "1.36.0",
|
||||
"version": "1.37.0",
|
||||
"bundled": true
|
||||
},
|
||||
"mime-types": {
|
||||
"version": "2.1.20",
|
||||
"version": "2.1.21",
|
||||
"bundled": true,
|
||||
"requires": {
|
||||
"mime-db": "~1.36.0"
|
||||
"mime-db": "~1.37.0"
|
||||
}
|
||||
},
|
||||
"node-fetch": {
|
||||
@ -1343,10 +1343,6 @@
|
||||
"version": "2.1.2",
|
||||
"bundled": true
|
||||
},
|
||||
"typescript": {
|
||||
"version": "2.9.2",
|
||||
"bundled": true
|
||||
},
|
||||
"url-parse": {
|
||||
"version": "1.4.3",
|
||||
"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 * as fs from 'fs';
|
||||
|
||||
const configuration = new Configuration()
|
||||
const petApi = new PetApi(configuration)
|
||||
@ -18,8 +19,6 @@ pet.category = undefined
|
||||
|
||||
describe("PetApi", () =>{
|
||||
it("addPet", (done) => {
|
||||
|
||||
|
||||
petApi.addPet(pet).then(() => {
|
||||
return petApi.getPetById(pet.id)
|
||||
}).then((createdPet) => {
|
||||
@ -38,7 +37,7 @@ describe("PetApi", () =>{
|
||||
}).then((pet: Pet) => {
|
||||
done("Pet with id " + pet.id + " was not deleted!");
|
||||
}).catch((err: any) => {
|
||||
if (err instanceof ApiException && err.code == 404) {
|
||||
if (err.code && err.code == 404) {
|
||||
done();
|
||||
} else {
|
||||
done(err)
|
||||
@ -118,8 +117,14 @@ describe("PetApi", () =>{
|
||||
})
|
||||
})*/
|
||||
|
||||
/* it("", (done) => {
|
||||
file
|
||||
petApi.uploadFile(pet.id, "", file)
|
||||
})*/
|
||||
it("uploadFile", (done) => {
|
||||
const image = fs.readFileSync(__dirname + "/pet.png")
|
||||
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