Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CodegenOperation {
public Boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, hasOptionalParams,
returnTypeIsPrimitive, returnSimpleType, subresourceOperation, isMapContainer,
isListContainer, isMultipart, hasMore = Boolean.TRUE,
isResponseBinary = Boolean.FALSE, hasReference = Boolean.FALSE,
isResponseBinary = Boolean.FALSE, isResponseFile = Boolean.FALSE, hasReference = Boolean.FALSE,
isRestfulIndex, isRestfulShow, isRestfulCreate, isRestfulUpdate, isRestfulDestroy,
isRestful;
public String path, operationId, returnType, httpMethod, returnBaseType,
Expand Down Expand Up @@ -215,6 +215,8 @@ public boolean equals(Object o) {
return false;
if (isResponseBinary != null ? !isResponseBinary.equals(that.isResponseBinary) : that.isResponseBinary != null)
return false;
if (isResponseFile != null ? !isResponseFile.equals(that.isResponseFile) : that.isResponseFile != null)
return false;
if (hasReference != null ? !hasReference.equals(that.hasReference) : that.hasReference != null)
return false;
if (path != null ? !path.equals(that.path) : that.path != null)
Expand Down Expand Up @@ -297,6 +299,7 @@ public int hashCode() {
result = 31 * result + (isMultipart != null ? isMultipart.hashCode() : 0);
result = 31 * result + (hasMore != null ? hasMore.hashCode() : 0);
result = 31 * result + (isResponseBinary != null ? isResponseBinary.hashCode() : 0);
result = 31 * result + (isResponseFile != null ? isResponseFile.hashCode() : 0);
result = 31 * result + (hasReference != null ? hasReference.hashCode() : 0);
result = 31 * result + (path != null ? path.hashCode() : 0);
result = 31 * result + (operationId != null ? operationId.hashCode() : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CodegenProperty implements Cloneable {
public Boolean hasMore, required, secondaryParam;
public Boolean hasMoreNonReadOnly; // for model constructor, true if next properyt is not readonly
public Boolean isPrimitiveType, isContainer, isNotContainer;
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;
public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isFile, isBoolean, isDate, isDateTime;
public Boolean isListContainer, isMapContainer;
public boolean isEnum;
public Boolean isReadOnly = false;
Expand Down Expand Up @@ -106,6 +106,7 @@ public int hashCode()
result = prime * result + ((isDouble == null) ? 0 : isDouble.hashCode());
result = prime * result + ((isByteArray == null) ? 0 : isByteArray.hashCode());
result = prime * result + ((isBinary == null) ? 0 : isBinary.hashCode());
result = prime * result + ((isFile == null) ? 0 : isFile.hashCode());
result = prime * result + ((isBoolean == null) ? 0 : isBoolean.hashCode());
result = prime * result + ((isDate == null) ? 0 : isDate.hashCode());
result = prime * result + ((isDateTime == null) ? 0 : isDateTime.hashCode());
Expand Down Expand Up @@ -262,6 +263,9 @@ public boolean equals(Object obj) {
if (this.isBinary != other.isBinary && (this.isBinary == null || !this.isBinary.equals(other.isBinary))) {
return false;
}
if (this.isFile != other.isFile && (this.isFile == null || !this.isFile.equals(other.isFile))) {
return false;
}
if (this.isListContainer != other.isListContainer && (this.isListContainer == null || !this.isListContainer.equals(other.isListContainer))) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class CodegenResponse {
public Boolean isMapContainer;
public Boolean isListContainer;
public Boolean isBinary = Boolean.FALSE;
public Boolean isFile = Boolean.FALSE;
public Object schema;
public String jsonSchema;

Expand Down Expand Up @@ -63,6 +64,8 @@ public boolean equals(Object o) {
return false;
if (isBinary != null ? !isBinary.equals(that.isBinary) : that.isBinary != null)
return false;
if (isFile != null ? !isFile.equals(that.isFile) : that.isFile != null)
return false;
if (schema != null ? !schema.equals(that.schema) : that.schema != null)
return false;
return jsonSchema != null ? jsonSchema.equals(that.jsonSchema) : that.jsonSchema == null;
Expand All @@ -85,6 +88,7 @@ public int hashCode() {
result = 31 * result + (isMapContainer != null ? isMapContainer.hashCode() : 0);
result = 31 * result + (isListContainer != null ? isListContainer.hashCode() : 0);
result = 31 * result + (isBinary != null ? isBinary.hashCode() : 0);
result = 31 * result + (isFile != null ? isFile.hashCode() : 0);
result = 31 * result + (schema != null ? schema.hashCode() : 0);
result = 31 * result + (jsonSchema != null ? jsonSchema.hashCode() : 0);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.DecimalProperty;
import io.swagger.models.properties.DoubleProperty;
import io.swagger.models.properties.FileProperty;
import io.swagger.models.properties.FloatProperty;
import io.swagger.models.properties.IntegerProperty;
import io.swagger.models.properties.LongProperty;
Expand Down Expand Up @@ -765,6 +766,7 @@ public DefaultCodegen() {
typeMapping.put("integer", "Integer");
typeMapping.put("ByteArray", "byte[]");
typeMapping.put("binary", "byte[]");
typeMapping.put("file", "File");


instantiationTypes = new HashMap<String, String>();
Expand Down Expand Up @@ -1069,6 +1071,8 @@ public String getSwaggerType(Property p) {
datatype = "ByteArray";
} else if (p instanceof BinaryProperty) {
datatype = "binary";
} else if (p instanceof FileProperty) {
datatype = "file";
} else if (p instanceof BooleanProperty) {
datatype = "boolean";
} else if (p instanceof DateProperty) {
Expand Down Expand Up @@ -1536,6 +1540,9 @@ public CodegenProperty fromProperty(String name, Property p) {
if (p instanceof BinaryProperty) {
property.isBinary = true;
}
if (p instanceof FileProperty) {
property.isFile = true;
}
if (p instanceof UUIDProperty) {
property.isString = true;
}
Expand Down Expand Up @@ -1964,6 +1971,9 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
if (r.isBinary && r.isDefault){
op.isResponseBinary = Boolean.TRUE;
}
if (r.isFile && r.isDefault){
op.isResponseFile = Boolean.TRUE;
}
}
op.responses.get(op.responses.size() - 1).hasMore = false;

Expand Down Expand Up @@ -2163,6 +2173,7 @@ public CodegenResponse fromResponse(String responseCode, Response response) {
}
r.dataType = cm.datatype;
r.isBinary = isDataTypeBinary(cm.datatype);
r.isFile = isDataTypeFile(cm.datatype);
if (cm.isContainer != null) {
r.simpleType = false;
r.containerType = cm.containerType;
Expand Down Expand Up @@ -2343,6 +2354,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
p.dataType = cp.datatype;
p.isPrimitiveType = cp.isPrimitiveType;
p.isBinary = isDataTypeBinary(cp.datatype);
p.isFile = isDataTypeFile(cp.datatype);
}

// set boolean flag (e.g. isString)
Expand Down Expand Up @@ -2409,6 +2421,8 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
p.example = "BINARY_DATA_HERE";
} else if (Boolean.TRUE.equals(p.isByteArray)) {
p.example = "B";
} else if (Boolean.TRUE.equals(p.isFile)) {
p.example = "/path/to/file.txt";
} else if (Boolean.TRUE.equals(p.isDate)) {
p.example = "2013-10-20";
} else if (Boolean.TRUE.equals(p.isDateTime)) {
Expand Down Expand Up @@ -2459,6 +2473,10 @@ public boolean isDataTypeBinary(String dataType) {
return dataType.toLowerCase().startsWith("byte");
}

public boolean isDataTypeFile(String dataType) {
return dataType.toLowerCase().equals("file");
}

/**
* Convert map of Swagger SecuritySchemeDefinition objects to a list of Codegen Security objects
*
Expand Down Expand Up @@ -3254,6 +3272,10 @@ public void setParameterBooleanFlagWithCodegenProperty(CodegenParameter paramete
} else if (Boolean.TRUE.equals(property.isBinary)) {
parameter.isByteArray = true;
parameter.isPrimitiveType = true;
} else if (Boolean.TRUE.equals(property.isFile)) {
parameter.isFile = true;
// file is *not* a primitive type
//parameter.isPrimitiveType = true;
} else if (Boolean.TRUE.equals(property.isDate)) {
parameter.isDate = true;
parameter.isPrimitiveType = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public TypeScriptAngular2ClientCodegen() {
embeddedTemplateDir = templateDir = "typescript-angular2";
modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.service.mustache", ".ts");
languageSpecificPrimitives.add("Blob");
typeMapping.put("Date","Date");
typeMapping.put("file","Blob");
apiPackage = "api";
modelPackage = "model";

Expand Down Expand Up @@ -116,6 +118,11 @@ private String getIndexDirectory() {
return indexPackage.replace('.', File.separatorChar);
}

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

@Override
public String getTypeDeclaration(Property p) {
Property inner;
Expand All @@ -127,7 +134,9 @@ public String getTypeDeclaration(Property p) {
MapProperty mp = (MapProperty)p;
inner = mp.getAdditionalProperties();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
} else if(p instanceof FileProperty || p instanceof ObjectProperty) {
} else if(p instanceof FileProperty) {
return "Blob";
} else if(p instanceof ObjectProperty) {
return "any";
} else {
return super.getTypeDeclaration(p);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.swagger.codegen.languages;

import io.swagger.models.properties.FileProperty;
import io.swagger.models.properties.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -27,6 +29,8 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
public TypeScriptNodeClientCodegen() {
super();

typeMapping.put("file", "Buffer");

// clear import mapping (from default generator) as TS does not use it
// at the moment
importMapping.clear();
Expand Down Expand Up @@ -92,6 +96,19 @@ public String getHelp() {
return "Generates a TypeScript nodejs client library.";
}

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

@Override
public String getTypeDeclaration(Property p) {
if (p instanceof FileProperty) {
return "Buffer";
}
return super.getTypeDeclaration(p);
}


public void setNpmName(String npmName) {
this.npmName = npmName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export class {{classname}} {
return <T1&T2>objA;
}

/**
* @param consumes string[] mime-types
* @return true: consumes contains 'multipart/form-data', false: otherwise
*/
private canConsumeForm(consumes: string[]): boolean {
const form = 'multipart/form-data';
for (let consume of consumes) {
if (form === consume) {
return true;
}
}
return false;
}

{{#operation}}
/**
* {{summary}}
Expand All @@ -66,7 +80,12 @@ export class {{classname}} {
if (response.status === 204) {
return undefined;
} else {
{{^isResponseFile}}
return response.json();
{{/isResponseFile}}
{{#isResponseFile}}
return response.blob();
{{/isResponseFile}}
}
});
}
Expand All @@ -84,10 +103,7 @@ export class {{classname}} {

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
{{#hasFormParams}}
let formParams = new URLSearchParams();

{{/hasFormParams}}
{{#allParams}}
{{#required}}
// verify required parameter '{{paramName}}' is not null or undefined
Expand All @@ -106,12 +122,24 @@ export class {{classname}} {
headers.set('{{baseName}}', String({{paramName}}));
{{/headerParams}}

{{#hasFormParams}}
// to determine the Content-Type header
let consumes: string[] = [
{{#consumes}}
'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
{{/consumes}}
];
let canConsumeForm = this.canConsumeForm(consumes);
let useForm = false;
{{#formParams}}
{{#isFile}}
useForm = canConsumeForm;
{{/isFile}}
{{/formParams}}
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
{{/hasFormParams}}

// to determine the Accept header
let produces: string[] = [
Expand Down Expand Up @@ -152,17 +180,13 @@ export class {{classname}} {
{{/isOAuth}}
{{/authMethods}}

{{#hasFormParams}}
headers.set('Content-Type', 'application/x-www-form-urlencoded');
{{/hasFormParams}}

{{#bodyParam}}
headers.set('Content-Type', 'application/json');
{{/bodyParam}}

{{#formParams}}
if ({{paramName}} !== undefined) {
formParams.set('{{baseName}}', <any>{{paramName}});
formParams.set('{{baseName}}', {{paramName}});
}
{{/formParams}}

Expand All @@ -173,8 +197,11 @@ export class {{classname}} {
body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{/bodyParam}}
{{#hasFormParams}}
body: formParams.toString(),
body: formParams,
{{/hasFormParams}}
{{#isResponseFile}}
responseType: ResponseContentType.Blob,
{{/isResponseFile}}
search: queryParameters
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ export class {{classname}} {
headers: headerParams,
uri: localVarPath,
useQuerystring: this._useQuerystring,
{{^isResponseFile}}
json: true,
{{/isResponseFile}}
{{#isResponseFile}}
encoding: null,
{{/isResponseFile}}
{{#bodyParam}}
body: {{paramName}},
{{/bodyParam}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void fileUploadParamTest() {

final CodegenParameter file = op.formParams.get(1);
Assert.assertTrue(file.isFormParam);
Assert.assertEquals(file.dataType, "file");
Assert.assertEquals(file.dataType, "File");
Assert.assertNull(file.required);
Assert.assertTrue(file.isFile);
Assert.assertNull(file.hasMore);
Expand Down Expand Up @@ -187,6 +187,19 @@ public void binaryDataTest() {
Assert.assertTrue(op.bodyParam.isBinary);
Assert.assertTrue(op.responses.get(0).isBinary);
}

@Test(description = "return file when response format is file")
public void fileResponeseTest() {
final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/fileResponseTest.json");
final DefaultCodegen codegen = new DefaultCodegen();
final String path = "/tests/fileResponse";
final Operation p = model.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions());

Assert.assertEquals(op.returnType, "File");
Assert.assertTrue(op.responses.get(0).isFile);
Assert.assertTrue(op.isResponseFile);
}

@Test(description = "discriminator is present")
public void discriminatorTest() {
Expand Down
Loading