Skip to content

Commit

Permalink
Fix form datatype (array of string) (#339)
Browse files Browse the repository at this point in the history
* fix data type for array of form parameters

* add test case to cover the form parameter issue
  • Loading branch information
wing328 authored May 6, 2018
1 parent a56d233 commit b1eac05
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4110,6 +4110,7 @@ public List<CodegenParameter> fromRequestBodyToFormParameters(RequestBody body,
codegenParameter.isContainer = true;
codegenParameter.isListContainer = true;
codegenParameter.description = s.getDescription();
codegenParameter.dataType = getTypeDeclaration(s);

// recursively add import
while (codegenProperty != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

package org.openapitools.codegen.ruby;

import org.openapitools.codegen.ClientOpts;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;
import io.swagger.models.parameters.FormParameter;
import io.swagger.v3.oas.models.Operation;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.RubyClientCodegen;

import io.swagger.parser.OpenAPIParser;
Expand All @@ -46,79 +44,79 @@
*/
public class RubyClientCodegenTest {

public TemporaryFolder folder = new TemporaryFolder();
public TemporaryFolder folder = new TemporaryFolder();

@BeforeMethod
public void setUp() throws Exception {
folder.create();
}
@BeforeMethod
public void setUp() throws Exception {
folder.create();
}

@AfterMethod
public void tearDown() throws Exception {
folder.delete();
}
@AfterMethod
public void tearDown() throws Exception {
folder.delete();
}

@Test
public void testGenerateRubyClientWithHtmlEntity() throws Exception {
final File output = folder.getRoot();
@Test
public void testGenerateRubyClientWithHtmlEntity() throws Exception {
final File output = folder.getRoot();

final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/pathWithHtmlEntity.yaml", null, new ParseOptions()).getOpenAPI();
CodegenConfig codegenConfig = new RubyClientCodegen();
codegenConfig.setOutputDir(output.getAbsolutePath());
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/pathWithHtmlEntity.yaml", null, new ParseOptions()).getOpenAPI();
CodegenConfig codegenConfig = new RubyClientCodegen();
codegenConfig.setOutputDir(output.getAbsolutePath());

ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).openAPI(openAPI).config(codegenConfig);
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).openAPI(openAPI).config(codegenConfig);

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();
boolean apiFileGenerated = false;
for (File file : files) {
if (file.getName().equals("default_api.rb")) {
apiFileGenerated = true;
// Ruby client should set the path unescaped in the api file
assertTrue(FileUtils.readFileToString(file, StandardCharsets.UTF_8).contains("local_var_path = '/foo=bar'"));
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();
boolean apiFileGenerated = false;
for (File file : files) {
if (file.getName().equals("default_api.rb")) {
apiFileGenerated = true;
// Ruby client should set the path unescaped in the api file
assertTrue(FileUtils.readFileToString(file, StandardCharsets.UTF_8).contains("local_var_path = '/foo=bar'"));
}
}
}
if (!apiFileGenerated) {
fail("Default api file is not generated!");
}
}

@Test
public void testInitialConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "models");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), null);
Assert.assertEquals(codegen.apiPackage(), "api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), null);
}

@Test
public void testSettersForConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.setHideGenerationTimestamp(false);
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
}

@Test
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, false);
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "ruby-models");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "ruby-api");
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "ruby-models");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "ruby-api");
}
if (!apiFileGenerated) {
fail("Default api file is not generated!");
}
}

@Test
public void testInitialConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertEquals(codegen.modelPackage(), "models");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), null);
Assert.assertEquals(codegen.apiPackage(), "api");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), null);
}

@Test
public void testSettersForConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.setHideGenerationTimestamp(false);
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
}

@Test
public void testAdditionalPropertiesPutForConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.additionalProperties().put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, false);
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "ruby-models");
codegen.additionalProperties().put(CodegenConstants.API_PACKAGE, "ruby-api");
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "ruby-models");
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "ruby-api");
}

@Test
public void testBooleanDefaultValue() throws Exception {
Expand All @@ -145,4 +143,18 @@ public void testBooleanDefaultValue() throws Exception {
}
}

@Test(description = "verify enum parameters (query, form, header)")
public void enumParameterTest() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new RubyClientCodegen();
final String path = "/fake";
final Operation p = openAPI.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "get", p, openAPI.getComponents().getSchemas());

Assert.assertEquals(op.formParams.size(), 2);
CodegenParameter fp = op.formParams.get(0);

Assert.assertEquals(fp.dataType, "Array<String>");
}

}
4 changes: 2 additions & 2 deletions samples/client/petstore/ruby/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ opts = {
enum_query_string: '-efg', # String | Query parameter enum test (string)
enum_query_integer: 56, # Integer | Query parameter enum test (double)
enum_query_double: 3.4, # Float | Query parameter enum test (double)
enum_form_string_array: nil, # Array<String> | Form parameter enum test (string array)
enum_form_string_array: '$', # Array<String> | Form parameter enum test (string array)
enum_form_string: '-efg' # String | Form parameter enum test (string)
}

Expand All @@ -408,7 +408,7 @@ Name | Type | Description | Notes
**enum_query_string** | **String**| Query parameter enum test (string) | [optional] [default to &#39;-efg&#39;]
**enum_query_integer** | **Integer**| Query parameter enum test (double) | [optional]
**enum_query_double** | **Float**| Query parameter enum test (double) | [optional]
**enum_form_string_array** | [**Array&lt;String&gt;**](Array.md)| Form parameter enum test (string array) | [optional]
**enum_form_string_array** | **Array&lt;String&gt;**| Form parameter enum test (string array) | [optional] [default to &#39;$&#39;]
**enum_form_string** | **String**| Form parameter enum test (string) | [optional] [default to &#39;-efg&#39;]

### Return type
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/lib/petstore/api/fake_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def test_endpoint_parameters_with_http_info(number, double, pattern_without_deli
# @option opts [String] :enum_query_string Query parameter enum test (string) (default to '-efg')
# @option opts [Integer] :enum_query_integer Query parameter enum test (double)
# @option opts [Float] :enum_query_double Query parameter enum test (double)
# @option opts [Array<String>] :enum_form_string_array Form parameter enum test (string array)
# @option opts [Array<String>] :enum_form_string_array Form parameter enum test (string array) (default to '$')
# @option opts [String] :enum_form_string Form parameter enum test (string) (default to '-efg')
# @return [nil]
def test_enum_parameters(opts = {})
Expand Down

0 comments on commit b1eac05

Please sign in to comment.