Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][scala][template] scala generate java.math.BigDecimal instead of scala type #5514

Merged
merged 2 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;

import static org.openapitools.codegen.utils.StringUtils.camelize;
import static org.openapitools.codegen.utils.StringUtils.underscore;
Expand Down Expand Up @@ -105,11 +102,27 @@ public AbstractScalaCodegen() {
"yield"
));

importMapping = new HashMap<String, String>();
importMapping.put("ListBuffer", "scala.collection.mutable.ListBuffer");
// although Seq is a predef, before Scala 2.13, it _could_ refer to a mutable Seq in some cases.
importMapping.put("Seq", "scala.collection.immutable.Seq");
importMapping.put("Set", "scala.collection.immutable.Set");
importMapping.put("ListSet", "scala.collection.immutable.ListSet");
// fallback to java types
importMapping.put("UUID", "java.util.UUID");
importMapping.put("URI", "java.net.URI");
importMapping.put("File", "java.io.File");
importMapping.put("Timestamp", "java.sql.Timestamp");
importMapping.put("HashMap", "java.util.HashMap");
importMapping.put("Array", "java.util.List");
importMapping.put("ArrayList", "java.util.ArrayList");
// todo remove legacy date types
importMapping.put("Date", "java.util.Date");
importMapping.put("DateTime", "org.joda.time.*");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this and others, we should use java.time rather than Joda. Non-standard libraries should be optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've copied that from DefaultCodegen for backward compatibility.
Next PR #5291 should solve java.time which should become default since 5.0.0, i think.

importMapping.put("LocalDateTime", "org.joda.time.*");
importMapping.put("LocalDate", "org.joda.time.*");
importMapping.put("LocalTime", "org.joda.time.*");


instantiationTypes.put("set", "Set");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ public void convertVarNameOriginalCase() {
Assert.assertEquals(fakeScalaCodegen.toVarName("1AAaa"), "`1AAaa`");
}

@Test
public void checkScalaTypeImportMapping() {
Assert.assertEquals(fakeScalaCodegen.importMapping().get("Seq"),
"scala.collection.immutable.Seq", "Seq is immutable collection");
Assert.assertEquals(fakeScalaCodegen.importMapping().get("Set"),
"scala.collection.immutable.Set", "Set is immutable collection");
Assert.assertFalse(fakeScalaCodegen.importMapping().containsKey("List"),
"List is a Scala type and must not be imported");
Assert.assertFalse(fakeScalaCodegen.importMapping().containsKey("BigDecimal"),
"BigDecimal is a Scala type and must not be imported");
Assert.assertFalse(fakeScalaCodegen.importMapping().containsKey("BigInt"),
"BigInt is a Scala type and must not be imported");
}
}