Skip to content

Commit 667d405

Browse files
committed
Fix template files.
Fix option processing.
1 parent d7bc3bc commit 667d405

File tree

7 files changed

+32
-28
lines changed

7 files changed

+32
-28
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ public RubyClientCodegen() {
5151
modelPackage = "models";
5252
apiPackage = "api";
5353
outputFolder = "generated-code" + File.separator + "ruby";
54-
modelTemplateFiles.put("model.mustache", ".rb");
55-
apiTemplateFiles.put("api.mustache", ".rb");
56-
modelDocTemplateFiles.put("model_doc.mustache", ".md");
57-
apiDocTemplateFiles.put("api_doc.mustache", ".md");
58-
embeddedTemplateDir = templateDir = "ruby";
59-
60-
modelTestTemplateFiles.put("model_test.mustache", ".rb");
61-
apiTestTemplateFiles.put("api_test.mustache", ".rb");
6254

6355
// default HIDE_GENERATION_TIMESTAMP to true
6456
hideGenerationTimestamp = Boolean.TRUE;
@@ -121,9 +113,9 @@ public RubyClientCodegen() {
121113
cliOptions.removeIf(opt -> CodegenConstants.MODEL_PACKAGE.equals(opt.getOpt()) ||
122114
CodegenConstants.API_PACKAGE.equals(opt.getOpt()));
123115
cliOptions.add(new CliOption(GEM_NAME, "gem name (convention: underscore_case).")
124-
.defaultValue("swagger_client"));
125-
cliOptions.add(new CliOption(MODULE_NAME, "top module name (convention: CamelCase, usually corresponding" +
126-
" to gem name).").defaultValue("SwaggerClient"));
116+
.defaultValue("openapi_client"));
117+
cliOptions.add(new CliOption(MODULE_NAME, "top module name (convention: CamelCase, usually corresponding to gem name).")
118+
.defaultValue("OpenAPIClient"));
127119
cliOptions.add(new CliOption(GEM_VERSION, "gem version.")
128120
.defaultValue("1.0.0"));
129121
cliOptions.add(new CliOption(GEM_LICENSE, "gem license. ")
@@ -133,9 +125,9 @@ public RubyClientCodegen() {
133125
cliOptions.add(new CliOption(GEM_HOMEPAGE, "gem homepage. ")
134126
.defaultValue("http://swagger.io"));
135127
cliOptions.add(new CliOption(GEM_SUMMARY, "gem summary. ")
136-
.defaultValue("A ruby wrapper for the swagger APIs"));
128+
.defaultValue("A ruby wrapper for the open APIs"));
137129
cliOptions.add(new CliOption(GEM_DESCRIPTION, "gem description. ")
138-
.defaultValue("This gem maps to a swagger API"));
130+
.defaultValue("This gem maps to a open API"));
139131
cliOptions.add(new CliOption(GEM_AUTHOR, "gem author (only one is supported)."));
140132
cliOptions.add(new CliOption(GEM_AUTHOR_EMAIL, "gem author email (only one is supported)."));
141133
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC)
@@ -146,6 +138,18 @@ public RubyClientCodegen() {
146138
public void processOpts() {
147139
super.processOpts();
148140

141+
if (StringUtils.isBlank(templateDir)) {
142+
embeddedTemplateDir = templateDir = getTemplateDir();
143+
}
144+
145+
modelTemplateFiles.put("model.mustache", ".rb");
146+
apiTemplateFiles.put("api.mustache", ".rb");
147+
modelDocTemplateFiles.put("model_doc.mustache", ".md");
148+
apiDocTemplateFiles.put("api_doc.mustache", ".md");
149+
150+
modelTestTemplateFiles.put("model_test.mustache", ".rb");
151+
apiTestTemplateFiles.put("api_test.mustache", ".rb");
152+
149153
if (additionalProperties.containsKey(GEM_NAME)) {
150154
setGemName((String) additionalProperties.get(GEM_NAME));
151155
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Please follow the [installation](#installation) procedure and then run the follo
6161
```ruby
6262
# Load the gem
6363
require '{{{gemName}}}'
64-
{{#apiInfo}}{{#apis}}{{@first}}{{#operations}}{{#operation}}{{@first}}{{#hasAuthMethods}}
64+
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#hasAuthMethods}}
6565
# Setup authorization
6666
{{{moduleName}}}.configure do |config|{{#authMethods}}{{#isBasic}}
6767
# Configure HTTP basic authorization: {{{name}}}
@@ -91,7 +91,7 @@ begin
9191
rescue {{{moduleName}}}::ApiError => e
9292
puts "Exception when calling {{classname}}->{{{operationId}}}: #{e}"
9393
end
94-
{{/-first}}{{/operation}}{{/operations}}{{/-first}}{{/apis}}{{/apiInfo}}
94+
{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
9595
```
9696

9797
## Documentation for API Endpoints

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ module {{moduleName}}
5151
{{#isEnum}}
5252
{{^isContainer}}
5353
# verify enum value
54-
if @api_client.config.client_side_validation && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
55-
fail ArgumentError, "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"
54+
if @api_client.config.client_side_validation && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}].include?({{{paramName}}})
55+
fail ArgumentError, "invalid value for '{{{paramName}}}', must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}"
5656
end
5757
{{/isContainer}}
5858
{{/isEnum}}
5959
{{/required}}
6060
{{^required}}
6161
{{#isEnum}}
6262
{{#collectionFormat}}
63-
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all? { |item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(item) }
64-
fail ArgumentError, 'invalid value for "{{{paramName}}}", must include one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
63+
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && !opts[:'{{{paramName}}}'].all? { |item| [{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}].include?(item) }
64+
fail ArgumentError, 'invalid value for "{{{paramName}}}", must include one of {{#allowableValues}}{{#values}}{{{this}}}{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}'
6565
end
6666
{{/collectionFormat}}
6767
{{^collectionFormat}}
68-
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
69-
fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}'
68+
if @api_client.config.client_side_validation && opts[:'{{{paramName}}}'] && ![{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}].include?(opts[:'{{{paramName}}}'])
69+
fail ArgumentError, 'invalid value for "{{{paramName}}}", must be one of {{#allowableValues}}{{#values}}{{{this}}}{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}'
7070
end
7171
{{/collectionFormat}}
7272
{{/isEnum}}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ end
5454
```
5555

5656
### Parameters
57-
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{@last}}
57+
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#@last}}
5858
Name | Type | Description | Notes
59-
------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}
59+
------------- | ------------- | ------------- | -------------{{/@last}}{{/allParams}}
6060
{{#allParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{^required}}[optional] {{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
6161
{{/allParams}}
6262

@@ -66,7 +66,7 @@ Name | Type | Description | Notes
6666

6767
### Authorization
6868

69-
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{name}}](../README.md#{{name}}){{^-last}}, {{/-last}}{{/authMethods}}
69+
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{name}}](../README.md#{{name}}){{^@last}}, {{/@last}}{{/authMethods}}
7070

7171
### HTTP request headers
7272

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe '{{classname}}' do
3131
it 'should work' do
3232
{{#isEnum}}
3333
# assertion here. ref: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
34-
# validator = Petstore::EnumTest::EnumAttributeValidator.new('{{{datatype}}}', [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}])
34+
# validator = Petstore::EnumTest::EnumAttributeValidator.new('{{{datatype}}}', [{{#allowableValues}}{{#values}}"{{{this}}}"{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}])
3535
# validator.allowable_values.each do |value|
3636
# expect { @instance.{{name}} = value }.not_to raise_error
3737
# end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
{{/required}}
151151
{{#isEnum}}
152152
{{^isContainer}}
153-
{{{name}}}_validator = EnumAttributeValidator.new('{{{datatype}}}', [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}])
153+
{{{name}}}_validator = EnumAttributeValidator.new('{{{datatype}}}', [{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}])
154154
return false unless {{{name}}}_validator.valid?(@{{{name}}})
155155
{{/isContainer}}
156156
{{/isEnum}}
@@ -187,7 +187,7 @@
187187
# Custom attribute writer method checking allowed values (enum).
188188
# @param [Object] {{{name}}} Object to be assigned
189189
def {{{name}}}=({{{name}}})
190-
validator = EnumAttributeValidator.new('{{{datatype}}}', [{{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}])
190+
validator = EnumAttributeValidator.new('{{{datatype}}}', [{{#allowableValues}}{{#values}}'{{{this}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}])
191191
unless validator.valid?({{{name}}})
192192
fail ArgumentError, 'invalid value for "{{{name}}}", must be one of #{validator.allowable_values}.'
193193
end

src/test/java/io/swagger/codegen/v3/generators/ruby/RubyClientCodegenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void testGenerateRubyClientWithHtmlEntity() throws Exception {
5858
if (file.getName().equals("default_api.rb")) {
5959
apiFileGenerated = true;
6060
// Ruby client should set the path unescaped in the api file
61-
assertTrue(FileUtils.readFileToString(file, StandardCharsets.UTF_8).contains("local_var_path = '/foo=bar'"));
61+
assertTrue(FileUtils.readFileToString(file, StandardCharsets.UTF_8).contains("local_var_path = '/test_optional'"));
6262
}
6363
}
6464
if (!apiFileGenerated) {

0 commit comments

Comments
 (0)