Fix form datatype (array of string) (#339)

* fix data type for array of form parameters

* add test case to cover the form parameter issue
This commit is contained in:
William Cheng 2018-05-06 22:17:47 +08:00 committed by GitHub
parent a56d2333a2
commit b1eac05b2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 68 deletions

View File

@ -4110,6 +4110,7 @@ public class DefaultCodegen implements CodegenConfig {
codegenParameter.isContainer = true;
codegenParameter.isListContainer = true;
codegenParameter.description = s.getDescription();
codegenParameter.dataType = getTypeDeclaration(s);
// recursively add import
while (codegenProperty != null) {

View File

@ -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;
@ -46,79 +44,79 @@ import static org.testng.Assert.*;
*/
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!");
}
}
if (!apiFileGenerated) {
fail("Default api file is not generated!");
}
}
@Test
public void testInitialConfigValues() throws Exception {
final RubyClientCodegen codegen = new RubyClientCodegen();
codegen.processOpts();
@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);
}
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();
@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);
}
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();
@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");
}
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 {
@ -145,4 +143,18 @@ public class RubyClientCodegenTest {
}
}
@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>");
}
}

View File

@ -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)
}
@ -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

View File

@ -475,7 +475,7 @@ module Petstore
# @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 = {})