[Swift4] Fix default value for enums (#652) (#656)

This fixes an issue that makes the swift 4 api client fail to compile when using enums with a default value.
The fix changes the toDefaultValue method to check if the property is of enum type, and then returns a properly formatted value.
Looking into the code, I wonder why toEnumDefaultValue is not being used here, which seems to be the case for the java implementation.
This commit is contained in:
Even André Fiskvik 2018-09-09 18:18:49 +02:00 committed by William Cheng
parent 7596fb7119
commit f80fa08a01
2 changed files with 60 additions and 0 deletions

View File

@ -510,6 +510,11 @@ public class Swift4Codegen extends DefaultCodegen implements CodegenConfig {
@Override
public String toDefaultValue(Schema p) {
if (p.getEnum() != null && !p.getEnum().isEmpty()) {
if (p.getDefault() != null) {
return "." + escapeText((String) p.getDefault());
}
}
if (ModelUtils.isIntegerSchema(p) || ModelUtils.isNumberSchema(p) || ModelUtils.isBooleanSchema(p)) {
if (p.getDefault() != null) {
return p.getDefault().toString();

View File

@ -0,0 +1,55 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.swift4;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.DefaultCodegen;
import org.openapitools.codegen.languages.Swift4Codegen;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collections;
@SuppressWarnings("static-method")
public class Swift4ModelEnumTest {
@Test(description = "convert a java model with an enum and a default value")
public void converterTest() {
final StringSchema enumSchema = new StringSchema();
enumSchema.setEnum(Arrays.asList("VALUE1", "VALUE2", "VALUE3"));
enumSchema.setDefault("VALUE2");
final Schema model = new Schema().type("object").addProperties("name", enumSchema);
final DefaultCodegen codegen = new Swift4Codegen();
final CodegenModel cm = codegen.fromModel("sample", model, Collections.singletonMap("sample", model));
Assert.assertEquals(cm.vars.size(), 1);
final CodegenProperty enumVar = cm.vars.get(0);
Assert.assertEquals(enumVar.baseName, "name");
Assert.assertEquals(enumVar.dataType, "String");
Assert.assertEquals(enumVar.datatypeWithEnum, "Name");
Assert.assertEquals(enumVar.name, "name");
Assert.assertEquals(enumVar.defaultValue, ".VALUE2");
Assert.assertEquals(enumVar.baseType, "String");
Assert.assertTrue(enumVar.isEnum);
}
}