Skip to content

Commit d7311cd

Browse files
authored
[Ruby] add property, parameter name mapping (#16191)
* add property, parameter name mapping support to ruby generators * update samples
1 parent 90eacb6 commit d7311cd

File tree

11 files changed

+535
-1
lines changed

11 files changed

+535
-1
lines changed

bin/configs/ruby.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ additionalProperties:
99
gemName: petstore
1010
skipFormModel: "true"
1111
strictSpecBehavior: false
12+
nameMappings:
13+
_type: underscore_type
14+
type_: type_with_underscore
15+
parameterNameMappings:
16+
_type: underscore_type
17+
type_: type_with_underscore

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ public String toEnumDefaultValue(String value, String datatype) {
176176

177177
@Override
178178
public String toVarName(final String name) {
179+
// obtain the name from nameMapping directly if provided
180+
if (nameMapping.containsKey(name)) {
181+
return nameMapping.get(name);
182+
}
183+
179184
String varName = sanitizeName(name);
180185
// if it's all upper case, convert to lower case
181186
if (name.matches("^[A-Z_]*$")) {
@@ -201,6 +206,11 @@ public String toRegularExpression(String pattern) {
201206

202207
@Override
203208
public String toParamName(String name) {
209+
// obtain the name from parameterNameMapping directly if provided
210+
if (parameterNameMapping.containsKey(name)) {
211+
return parameterNameMapping.get(name);
212+
}
213+
204214
// should be the same as variable name
205215
return toVarName(name);
206216
}
@@ -265,5 +275,7 @@ public void postProcessFile(File file, String fileType) {
265275
}
266276

267277
@Override
268-
public GeneratorLanguage generatorLanguage() { return GeneratorLanguage.RUBY; }
278+
public GeneratorLanguage generatorLanguage() {
279+
return GeneratorLanguage.RUBY;
280+
}
269281
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,41 @@ paths:
12671267
responses:
12681268
200:
12691269
description: The instance started successfully
1270+
/fake/parameter-name-mapping:
1271+
get:
1272+
tags:
1273+
- fake
1274+
summary: parameter name mapping test
1275+
operationId: getParameterNameMapping
1276+
parameters:
1277+
- name: _type
1278+
in: header
1279+
description: _type
1280+
required: true
1281+
schema:
1282+
type: integer
1283+
format: int64
1284+
- name: type
1285+
in: query
1286+
description: type
1287+
required: true
1288+
schema:
1289+
type: string
1290+
- name: type_
1291+
in: header
1292+
description: type_
1293+
required: true
1294+
schema:
1295+
type: string
1296+
- name: http_debug_option
1297+
in: query
1298+
description: http debug option (to test parameter naming option)
1299+
required: true
1300+
schema:
1301+
type: string
1302+
responses:
1303+
200:
1304+
description: OK
12701305
servers:
12711306
- url: 'http://{server}.swagger.io:{port}/v2'
12721307
description: petstore server
@@ -2045,3 +2080,13 @@ components:
20452080
required:
20462081
- classname
20472082
additionalProperties: true
2083+
PropertyNameMapping:
2084+
properties:
2085+
http_debug_operation:
2086+
type: string
2087+
_type:
2088+
type: string
2089+
type:
2090+
type: string
2091+
type_:
2092+
type: string

samples/client/petstore/ruby/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ docs/OuterEnumIntegerDefaultValue.md
5656
docs/OuterObjectWithEnumProperty.md
5757
docs/Pet.md
5858
docs/PetApi.md
59+
docs/PropertyNameMapping.md
5960
docs/ReadOnlyFirst.md
6061
docs/SingleRefType.md
6162
docs/SpecialModelName.md
@@ -122,6 +123,7 @@ lib/petstore/models/outer_enum_integer.rb
122123
lib/petstore/models/outer_enum_integer_default_value.rb
123124
lib/petstore/models/outer_object_with_enum_property.rb
124125
lib/petstore/models/pet.rb
126+
lib/petstore/models/property_name_mapping.rb
125127
lib/petstore/models/read_only_first.rb
126128
lib/petstore/models/single_ref_type.rb
127129
lib/petstore/models/special_model_name.rb

samples/client/petstore/ruby/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Class | Method | HTTP request | Description
8585
*Petstore::FakeApi* | [**fake_outer_number_serialize**](docs/FakeApi.md#fake_outer_number_serialize) | **POST** /fake/outer/number |
8686
*Petstore::FakeApi* | [**fake_outer_string_serialize**](docs/FakeApi.md#fake_outer_string_serialize) | **POST** /fake/outer/string |
8787
*Petstore::FakeApi* | [**fake_property_enum_integer_serialize**](docs/FakeApi.md#fake_property_enum_integer_serialize) | **POST** /fake/property/enum-int |
88+
*Petstore::FakeApi* | [**get_parameter_name_mapping**](docs/FakeApi.md#get_parameter_name_mapping) | **GET** /fake/parameter-name-mapping | parameter name mapping test
8889
*Petstore::FakeApi* | [**test_body_with_binary**](docs/FakeApi.md#test_body_with_binary) | **PUT** /fake/body-with-binary |
8990
*Petstore::FakeApi* | [**test_body_with_file_schema**](docs/FakeApi.md#test_body_with_file_schema) | **PUT** /fake/body-with-file-schema |
9091
*Petstore::FakeApi* | [**test_body_with_query_params**](docs/FakeApi.md#test_body_with_query_params) | **PUT** /fake/body-with-query-params |
@@ -166,6 +167,7 @@ Class | Method | HTTP request | Description
166167
- [Petstore::OuterEnumIntegerDefaultValue](docs/OuterEnumIntegerDefaultValue.md)
167168
- [Petstore::OuterObjectWithEnumProperty](docs/OuterObjectWithEnumProperty.md)
168169
- [Petstore::Pet](docs/Pet.md)
170+
- [Petstore::PropertyNameMapping](docs/PropertyNameMapping.md)
169171
- [Petstore::ReadOnlyFirst](docs/ReadOnlyFirst.md)
170172
- [Petstore::SingleRefType](docs/SingleRefType.md)
171173
- [Petstore::SpecialModelName](docs/SpecialModelName.md)

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
1212
| [**fake_outer_number_serialize**](FakeApi.md#fake_outer_number_serialize) | **POST** /fake/outer/number | |
1313
| [**fake_outer_string_serialize**](FakeApi.md#fake_outer_string_serialize) | **POST** /fake/outer/string | |
1414
| [**fake_property_enum_integer_serialize**](FakeApi.md#fake_property_enum_integer_serialize) | **POST** /fake/property/enum-int | |
15+
| [**get_parameter_name_mapping**](FakeApi.md#get_parameter_name_mapping) | **GET** /fake/parameter-name-mapping | parameter name mapping test |
1516
| [**test_body_with_binary**](FakeApi.md#test_body_with_binary) | **PUT** /fake/body-with-binary | |
1617
| [**test_body_with_file_schema**](FakeApi.md#test_body_with_file_schema) | **PUT** /fake/body-with-file-schema | |
1718
| [**test_body_with_query_params**](FakeApi.md#test_body_with_query_params) | **PUT** /fake/body-with-query-params | |
@@ -542,6 +543,73 @@ No authorization required
542543
- **Accept**: */*
543544

544545

546+
## get_parameter_name_mapping
547+
548+
> get_parameter_name_mapping(underscore_type, type, type_with_underscore, http_debug_option)
549+
550+
parameter name mapping test
551+
552+
### Examples
553+
554+
```ruby
555+
require 'time'
556+
require 'petstore'
557+
558+
api_instance = Petstore::FakeApi.new
559+
underscore_type = 789 # Integer | _type
560+
type = 'type_example' # String | type
561+
type_with_underscore = 'type_with_underscore_example' # String | type_
562+
http_debug_option = 'http_debug_option_example' # String | http debug option (to test parameter naming option)
563+
564+
begin
565+
# parameter name mapping test
566+
api_instance.get_parameter_name_mapping(underscore_type, type, type_with_underscore, http_debug_option)
567+
rescue Petstore::ApiError => e
568+
puts "Error when calling FakeApi->get_parameter_name_mapping: #{e}"
569+
end
570+
```
571+
572+
#### Using the get_parameter_name_mapping_with_http_info variant
573+
574+
This returns an Array which contains the response data (`nil` in this case), status code and headers.
575+
576+
> <Array(nil, Integer, Hash)> get_parameter_name_mapping_with_http_info(underscore_type, type, type_with_underscore, http_debug_option)
577+
578+
```ruby
579+
begin
580+
# parameter name mapping test
581+
data, status_code, headers = api_instance.get_parameter_name_mapping_with_http_info(underscore_type, type, type_with_underscore, http_debug_option)
582+
p status_code # => 2xx
583+
p headers # => { ... }
584+
p data # => nil
585+
rescue Petstore::ApiError => e
586+
puts "Error when calling FakeApi->get_parameter_name_mapping_with_http_info: #{e}"
587+
end
588+
```
589+
590+
### Parameters
591+
592+
| Name | Type | Description | Notes |
593+
| ---- | ---- | ----------- | ----- |
594+
| **underscore_type** | **Integer** | _type | |
595+
| **type** | **String** | type | |
596+
| **type_with_underscore** | **String** | type_ | |
597+
| **http_debug_option** | **String** | http debug option (to test parameter naming option) | |
598+
599+
### Return type
600+
601+
nil (empty response body)
602+
603+
### Authorization
604+
605+
No authorization required
606+
607+
### HTTP request headers
608+
609+
- **Content-Type**: Not defined
610+
- **Accept**: Not defined
611+
612+
545613
## test_body_with_binary
546614

547615
> test_body_with_binary(body)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Petstore::PropertyNameMapping
2+
3+
## Properties
4+
5+
| Name | Type | Description | Notes |
6+
| ---- | ---- | ----------- | ----- |
7+
| **http_debug_operation** | **String** | | [optional] |
8+
| **underscore_type** | **String** | | [optional] |
9+
| **type** | **String** | | [optional] |
10+
| **type_with_underscore** | **String** | | [optional] |
11+
12+
## Example
13+
14+
```ruby
15+
require 'petstore'
16+
17+
instance = Petstore::PropertyNameMapping.new(
18+
http_debug_operation: null,
19+
underscore_type: null,
20+
type: null,
21+
type_with_underscore: null
22+
)
23+
```
24+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
require 'petstore/models/outer_enum_integer_default_value'
6161
require 'petstore/models/outer_object_with_enum_property'
6262
require 'petstore/models/pet'
63+
require 'petstore/models/property_name_mapping'
6364
require 'petstore/models/read_only_first'
6465
require 'petstore/models/single_ref_type'
6566
require 'petstore/models/special_model_name'

samples/client/petstore/ruby/lib/petstore/api/fake_api.rb

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,87 @@ def fake_property_enum_integer_serialize_with_http_info(outer_object_with_enum_p
513513
return data, status_code, headers
514514
end
515515

516+
# parameter name mapping test
517+
# @param underscore_type [Integer] _type
518+
# @param type [String] type
519+
# @param type_with_underscore [String] type_
520+
# @param http_debug_option [String] http debug option (to test parameter naming option)
521+
# @param [Hash] opts the optional parameters
522+
# @return [nil]
523+
def get_parameter_name_mapping(underscore_type, type, type_with_underscore, http_debug_option, opts = {})
524+
get_parameter_name_mapping_with_http_info(underscore_type, type, type_with_underscore, http_debug_option, opts)
525+
nil
526+
end
527+
528+
# parameter name mapping test
529+
# @param underscore_type [Integer] _type
530+
# @param type [String] type
531+
# @param type_with_underscore [String] type_
532+
# @param http_debug_option [String] http debug option (to test parameter naming option)
533+
# @param [Hash] opts the optional parameters
534+
# @return [Array<(nil, Integer, Hash)>] nil, response status code and response headers
535+
def get_parameter_name_mapping_with_http_info(underscore_type, type, type_with_underscore, http_debug_option, opts = {})
536+
if @api_client.config.debugging
537+
@api_client.config.logger.debug 'Calling API: FakeApi.get_parameter_name_mapping ...'
538+
end
539+
# verify the required parameter 'underscore_type' is set
540+
if @api_client.config.client_side_validation && underscore_type.nil?
541+
fail ArgumentError, "Missing the required parameter 'underscore_type' when calling FakeApi.get_parameter_name_mapping"
542+
end
543+
# verify the required parameter 'type' is set
544+
if @api_client.config.client_side_validation && type.nil?
545+
fail ArgumentError, "Missing the required parameter 'type' when calling FakeApi.get_parameter_name_mapping"
546+
end
547+
# verify the required parameter 'type_with_underscore' is set
548+
if @api_client.config.client_side_validation && type_with_underscore.nil?
549+
fail ArgumentError, "Missing the required parameter 'type_with_underscore' when calling FakeApi.get_parameter_name_mapping"
550+
end
551+
# verify the required parameter 'http_debug_option' is set
552+
if @api_client.config.client_side_validation && http_debug_option.nil?
553+
fail ArgumentError, "Missing the required parameter 'http_debug_option' when calling FakeApi.get_parameter_name_mapping"
554+
end
555+
# resource path
556+
local_var_path = '/fake/parameter-name-mapping'
557+
558+
# query parameters
559+
query_params = opts[:query_params] || {}
560+
query_params[:'type'] = type
561+
query_params[:'http_debug_option'] = http_debug_option
562+
563+
# header parameters
564+
header_params = opts[:header_params] || {}
565+
header_params[:'_type'] = underscore_type
566+
header_params[:'type_'] = type_with_underscore
567+
568+
# form parameters
569+
form_params = opts[:form_params] || {}
570+
571+
# http body (model)
572+
post_body = opts[:debug_body]
573+
574+
# return_type
575+
return_type = opts[:debug_return_type]
576+
577+
# auth_names
578+
auth_names = opts[:debug_auth_names] || []
579+
580+
new_options = opts.merge(
581+
:operation => :"FakeApi.get_parameter_name_mapping",
582+
:header_params => header_params,
583+
:query_params => query_params,
584+
:form_params => form_params,
585+
:body => post_body,
586+
:auth_names => auth_names,
587+
:return_type => return_type
588+
)
589+
590+
data, status_code, headers = @api_client.call_api(:GET, local_var_path, new_options)
591+
if @api_client.config.debugging
592+
@api_client.config.logger.debug "API called: FakeApi#get_parameter_name_mapping\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
593+
end
594+
return data, status_code, headers
595+
end
596+
516597
# For this test, the body has to be a binary file.
517598
# @param body [File] image to upload
518599
# @param [Hash] opts the optional parameters

0 commit comments

Comments
 (0)