Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mapping features to crystal client generator #16253

Merged
merged 1 commit into from
Aug 5, 2023
Merged
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
9 changes: 9 additions & 0 deletions bin/configs/crystal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ additionalProperties:
moduleName: Petstore
shardName: petstore
strictSpecBehavior: false
modelNameMappings:
PropertyNameMapping: AnotherPropertyNameMapping
nameMappings:
_type: underscore_type
type_: type_with_underscore
type-: type_with_dash
parameterNameMappings:
_type: underscore_type
type_: type_with_underscore
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ public String toModelImport(String name) {

@Override
public String toModelName(final String name) {
// obtain the name from modelNameMapping directly if provided
if (modelNameMapping.containsKey(name)) {
return modelNameMapping.get(name);
}

String modelName;
modelName = sanitizeModelName(name);

Expand Down Expand Up @@ -409,6 +414,11 @@ public String sanitizeModelName(String modelName) {

@Override
public String toModelFilename(String name) {
// obtain the name from modelNameMapping directly if provided
if (modelNameMapping.containsKey(name)) {
return underscore(modelNameMapping.get(name));
}

return underscore(toModelName(name));
}

Expand Down Expand Up @@ -867,6 +877,11 @@ public String toEnumDefaultValue(String value, String datatype) {

@Override
public String toVarName(final String name) {
// obtain the name from nameMapping directly if provided
if (nameMapping.containsKey(name)) {
return nameMapping.get(name);
}

String varName;
// sanitize name
varName = sanitizeName(name);
Expand Down Expand Up @@ -894,6 +909,11 @@ public String toRegularExpression(String pattern) {

@Override
public String toParamName(String name) {
// obtain the name from parameterNameMapping directly if provided
if (parameterNameMapping.containsKey(name)) {
return parameterNameMapping.get(name);
}

// should be the same as variable name
return toVarName(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,47 @@ paths:
description: User not found
security:
- api_key: []
/fake/parameter-name-mapping:
get:
tags:
- fake
summary: parameter name mapping test
operationId: getParameterNameMapping
parameters:
- name: _type
in: header
description: _type
required: true
schema:
type: integer
format: int64
- name: type
in: query
description: type
required: true
schema:
type: string
- name: type_
in: header
description: type_
required: true
schema:
type: string
- name: type-
in: header
description: type-
required: true
schema:
type: string
- name: http_debug_option
in: query
description: http debug option (to test parameter naming option)
required: true
schema:
type: string
responses:
200:
description: OK
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Expand Down Expand Up @@ -809,3 +850,13 @@ components:
description: A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
type: string
pattern: '/^image_\d{1,3}$/i'
PropertyNameMapping:
properties:
http_debug_operation:
type: string
_type:
type: string
type:
type: string
type_:
type: string
2 changes: 2 additions & 0 deletions samples/client/petstore/crystal/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ git_push.sh
shard.yml
spec/spec_helper.cr
src/petstore.cr
src/petstore/api/fake_api.cr
src/petstore/api/pet_api.cr
src/petstore/api/store_api.cr
src/petstore/api/user_api.cr
src/petstore/api_client.cr
src/petstore/api_error.cr
src/petstore/configuration.cr
src/petstore/models/another_property_name_mapping.cr
src/petstore/models/api_response.cr
src/petstore/models/category.cr
src/petstore/models/format_test.cr
Expand Down
41 changes: 41 additions & 0 deletions samples/client/petstore/crystal/spec/api/fake_api_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# #OpenAPI Petstore
#
##This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#
#The version of the OpenAPI document: 1.0.0
#
#Generated by: https://openapi-generator.tech
#OpenAPI Generator version: 7.0.0-SNAPSHOT
#

require "../spec_helper"
require "json"
require "time"

# Unit tests for Petstore::FakeApi
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate
describe "FakeApi" do
describe "test an instance of FakeApi" do
it "should create an instance of FakeApi" do
api_instance = Petstore::FakeApi.new
# TODO expect(api_instance).to be_instance_of(Petstore::FakeApi)
end
end

# unit tests for get_parameter_name_mapping
# parameter name mapping test
# @param _type _type
# @param _type2 type
# @param type_ type_
# @param type_2 type-
# @param http_debug_option http debug option (to test parameter naming option)
# @param [Hash] opts the optional parameters
# @return [nil]
describe "get_parameter_name_mapping test" do
it "should work" do
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
end
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# #OpenAPI Petstore
#
##This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#
#The version of the OpenAPI document: 1.0.0
#
#Generated by: https://openapi-generator.tech
#OpenAPI Generator version: 7.0.0-SNAPSHOT
#

require "../spec_helper"
require "json"
require "time"

# Unit tests for Petstore::AnotherPropertyNameMapping
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate
describe Petstore::AnotherPropertyNameMapping do

describe "test an instance of AnotherPropertyNameMapping" do
it "should create an instance of AnotherPropertyNameMapping" do
#instance = Petstore::AnotherPropertyNameMapping.new
#expect(instance).to be_instance_of(Petstore::AnotherPropertyNameMapping)
end
end
describe "test attribute 'http_debug_operation'" do
it "should work" do
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
end
end

describe "test attribute 'underscore_type'" do
it "should work" do
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
end
end

describe "test attribute '_type'" do
it "should work" do
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
end
end

describe "test attribute 'type_with_underscore'" do
it "should work" do
# assertion here. ref: https://crystal-lang.org/reference/guides/testing.html
end
end

end
104 changes: 104 additions & 0 deletions samples/client/petstore/crystal/src/petstore/api/fake_api.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# #OpenAPI Petstore
#
##This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#
#The version of the OpenAPI document: 1.0.0
#
#Generated by: https://openapi-generator.tech
#OpenAPI Generator version: 7.0.0-SNAPSHOT
#

require "uri"

module Petstore
class FakeApi
property api_client : ApiClient

def initialize(api_client = ApiClient.default)
@api_client = api_client
end
# parameter name mapping test
# @param underscore_type [Int64] _type
# @param _type [String] type
# @param type_with_underscore [String] type_
# @param type_with_dash [String] type-
# @param http_debug_option [String] http debug option (to test parameter naming option)
# @return [nil]
def get_parameter_name_mapping(underscore_type : Int64, _type : String, type_with_underscore : String, type_with_dash : String, http_debug_option : String)
get_parameter_name_mapping_with_http_info(underscore_type, _type, type_with_underscore, type_with_dash, http_debug_option)
nil
end

# parameter name mapping test
# @param underscore_type [Int64] _type
# @param _type [String] type
# @param type_with_underscore [String] type_
# @param type_with_dash [String] type-
# @param http_debug_option [String] http debug option (to test parameter naming option)
# @return [Array<(nil, Integer, Hash)>] nil, response status code and response headers
def get_parameter_name_mapping_with_http_info(underscore_type : Int64, _type : String, type_with_underscore : String, type_with_dash : String, http_debug_option : String)
if @api_client.config.debugging
Log.debug {"Calling API: FakeApi.get_parameter_name_mapping ..."}
end
# verify the required parameter "underscore_type" is set
if @api_client.config.client_side_validation && underscore_type.nil?
raise ArgumentError.new("Missing the required parameter 'underscore_type' when calling FakeApi.get_parameter_name_mapping")
end
# verify the required parameter "_type" is set
if @api_client.config.client_side_validation && _type.nil?
raise ArgumentError.new("Missing the required parameter '_type' when calling FakeApi.get_parameter_name_mapping")
end
# verify the required parameter "type_with_underscore" is set
if @api_client.config.client_side_validation && type_with_underscore.nil?
raise ArgumentError.new("Missing the required parameter 'type_with_underscore' when calling FakeApi.get_parameter_name_mapping")
end
# verify the required parameter "type_with_dash" is set
if @api_client.config.client_side_validation && type_with_dash.nil?
raise ArgumentError.new("Missing the required parameter 'type_with_dash' when calling FakeApi.get_parameter_name_mapping")
end
# verify the required parameter "http_debug_option" is set
if @api_client.config.client_side_validation && http_debug_option.nil?
raise ArgumentError.new("Missing the required parameter 'http_debug_option' when calling FakeApi.get_parameter_name_mapping")
end
# resource path
local_var_path = "/fake/parameter-name-mapping"

# query parameters
query_params = Hash(String, String).new
query_params["type"] = _type.to_s unless _type.nil?
query_params["http_debug_option"] = http_debug_option.to_s unless http_debug_option.nil?

# header parameters
header_params = Hash(String, String).new
header_params["_type"] = underscore_type
header_params["type_"] = type_with_underscore
header_params["type-"] = type_with_dash

# form parameters
form_params = Hash(Symbol, (String | ::File)).new

# http body (model)
post_body = nil

# return_type
return_type = nil

# auth_names
auth_names = [] of String

data, status_code, headers = @api_client.call_api(:GET,
local_var_path,
:"FakeApi.get_parameter_name_mapping",
return_type,
post_body,
auth_names,
header_params,
query_params,
form_params)
if @api_client.config.debugging
Log.debug {"API called: FakeApi#get_parameter_name_mapping\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"}
end
return nil, status_code, headers
end
end
end
Loading
Loading