Skip to content

Commit b7a56d7

Browse files
committed
updated ruby generator to fix errors on output code
1 parent 59e5eb5 commit b7a56d7

File tree

12 files changed

+309
-224
lines changed

12 files changed

+309
-224
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,6 +2276,11 @@ public int compare(CodegenParameter one, CodegenParameter another) {
22762276

22772277
configureDataForTestTemplate(codegenOperation);
22782278

2279+
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||| returnBaseType: " + codegenOperation.returnBaseType);
2280+
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||| returnContainer: " + codegenOperation.returnContainer);
2281+
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||| returnType: " + codegenOperation.returnType);
2282+
System.out.println("||||||||||||||||||||||||||||||||||||||||||||||||||||||| returnSimpleType: " + codegenOperation.returnSimpleType);
2283+
22792284
return codegenOperation;
22802285
}
22812286

src/main/java/io/swagger/codegen/v3/generators/ruby/RubyClientCodegen.java

Lines changed: 108 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
package io.swagger.codegen.v3.generators.ruby;
22

3-
import io.swagger.codegen.v3.*;
3+
import io.swagger.codegen.v3.CliOption;
4+
import io.swagger.codegen.v3.CodegenConstants;
5+
import io.swagger.codegen.v3.CodegenOperation;
6+
import io.swagger.codegen.v3.CodegenParameter;
7+
import io.swagger.codegen.v3.CodegenProperty;
8+
import io.swagger.codegen.v3.CodegenType;
9+
import io.swagger.codegen.v3.SupportingFile;
410
import io.swagger.codegen.v3.generators.DefaultCodegenConfig;
11+
import io.swagger.v3.oas.models.OpenAPI;
12+
import io.swagger.v3.oas.models.Operation;
13+
import io.swagger.v3.oas.models.media.ArraySchema;
14+
import io.swagger.v3.oas.models.media.BooleanSchema;
15+
import io.swagger.v3.oas.models.media.IntegerSchema;
16+
import io.swagger.v3.oas.models.media.MapSchema;
17+
import io.swagger.v3.oas.models.media.NumberSchema;
18+
import io.swagger.v3.oas.models.media.Schema;
19+
import io.swagger.v3.oas.models.media.StringSchema;
520
import org.apache.commons.lang3.StringUtils;
621
import org.slf4j.Logger;
722
import org.slf4j.LoggerFactory;
@@ -232,38 +247,38 @@ public void processOpts() {
232247
writeOptional(outputFolder, new SupportingFile("configuration_spec.mustache", specFolder, "configuration_spec.rb"));
233248
writeOptional(outputFolder, new SupportingFile("api_client_spec.mustache", specFolder, "api_client_spec.rb"));
234249
// not including base object test as the moment as not all API has model
235-
//writeOptional(outputFolder, new SupportingFile("base_object_spec.mustache", specFolder, "base_object_spec.rb"));
236-
}
237-
238-
// @Override
239-
// public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
240-
// CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);
241-
// // Set vendor-extension to be used in template:
242-
// // x-codegen-hasMoreRequired
243-
// // x-codegen-hasMoreOptional
244-
// // x-codegen-hasRequiredParams
245-
// CodegenParameter lastRequired = null;
246-
// CodegenParameter lastOptional = null;
247-
// for (CodegenParameter p : op.allParams) {
248-
// if (p.required) {
249-
// lastRequired = p;
250-
// } else {
251-
// lastOptional = p;
252-
// }
253-
// }
254-
// for (CodegenParameter p : op.allParams) {
255-
// if (p == lastRequired) {
256-
// p.vendorExtensions.put("x-codegen-hasMoreRequired", false);
257-
// } else if (p == lastOptional) {
258-
// p.vendorExtensions.put("x-codegen-hasMoreOptional", false);
259-
// } else {
260-
// p.vendorExtensions.put("x-codegen-hasMoreRequired", true);
261-
// p.vendorExtensions.put("x-codegen-hasMoreOptional", true);
262-
// }
263-
// }
264-
// op.vendorExtensions.put("x-codegen-hasRequiredParams", lastRequired != null);
265-
// return op;
266-
// }
250+
writeOptional(outputFolder, new SupportingFile("base_object_spec.mustache", specFolder, "base_object_spec.rb"));
251+
}
252+
253+
@Override
254+
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> definitions, OpenAPI openAPI) {
255+
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, openAPI);
256+
// Set vendor-extension to be used in template:
257+
// x-codegen-hasMoreRequired
258+
// x-codegen-hasMoreOptional
259+
// x-codegen-hasRequiredParams
260+
CodegenParameter lastRequired = null;
261+
CodegenParameter lastOptional = null;
262+
for (CodegenParameter p : op.allParams) {
263+
if (p.required) {
264+
lastRequired = p;
265+
} else {
266+
lastOptional = p;
267+
}
268+
}
269+
for (CodegenParameter p : op.allParams) {
270+
if (p == lastRequired) {
271+
p.vendorExtensions.put("x-codegen-hasMoreRequired", false);
272+
} else if (p == lastOptional) {
273+
p.vendorExtensions.put("x-codegen-hasMoreOptional", false);
274+
} else {
275+
p.vendorExtensions.put("x-codegen-hasMoreRequired", true);
276+
p.vendorExtensions.put("x-codegen-hasMoreOptional", true);
277+
}
278+
}
279+
op.vendorExtensions.put("x-codegen-hasRequiredParams", lastRequired != null);
280+
return op;
281+
}
267282

268283
@Override
269284
public CodegenType getTag() {
@@ -338,74 +353,66 @@ public String modelDocFileFolder() {
338353
return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
339354
}
340355

341-
// @Override
342-
// public String getTypeDeclaration(Property p) {
343-
// if (p instanceof ArrayProperty) {
344-
// ArrayProperty ap = (ArrayProperty) p;
345-
// Property inner = ap.getItems();
346-
// return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">";
347-
// } else if (p instanceof MapProperty) {
348-
// MapProperty mp = (MapProperty) p;
349-
// Property inner = mp.getAdditionalProperties();
350-
// return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">";
351-
// }
352-
// return super.getTypeDeclaration(p);
353-
// }
354-
355-
// @Override
356-
// public String toDefaultValue(Property p) {
357-
// if (p instanceof IntegerProperty) {
358-
// IntegerProperty dp = (IntegerProperty) p;
359-
// if (dp.getDefault() != null) {
360-
// return dp.getDefault().toString();
361-
// }
362-
// } else if (p instanceof LongProperty) {
363-
// LongProperty dp = (LongProperty) p;
364-
// if (dp.getDefault() != null) {
365-
// return dp.getDefault().toString();
366-
// }
367-
// } else if (p instanceof DoubleProperty) {
368-
// DoubleProperty dp = (DoubleProperty) p;
369-
// if (dp.getDefault() != null) {
370-
// return dp.getDefault().toString();
371-
// }
372-
// } else if (p instanceof FloatProperty) {
373-
// FloatProperty dp = (FloatProperty) p;
374-
// if (dp.getDefault() != null) {
375-
// return dp.getDefault().toString();
376-
// }
377-
// } else if (p instanceof BooleanProperty) {
378-
// BooleanProperty bp = (BooleanProperty) p;
379-
// if (bp.getDefault() != null) {
380-
// return bp.getDefault().toString();
381-
// }
382-
// } else if (p instanceof StringProperty) {
383-
// StringProperty sp = (StringProperty) p;
384-
// if (sp.getDefault() != null) {
385-
// return "'" + escapeText(sp.getDefault()) + "'";
386-
// }
387-
// }
388-
//
389-
// return null;
390-
// }
391-
392-
// @Override
393-
// public String getSwaggerType(Property p) {
394-
// String swaggerType = super.getSwaggerType(p);
395-
// String type = null;
396-
// if (typeMapping.containsKey(swaggerType)) {
397-
// type = typeMapping.get(swaggerType);
398-
// if (languageSpecificPrimitives.contains(type)) {
399-
// return type;
400-
// }
401-
// } else {
402-
// type = swaggerType;
403-
// }
404-
// if (type == null) {
405-
// return null;
406-
// }
407-
// return toModelName(type);
408-
// }
356+
@Override
357+
public String getTypeDeclaration(Schema schema) {
358+
if (schema instanceof ArraySchema) {
359+
ArraySchema arraySchema = (ArraySchema) schema;
360+
Schema inner = arraySchema.getItems();
361+
return getSchemaType(schema) + "<" + getTypeDeclaration(inner) + ">";
362+
} else if (schema instanceof MapSchema) {
363+
MapSchema mapSchema = (MapSchema) schema;
364+
if (mapSchema.getAdditionalProperties() instanceof Schema) {
365+
Schema inner = (Schema) mapSchema.getAdditionalProperties();
366+
return getSchemaType(schema) + "<String, " + getTypeDeclaration(inner) + ">";
367+
}
368+
}
369+
return super.getTypeDeclaration(schema);
370+
}
371+
372+
@Override
373+
public String toDefaultValue(Schema schema) {
374+
if (schema instanceof IntegerSchema) {
375+
IntegerSchema integerSchema = (IntegerSchema) schema;
376+
if (integerSchema.getDefault() != null) {
377+
return integerSchema.getDefault().toString();
378+
}
379+
} else if (schema instanceof NumberSchema) {
380+
NumberSchema numberSchema = (NumberSchema) schema;
381+
if (numberSchema.getDefault() != null) {
382+
return numberSchema.getDefault().toString();
383+
}
384+
} else if (schema instanceof BooleanSchema) {
385+
BooleanSchema booleanSchema = (BooleanSchema) schema;
386+
if (booleanSchema.getDefault() != null) {
387+
return booleanSchema.getDefault().toString();
388+
}
389+
} else if (schema instanceof StringSchema) {
390+
StringSchema stringSchema = (StringSchema) schema;
391+
if (stringSchema.getDefault() != null) {
392+
return "'" + escapeText(stringSchema.getDefault()) + "'";
393+
}
394+
}
395+
396+
return null;
397+
}
398+
399+
@Override
400+
public String getSchemaType(Schema schema) {
401+
String swaggerType = super.getSchemaType(schema);
402+
String type = null;
403+
if (typeMapping.containsKey(swaggerType)) {
404+
type = typeMapping.get(swaggerType);
405+
if (languageSpecificPrimitives.contains(type)) {
406+
return type;
407+
}
408+
} else {
409+
type = swaggerType;
410+
}
411+
if (type == null) {
412+
return null;
413+
}
414+
return toModelName(type);
415+
}
409416

410417
@Override
411418
public String toVarName(String name) {

src/main/resources/handlebars/ruby/Gemfile.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ source 'https://rubygems.org'
33
gemspec
44

55
group :development, :test do
6-
gem 'rake', '~> 12.0.0'
6+
gem 'rake', '~> 13.0.1'
7+
gem 'pry-byebug'
8+
gem 'rubocop', '~> 0.66.0'
79
end

src/main/resources/handlebars/ruby/api.mustache

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{{> api_info}}
33
=end
44

5-
require 'uri'
6-
75
module {{moduleName}}
86
{{#operations}}
97
class {{classname}}
@@ -13,18 +11,19 @@ module {{moduleName}}
1311
@api_client = api_client
1412
end
1513
{{#operation}}
14+
{{#contents}}
1615
{{#summary}}
1716
# {{{summary}}}
1817
{{/summary}}
1918
{{#notes}}
2019
# {{{notes}}}
2120
{{/notes}}
22-
{{#allParams}}{{#required}} # @param {{paramName}} {{description}}
23-
{{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
24-
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
25-
{{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
26-
def {{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
27-
{{#returnType}}data, _status_code, _headers = {{/returnType}}{{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts)
21+
{{#parameters}}{{#required}} # @param {{paramName}} {{description}}
22+
{{/required}}{{/parameters}} # @param [Hash] opts the optional parameters
23+
{{#parameters}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}
24+
{{/required}}{{/parameters}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
25+
def {{operationId}}({{#parameters}}{{#required}}{{paramName}}, {{/required}}{{/parameters}}opts = {})
26+
{{#returnType}}data, _status_code, _headers = {{/returnType}}{{operationId}}_with_http_info({{#parameters}}{{#required}}{{paramName}}, {{/required}}{{/parameters}}opts)
2827
{{#returnType}}data{{/returnType}}{{^returnType}}nil{{/returnType}}
2928
end
3029

@@ -34,15 +33,15 @@ module {{moduleName}}
3433
{{#notes}}
3534
# {{notes}}
3635
{{/notes}}
37-
{{#allParams}}{{#required}} # @param {{paramName}} {{description}}
38-
{{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
39-
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
40-
{{/required}}{{/allParams}} # @return [Array<({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}, Fixnum, Hash)>] {{#returnType}}{{{returnType}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers
41-
def {{operationId}}_with_http_info({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}opts = {})
36+
{{#parameters}}{{#required}} # @param {{paramName}} {{description}}
37+
{{/required}}{{/parameters}} # @param [Hash] opts the optional parameters
38+
{{#parameters}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
39+
{{/required}}{{/parameters}} # @return [Array<({{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}, Integer, Hash)>] {{#returnType}}{{{returnType}}} data{{/returnType}}{{^returnType}}nil{{/returnType}}, response status code and response headers
40+
def {{operationId}}_with_http_info({{#parameters}}{{#required}}{{paramName}}, {{/required}}{{/parameters}}opts = {})
4241
if @api_client.config.debugging
4342
@api_client.config.logger.debug 'Calling API: {{classname}}.{{operationId}} ...'
4443
end
45-
{{#allParams}}
44+
{{#parameters}}
4645
{{#required}}
4746
# verify the required parameter '{{paramName}}' is set
4847
if @api_client.config.client_side_validation && {{{paramName}}}.nil?
@@ -115,12 +114,12 @@ module {{moduleName}}
115114

116115
{{/minItems}}
117116
{{/hasValidation}}
118-
{{/allParams}}
117+
{{/parameters}}
119118
# resource path
120119
local_var_path = '{{{path}}}'{{#pathParams}}.sub('{' + '{{baseName}}' + '}', {{paramName}}.to_s){{/pathParams}}
121120

122121
# query parameters
123-
query_params = {}
122+
query_params = opts[:query_params] || {}
124123
{{#queryParams}}
125124
{{#required}}
126125
query_params[:'{{{baseName}}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
@@ -133,7 +132,7 @@ module {{moduleName}}
133132
{{/queryParams}}
134133

135134
# header parameters
136-
header_params = {}
135+
header_params = opts[:header_params] || {}
137136
{{#hasProduces}}
138137
# HTTP header 'Accept' (if needed)
139138
header_params['Accept'] = @api_client.select_header_accept([{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}])
@@ -154,7 +153,7 @@ module {{moduleName}}
154153
{{/headerParams}}
155154

156155
# form parameters
157-
form_params = {}
156+
form_params = opts[:form_params] || {}
158157
{{#formParams}}
159158
{{#required}}
160159
form_params['{{baseName}}'] = {{#collectionFormat}}@api_client.build_collection_param({{{paramName}}}, :{{{collectionFormat}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}}
@@ -167,25 +166,25 @@ module {{moduleName}}
167166
{{/formParams}}
168167

169168
# http body (model)
170-
{{^bodyParam}}
171-
post_body = nil
172-
{{/bodyParam}}
173-
{{#bodyParam}}
174-
post_body = @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}})
175-
{{/bodyParam}}
176-
auth_names = [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
169+
post_body = opts[:body] {{#bodyParam}}|| @api_client.object_to_http_body({{#required}}{{{paramName}}}{{/required}}{{^required}}opts[:'{{{paramName}}}']{{/required}}) {{/bodyParam}}
170+
171+
return_type = opts[:return_type] {{#returnType}}|| '{{{returnType}}}' {{/returnType}}
172+
173+
auth_names = opts[:auth_names] || [{{#authMethods}}'{{name}}'{{#hasMore}}, {{/hasMore}}{{/authMethods}}]
177174
data, status_code, headers = @api_client.call_api(:{{httpMethod}}, local_var_path,
178175
:header_params => header_params,
179176
:query_params => query_params,
180177
:form_params => form_params,
181178
:body => post_body,
182-
:auth_names => auth_names{{#returnType}},
183-
:return_type => '{{{returnType}}}'{{/returnType}})
179+
:auth_names => auth_names,
180+
:return_type => return_type)
181+
184182
if @api_client.config.debugging
185183
@api_client.config.logger.debug "API called: {{classname}}#{{operationId}}\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
186184
end
187185
return data, status_code, headers
188186
end
187+
{{/contents}}
189188
{{/operation}}
190189
end
191190
{{/operations}}

0 commit comments

Comments
 (0)