Skip to content

Commit

Permalink
fix default value and type declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Apr 8, 2018
1 parent 665f0bd commit ffa0e11
Show file tree
Hide file tree
Showing 24 changed files with 415 additions and 635 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openapitools.codegen.languages;

import io.swagger.v3.oas.models.media.*;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConstants;
Expand All @@ -17,12 +18,6 @@

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.DateSchema;
import io.swagger.v3.oas.models.media.DateTimeSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -78,15 +73,15 @@ public RubyClientCodegen() {

setReservedWordsLowerCase(
Arrays.asList(
// local variable names used in API methods (endpoints)
"local_var_path", "query_params", "header_params", "_header_accept", "_header_accept_result",
"_header_content_type", "form_params", "post_body", "auth_names",
// ruby reserved keywords
"__FILE__", "and", "def", "end", "in", "or", "self", "unless", "__LINE__",
"begin", "defined?", "ensure", "module", "redo", "super", "until", "BEGIN",
"break", "do", "false", "next", "rescue", "then", "when", "END", "case",
"else", "for", "nil", "retry", "true", "while", "alias", "class", "elsif",
"if", "not", "return", "undef", "yield")
// local variable names used in API methods (endpoints)
"local_var_path", "query_params", "header_params", "_header_accept", "_header_accept_result",
"_header_content_type", "form_params", "post_body", "auth_names",
// ruby reserved keywords
"__FILE__", "and", "def", "end", "in", "or", "self", "unless", "__LINE__",
"begin", "defined?", "ensure", "module", "redo", "super", "until", "BEGIN",
"break", "do", "false", "next", "rescue", "then", "when", "END", "case",
"else", "for", "nil", "retry", "true", "while", "alias", "class", "elsif",
"if", "not", "return", "undef", "yield")
);

typeMapping.clear();
Expand Down Expand Up @@ -202,7 +197,7 @@ public void processOpts() {

if (additionalProperties.containsKey(GEM_VERSION)) {
setGemVersion((String) additionalProperties.get(GEM_VERSION));
}else {
} else {
// not set, pass the default value to template
additionalProperties.put(GEM_VERSION, gemVersion);
}
Expand Down Expand Up @@ -325,7 +320,7 @@ public String generateModuleName(String gemName) {
/**
* Generate Ruby gem name from the module name, e.g. use "swagger_client" for "SwaggerClient".
*
* @param moduleName Ruby module naame
* @param moduleName Ruby module naame
* @return Ruby gem name
*/
@SuppressWarnings("static-method")
Expand All @@ -334,8 +329,8 @@ public String generateGemName(String moduleName) {
}

@Override
public String escapeReservedWord(String name) {
if(this.reservedWordsMappings().containsKey(name)) {
public String escapeReservedWord(String name) {
if (this.reservedWordsMappings().containsKey(name)) {
return this.reservedWordsMappings().get(name);
}
return "_" + name;
Expand Down Expand Up @@ -375,41 +370,42 @@ public String modelDocFileFolder() {
public String getTypeDeclaration(Schema schema) {
if (schema instanceof ArraySchema) {
Schema inner = ((ArraySchema) schema).getItems();
return String.format("%s[%s]", getSchemaType(schema), getTypeDeclaration(inner));
return getSchemaType(schema) + "<" + getTypeDeclaration(inner) + ">";
} else if (isMapSchema(schema)) {
Schema inner = (Schema) schema.getAdditionalProperties();
return String.format("%s[String, %s]", getSchemaType(schema), getTypeDeclaration(inner));
return getSchemaType(schema) + "<String, " + getTypeDeclaration(inner) + ">";
}

return super.getTypeDeclaration(schema);
}

@Override
public String toDefaultValue(Schema schema) {
if(schema instanceof StringSchema) {
if (schema.getDefault() != null) {
return String.format("\"%s\"", schema.getDefault());
public String toDefaultValue(Schema p) {
if (p instanceof IntegerSchema || p instanceof NumberSchema || p instanceof BooleanSchema) {
if (p.getDefault() != null) {
return p.getDefault().toString();
}
} else if (p instanceof StringSchema) {
StringSchema sp = (StringSchema) p;
if (sp.getDefault() != null) {
return "'" + escapeText(sp.getDefault()) + "'";
}
}

if (schema.getDefault() != null) {
return schema.getDefault().toString();
}

return null;
}

@Override
public String getSchemaType(Schema schema) {
String swaggerType = super.getSchemaType(schema);
String openAPIType = super.getSchemaType(schema);
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
if (languageSpecificPrimitives.contains(type)) {
return type;
}
} else {
type = swaggerType;
type = openAPIType;
}

if (type == null) {
Expand Down
9 changes: 5 additions & 4 deletions samples/client/petstore/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ require 'petstore'

api_instance = Petstore::AnotherFakeApi.new

body = Petstore::Client.new # Client | client model
client = Petstore::Client.new # Client | client model


begin
#To test special tags
result = api_instance.test_special_tags(body)
result = api_instance.test_special_tags(client)
p result
rescue Petstore::ApiError => e
puts "Exception when calling AnotherFakeApi->test_special_tags: #{e}"
Expand All @@ -80,6 +80,7 @@ Class | Method | HTTP request | Description
*Petstore::FakeApi* | [**fake_outer_composite_serialize**](docs/FakeApi.md#fake_outer_composite_serialize) | **POST** /fake/outer/composite |
*Petstore::FakeApi* | [**fake_outer_number_serialize**](docs/FakeApi.md#fake_outer_number_serialize) | **POST** /fake/outer/number |
*Petstore::FakeApi* | [**fake_outer_string_serialize**](docs/FakeApi.md#fake_outer_string_serialize) | **POST** /fake/outer/string |
*Petstore::FakeApi* | [**test_body_with_query_params**](docs/FakeApi.md#test_body_with_query_params) | **PUT** /fake/body-with-query-params |
*Petstore::FakeApi* | [**test_client_model**](docs/FakeApi.md#test_client_model) | **PATCH** /fake | To test \"client\" model
*Petstore::FakeApi* | [**test_endpoint_parameters**](docs/FakeApi.md#test_endpoint_parameters) | **POST** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
*Petstore::FakeApi* | [**test_enum_parameters**](docs/FakeApi.md#test_enum_parameters) | **GET** /fake | To test enum parameters
Expand Down Expand Up @@ -118,9 +119,11 @@ Class | Method | HTTP request | Description
- [Petstore::ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
- [Petstore::ArrayTest](docs/ArrayTest.md)
- [Petstore::Capitalization](docs/Capitalization.md)
- [Petstore::Cat](docs/Cat.md)
- [Petstore::Category](docs/Category.md)
- [Petstore::ClassModel](docs/ClassModel.md)
- [Petstore::Client](docs/Client.md)
- [Petstore::Dog](docs/Dog.md)
- [Petstore::EnumArrays](docs/EnumArrays.md)
- [Petstore::EnumClass](docs/EnumClass.md)
- [Petstore::EnumTest](docs/EnumTest.md)
Expand All @@ -144,8 +147,6 @@ Class | Method | HTTP request | Description
- [Petstore::SpecialModelName](docs/SpecialModelName.md)
- [Petstore::Tag](docs/Tag.md)
- [Petstore::User](docs/User.md)
- [Petstore::Cat](docs/Cat.md)
- [Petstore::Dog](docs/Dog.md)


## Documentation for Authorization
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/ruby/docs/AnotherFakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Method | HTTP request | Description


# **test_special_tags**
> Client test_special_tags(body)
> Client test_special_tags(client)
To test special tags

Expand All @@ -21,12 +21,12 @@ require 'petstore'

api_instance = Petstore::AnotherFakeApi.new

body = Petstore::Client.new # Client | client model
client = Petstore::Client.new # Client | client model


begin
#To test special tags
result = api_instance.test_special_tags(body)
result = api_instance.test_special_tags(client)
p result
rescue Petstore::ApiError => e
puts "Exception when calling AnotherFakeApi->test_special_tags: #{e}"
Expand All @@ -37,7 +37,7 @@ end

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**body** | [**Client**](Client.md)| client model |
**client** | [**Client**](Client.md)| client model |

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**array_array_number** | **Array&lt;Array&lt;Float&gt;&gt;** | | [optional]
**array_array_number** | **Array&lt;Array&lt;BigDecimal&gt;&gt;** | | [optional]


2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/docs/ArrayOfNumberOnly.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**array_number** | **Array&lt;Float&gt;** | | [optional]
**array_number** | [**Array&lt;BigDecimal&gt;**](BigDecimal.md) | | [optional]


Loading

0 comments on commit ffa0e11

Please sign in to comment.