Compare commits

..

1 Commits

Author SHA1 Message Date
William Cheng
bea6af7f37 fix enum type comparison for string 2020-07-09 11:23:05 +08:00
5 changed files with 28 additions and 11 deletions

View File

@@ -5306,7 +5306,7 @@ public class DefaultCodegen implements CodegenConfig {
if (var.defaultValue != null) {
String enumName = null;
final String enumDefaultValue;
if ("string".equalsIgnoreCase(dataType)) {
if (typeMapping.get("string").equalsIgnoreCase(dataType)) {
enumDefaultValue = toEnumValue(var.defaultValue, dataType);
} else {
enumDefaultValue = var.defaultValue;

View File

@@ -158,6 +158,8 @@ public class NimClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("DateTime", "string");
typeMapping.put("password", "string");
typeMapping.put("file", "string");
typeMapping.put("string", "string");
}
public void setPackageName(String packageName) {

View File

@@ -1222,6 +1222,8 @@ public class ModelUtils {
public static String getParentName(ComposedSchema composedSchema, Map<String, Schema> allSchemas) {
List<Schema> interfaces = getInterfaces(composedSchema);
int nullSchemaChildrenCount = 0;
boolean hasAmbiguousParents = false;
List<String> refedWithoutDiscriminator = new ArrayList<>();
if (interfaces != null && !interfaces.isEmpty()) {
for (Schema schema : interfaces) {
@@ -1236,10 +1238,9 @@ public class ModelUtils {
// discriminator.propertyName is used
return parentName;
} else {
// TOOD to be removed in 6.x release
LOGGER.warn("[deprecated] inheritance without use of 'discriminator.propertyName' has been deprecated" +
" in the 5.x release. Composed schema name: {}. Title: {}",
composedSchema.getName(), composedSchema.getTitle());
// not a parent since discriminator.propertyName is not set
hasAmbiguousParents = true;
refedWithoutDiscriminator.add(parentName);
}
} else {
// not a ref, doing nothing, except counting the number of times the 'null' type
@@ -1252,6 +1253,21 @@ public class ModelUtils {
}
}
}
if (refedWithoutDiscriminator.size() == 1 && nullSchemaChildrenCount == 1) {
// One schema is a $ref and the other is the 'null' type, so the parent is obvious.
// In this particular case there is no need to specify a discriminator.
hasAmbiguousParents = false;
}
}
// parent name only makes sense when there is a single obvious parent
if (refedWithoutDiscriminator.size() == 1) {
if (hasAmbiguousParents) {
LOGGER.warn("[deprecated] inheritance without use of 'discriminator.propertyName' is deprecated " +
"and will be removed in a future release. Generating model for composed schema name: {}. Title: {}",
composedSchema.getName(), composedSchema.getTitle());
}
return refedWithoutDiscriminator.get(0);
}
return null;

View File

@@ -1372,9 +1372,9 @@ public class DefaultCodegenTest {
Schema schema = openAPI.getComponents().getSchemas().get("NewMessageEventCoreNoOwnProps");
codegen.setOpenAPI(openAPI);
CodegenModel model = codegen.fromModel("NewMessageEventCoreNoOwnProps", schema);
Assert.assertEquals(getNames(model.getVars()), Arrays.asList("id","message"));
Assert.assertNull(model.parent);
Assert.assertNull(model.allParents);
Assert.assertEquals(getNames(model.getVars()), Collections.emptyList());
Assert.assertEquals(model.parent, "MessageEventCore");
Assert.assertEquals(model.allParents, Collections.singletonList("MessageEventCore"));
}
class CodegenWithMultipleInheritance extends DefaultCodegen {

View File

@@ -25,7 +25,6 @@ import io.swagger.v3.oas.models.media.Discriminator;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
@@ -58,8 +57,8 @@ public class JavaInheritanceTest {
Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample");
Assert.assertNull(cm.parent);
Assert.assertEquals(cm.imports, Collections.emptySet());
Assert.assertEquals(cm.parent, "Base");
Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
}
@Test(description = "convert a composed model with discriminator")