Skip to content

Commit 274510c

Browse files
authored
[Ruby] Minor code improvement (#23196)
* minor refactoring in ruby client codegen * add tests for minus 1 enum value
1 parent 1a09c7a commit 274510c

9 files changed

Lines changed: 26 additions & 29 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyClientCodegen.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public void processOpts() {
295295
// for Faraday
296296
additionalProperties.put("isHttpx", Boolean.TRUE);
297297
} else {
298-
throw new RuntimeException("Invalid HTTP library " + getLibrary() + ". Only faraday, typhoeus and httpx are supported.");
298+
throw new IllegalArgumentException("Invalid HTTP library " + getLibrary() + ". Only faraday, typhoeus and httpx are supported.");
299299
}
300300

301301
// test files should not be overwritten
@@ -469,11 +469,11 @@ public String toModelDocFilename(String name) {
469469
public String toApiFilename(final String name) {
470470
// replace - with _ e.g. created-at => created_at
471471
String filename = name;
472-
if (apiNameSuffix != null && apiNameSuffix.length() > 0) {
472+
if (apiNameSuffix != null && !apiNameSuffix.isEmpty()) {
473473
filename = filename + "_" + apiNameSuffix;
474474
}
475475

476-
filename = filename.replaceAll("-", "_");
476+
filename = filename.replace("-", "_");
477477

478478
// e.g. PhoneNumberApi.rb => phone_number_api.rb
479479
return underscore(filename);
@@ -494,11 +494,6 @@ public String toModelTestFilename(String name) {
494494
return toModelFilename(name) + "_spec";
495495
}
496496

497-
@Override
498-
public String toApiName(String name) {
499-
return super.toApiName(name);
500-
}
501-
502497
@Override
503498
public String toEnumValue(String value, String datatype) {
504499
if ("Integer".equals(datatype) || "Float".equals(datatype)) {
@@ -514,16 +509,16 @@ public String toEnumVarName(String name, String datatype) {
514509
return enumNameMapping.get(name);
515510
}
516511

517-
if (name.length() == 0) {
512+
if (name.isEmpty()) {
518513
return "EMPTY";
519514
}
520515

521516
// number
522517
if ("Integer".equals(datatype) || "Float".equals(datatype)) {
523518
String varName = name;
524-
varName = varName.replaceAll("-", "MINUS_");
525-
varName = varName.replaceAll("\\+", "PLUS_");
526-
varName = varName.replaceAll("\\.", "_DOT_");
519+
varName = varName.replace("-", "MINUS_");
520+
varName = varName.replace("+", "PLUS_");
521+
varName = varName.replace(".", "_DOT_");
527522
return NUMERIC_ENUM_PREFIX + varName;
528523
}
529524

@@ -594,7 +589,7 @@ public String toApiImport(String name) {
594589

595590
@Override
596591
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
597-
final Schema additionalProperties = ModelUtils.getAdditionalProperties(schema);
592+
final Schema<?> additionalProperties = ModelUtils.getAdditionalProperties(schema);
598593

599594
if (additionalProperties != null) {
600595
codegenModel.additionalPropertiesType = getSchemaType(additionalProperties);
@@ -692,7 +687,7 @@ private String constructExampleCode(CodegenParameter codegenParameter, HashMap<S
692687
if (modelMaps.containsKey(codegenParameter.dataType)) {
693688
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps, processedModelMap);
694689
} else {
695-
//LOGGER.error("Error in constructing examples. Failed to look up the model " + codegenParameter.dataType);
690+
LOGGER.debug("Error in constructing examples. Failed to look up the model " + codegenParameter.dataType);
696691
return "TODO";
697692
}
698693
}
@@ -702,8 +697,8 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
702697
if (codegenProperty.isArray) { // array
703698
if (!StringUtils.isEmpty(codegenProperty.example) && !"null".equals(codegenProperty.example)) {
704699
String value = codegenProperty.example;
705-
value = value.replaceAll(",", ", ");
706-
value = value.replaceAll(":", ": ");
700+
value = value.replace(",", ", ");
701+
value = value.replace(":", ": ");
707702
return value;
708703
}
709704
return "[" + constructExampleCode(codegenProperty.items, modelMaps, processedModelMap) + "]";
@@ -764,7 +759,7 @@ private String constructExampleCode(CodegenProperty codegenProperty, HashMap<Str
764759
if (modelMaps.containsKey(codegenProperty.dataType)) {
765760
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps, processedModelMap);
766761
} else {
767-
//LOGGER.error("Error in constructing examples. Failed to look up the model " + codegenParameter.dataType);
762+
LOGGER.debug("Error in constructing examples. Failed to look up the model " + codegenProperty.dataType);
768763
return "TODO";
769764
}
770765
}
@@ -791,7 +786,7 @@ private String constructExampleCode(CodegenModel codegenModel, HashMap<String, C
791786
// oneOf models
792787
return constructExampleCode(modelMaps.get(subModel), modelMaps, processedModelMap);
793788
} else {
794-
// TODO oneOf primitive type not supported at the moment
789+
// oneOf primitive type not supported at the moment
795790
LOGGER.warn("oneOf example value not supported at the moment.");
796791
return "nil";
797792
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/ruby/RubyClientCodegenTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testGenerateRubyClientWithHtmlEntity() throws Exception {
7272
}
7373

7474
@Test
75-
public void testInitialConfigValues() throws Exception {
75+
public void testInitialConfigValues() {
7676
final RubyClientCodegen codegen = new RubyClientCodegen();
7777
codegen.processOpts();
7878

@@ -85,7 +85,7 @@ public void testInitialConfigValues() throws Exception {
8585
}
8686

8787
@Test
88-
public void testSettersForConfigValues() throws Exception {
88+
public void testSettersForConfigValues() {
8989
final RubyClientCodegen codegen = new RubyClientCodegen();
9090
codegen.setHideGenerationTimestamp(false);
9191
codegen.processOpts();
@@ -95,7 +95,7 @@ public void testSettersForConfigValues() throws Exception {
9595
}
9696

9797
@Test
98-
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
98+
public void testAdditionalPropertiesPutForConfigValues() {
9999
final RubyClientCodegen codegen = new RubyClientCodegen();
100100
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, false);
101101
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "ruby-models");
@@ -183,7 +183,6 @@ public void nullablePropertyTest() {
183183
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore_oas3_test.yaml");
184184
final RubyClientCodegen codegen = new RubyClientCodegen();
185185
codegen.setModuleName("OnlinePetstore");
186-
final String path = "/pet";
187186

188187
final Schema schema = openAPI.getComponents().getSchemas().get("NullablePet");
189188
codegen.setOpenAPI(openAPI);
@@ -212,7 +211,6 @@ public void propertiesWithoutNullableTest() {
212211
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore_oas3_test.yaml");
213212
final RubyClientCodegen codegen = new RubyClientCodegen();
214213
codegen.setModuleName("OnlinePetstore");
215-
final String path = "/pet";
216214

217215
final Schema schema = openAPI.getComponents().getSchemas().get("Pet");
218216
codegen.setOpenAPI(openAPI);

modules/openapi-generator/src/test/resources/3_0/ruby/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,7 @@ components:
19361936
OuterEnumInteger:
19371937
type: integer
19381938
enum:
1939+
- -1
19391940
- 0
19401941
- 1
19411942
- 2

samples/client/petstore/ruby-faraday/docs/FakeApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ require 'time'
497497
require 'petstore'
498498

499499
api_instance = Petstore::FakeApi.new
500-
outer_object_with_enum_property = Petstore::OuterObjectWithEnumProperty.new({value: Petstore::OuterEnumInteger::N0}) # OuterObjectWithEnumProperty | Input enum (int) as post body
500+
outer_object_with_enum_property = Petstore::OuterObjectWithEnumProperty.new({value: Petstore::OuterEnumInteger::NMINUS_1}) # OuterObjectWithEnumProperty | Input enum (int) as post body
501501

502502
begin
503503

samples/client/petstore/ruby-faraday/lib/petstore/models/outer_enum_integer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
module Petstore
1717
class OuterEnumInteger
18+
NMINUS_1 = -1.freeze
1819
N0 = 0.freeze
1920
N1 = 1.freeze
2021
N2 = 2.freeze
2122

2223
def self.all_vars
23-
@all_vars ||= [N0, N1, N2].freeze
24+
@all_vars ||= [NMINUS_1, N0, N1, N2].freeze
2425
end
2526

2627
# Builds the enum from string

samples/client/petstore/ruby-httpx/docs/FakeApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ require 'time'
497497
require 'petstore'
498498

499499
api_instance = Petstore::FakeApi.new
500-
outer_object_with_enum_property = Petstore::OuterObjectWithEnumProperty.new({value: Petstore::OuterEnumInteger::N0}) # OuterObjectWithEnumProperty | Input enum (int) as post body
500+
outer_object_with_enum_property = Petstore::OuterObjectWithEnumProperty.new({value: Petstore::OuterEnumInteger::NMINUS_1}) # OuterObjectWithEnumProperty | Input enum (int) as post body
501501

502502
begin
503503

samples/client/petstore/ruby-httpx/lib/petstore/models/outer_enum_integer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
module Petstore
1717
class OuterEnumInteger
18+
NMINUS_1 = -1.freeze
1819
N0 = 0.freeze
1920
N1 = 1.freeze
2021
N2 = 2.freeze
2122

2223
def self.all_vars
23-
@all_vars ||= [N0, N1, N2].freeze
24+
@all_vars ||= [NMINUS_1, N0, N1, N2].freeze
2425
end
2526

2627
# Builds the enum from string

samples/client/petstore/ruby/docs/FakeApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ require 'time'
497497
require 'petstore'
498498

499499
api_instance = Petstore::FakeApi.new
500-
outer_object_with_enum_property = Petstore::OuterObjectWithEnumProperty.new({value: Petstore::OuterEnumInteger::N0}) # OuterObjectWithEnumProperty | Input enum (int) as post body
500+
outer_object_with_enum_property = Petstore::OuterObjectWithEnumProperty.new({value: Petstore::OuterEnumInteger::NMINUS_1}) # OuterObjectWithEnumProperty | Input enum (int) as post body
501501

502502
begin
503503

samples/client/petstore/ruby/lib/petstore/models/outer_enum_integer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
module Petstore
1717
class OuterEnumInteger
18+
NMINUS_1 = -1.freeze
1819
N0 = 0.freeze
1920
N1 = 1.freeze
2021
N2 = 2.freeze
2122

2223
def self.all_vars
23-
@all_vars ||= [N0, N1, N2].freeze
24+
@all_vars ||= [NMINUS_1, N0, N1, N2].freeze
2425
end
2526

2627
# Builds the enum from string

0 commit comments

Comments
 (0)