Skip to content

Commit

Permalink
[dart] type improvements (#8111)
Browse files Browse the repository at this point in the history
* [dart] Configure and use import mapping

This prevents models from being generated which would clash with exisiting dart types, e.g. List.

* [dart] Fix decimal format not supported

* [dart-dio] Remove redundant modelToIgnore & ignore dart:core import

* modelToIgnore is now handled via importMappings the same way other generators do this
* choose not to import dart:core as this is available by default

* [dart-dio] EnumClass is a reserved word in built_value

* Review changes

* Fix regenerate docs
  • Loading branch information
kuhnroyal authored Dec 8, 2020
1 parent 99d8371 commit 7fae4ab
Show file tree
Hide file tree
Showing 27 changed files with 121 additions and 237 deletions.
11 changes: 11 additions & 0 deletions docs/generators/dart-dio.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|BuiltList|package:built_collection/built_collection.dart|
|BuiltMap|package:built_collection/built_collection.dart|
|BuiltSet|package:built_collection/built_collection.dart|
|DateTime|dart:core|
|JsonObject|package:built_value/json_object.dart|
|List|dart:core|
|Map|dart:core|
|Object|dart:core|
|Set|dart:core|
|String|dart:core|
|Uint8List|dart:typed_data|
|bool|dart:core|
|double|dart:core|
|dynamic|dart:core|
|int|dart:core|
|num|dart:core|


## INSTANTIATION TYPES
Expand Down
11 changes: 11 additions & 0 deletions docs/generators/dart-jaguar.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ These options may be applied as additional-properties (cli) or configOptions (pl

| Type/Alias | Imports |
| ---------- | ------- |
|DateTime|dart:core|
|List|dart:core|
|Map|dart:core|
|Object|dart:core|
|Set|dart:core|
|String|dart:core|
|bool|dart:core|
|double|dart:core|
|dynamic|dart:core|
|int|dart:core|
|num|dart:core|


## INSTANTIATION TYPES
Expand Down
11 changes: 11 additions & 0 deletions docs/generators/dart.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ These options may be applied as additional-properties (cli) or configOptions (pl

| Type/Alias | Imports |
| ---------- | ------- |
|DateTime|dart:core|
|List|dart:core|
|Map|dart:core|
|Object|dart:core|
|Set|dart:core|
|String|dart:core|
|bool|dart:core|
|double|dart:core|
|dynamic|dart:core|
|int|dart:core|
|num|dart:core|


## INSTANTIATION TYPES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenProperty;
Expand All @@ -56,7 +55,7 @@
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;

public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
public class DartClientCodegen extends DefaultCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(DartClientCodegen.class);

public static final String PUB_LIBRARY = "pubLibrary";
Expand Down Expand Up @@ -161,8 +160,7 @@ public DartClientCodegen() {
typeMapping.put("number", "num");
typeMapping.put("float", "double");
typeMapping.put("double", "double");
typeMapping.put("object", "Object");
typeMapping.put("AnyType", "Object");
typeMapping.put("decimal", "double");
typeMapping.put("integer", "int");
typeMapping.put("Date", "DateTime");
typeMapping.put("date", "DateTime");
Expand All @@ -171,6 +169,24 @@ public DartClientCodegen() {
typeMapping.put("UUID", "String");
typeMapping.put("URI", "String");
typeMapping.put("ByteArray", "String");
typeMapping.put("object", "Object");
typeMapping.put("AnyType", "Object");

// These are needed as they prevent models from being generated
// which would clash with existing types, e.g. List
// Importing dart:core doesn't hurt but a subclass may choose to skip
// dart:core imports.
importMapping.put("dynamic", "dart:core");
importMapping.put("Object", "dart:core");
importMapping.put("String", "dart:core");
importMapping.put("bool", "dart:core");
importMapping.put("num", "dart:core");
importMapping.put("int", "dart:core");
importMapping.put("double", "dart:core");
importMapping.put("List", "dart:core");
importMapping.put("Map", "dart:core");
importMapping.put("Set", "dart:core");
importMapping.put("DateTime", "dart:core");

cliOptions.add(new CliOption(PUB_LIBRARY, "Library name in generated code"));
cliOptions.add(new CliOption(PUB_NAME, "Name in generated pubspec"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package org.openapitools.codegen.languages;

import java.util.HashMap;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
Expand All @@ -32,11 +31,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.*;

import io.swagger.v3.oas.models.media.Schema;

Expand All @@ -51,19 +46,10 @@ public class DartDioClientCodegen extends DartClientCodegen {

private static final String IS_FORMAT_JSON = "jsonFormat";
private static final String CLIENT_NAME = "clientName";
private static final Set<String> modelToIgnore = new HashSet<>();

static {
modelToIgnore.add("datetime");
modelToIgnore.add("map");
modelToIgnore.add("object");
modelToIgnore.add("list");
modelToIgnore.add("file");
modelToIgnore.add("uint8list");
}

private boolean nullableFields = true;
private String dateLibrary = "core";
private static final Set<String> reservedBuiltValueWords = Sets.newHashSet("EnumClass");

public DartDioClientCodegen() {
super();
Expand Down Expand Up @@ -116,6 +102,11 @@ public String getHelp() {
return "Generates a Dart Dio client library.";
}

@Override
protected boolean isReservedWord(String word) {
return super.isReservedWord(word) || reservedBuiltValueWords.contains(word);
}

@Override
public String toDefaultValue(Schema schema) {
if (ModelUtils.isMapSchema(schema)) {
Expand Down Expand Up @@ -270,12 +261,13 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
Set<String> modelImports = new HashSet<>();
CodegenModel cm = (CodegenModel) mo.get("model");
for (String modelImport : cm.imports) {
if (importMapping.containsKey(modelImport)) {
modelImports.add(importMapping.get(modelImport));
} else {
if (!modelToIgnore.contains(modelImport.toLowerCase(Locale.ROOT))) {
modelImports.add("package:" + pubName + "/model/" + underscore(modelImport) + ".dart");
if (importMapping().containsKey(modelImport)) {
final String value = importMapping().get(modelImport);
if (!Objects.equals(value, "dart:core")) {
modelImports.add(value);
}
} else {
modelImports.add("package:" + pubName + "/model/" + underscore(modelImport) + ".dart");
}
}

Expand Down Expand Up @@ -362,10 +354,13 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o

Set<String> imports = new HashSet<>();
for (String item : op.imports) {
if (!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
if (importMapping().containsKey(item)) {
final String value = importMapping().get(item);
if (!Objects.equals(value, "dart:core")) {
fullImports.add(value);
}
} else {
imports.add(underscore(item));
} else if (item.equalsIgnoreCase("Uint8List")) {
fullImports.add("dart:typed_data");
}
}
modelImports.addAll(imports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void simpleModelTest() {
.addProperties("name", new StringSchema())
.addProperties("createdAt", new DateTimeSchema())
.addProperties("defaultItem", new IntegerSchema()._default(1))
.addProperties("number", new NumberSchema())
.addProperties("decimal", new StringSchema().format("number"))
.addRequiredItem("id")
.addRequiredItem("name");
final DefaultCodegen codegen = new DartClientCodegen();
Expand All @@ -47,9 +49,7 @@ public void simpleModelTest() {
Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.description, "a sample model");
Assert.assertEquals(cm.vars.size(), 4);
// {{imports}} is not used in template
//Assert.assertEquals(cm.imports.size(), 1);
Assert.assertEquals(cm.vars.size(), 6);

final CodegenProperty property1 = cm.vars.get(0);
Assert.assertEquals(property1.baseName, "id");
Expand Down Expand Up @@ -88,6 +88,16 @@ public void simpleModelTest() {
Assert.assertEquals(property4.baseType, "int");
Assert.assertFalse(property4.required);
Assert.assertFalse(property4.isContainer);

final CodegenProperty property5 = cm.vars.get(4);
Assert.assertEquals(property5.baseName, "number");
Assert.assertEquals(property5.dataType, "num");
Assert.assertEquals(property5.baseType, "num");

final CodegenProperty property6 = cm.vars.get(5);
Assert.assertEquals(property6.baseName, "decimal");
Assert.assertEquals(property6.dataType, "double");
Assert.assertEquals(property6.baseType, "double");
}

@Test(description = "convert a model with list property")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ doc/DefaultApi.md
doc/Dog.md
doc/DogAllOf.md
doc/EnumArrays.md
doc/EnumClass.md
doc/EnumTest.md
doc/FakeApi.md
doc/FakeClassnameTags123Api.md
Expand All @@ -35,10 +34,10 @@ doc/InlineObject3.md
doc/InlineObject4.md
doc/InlineObject5.md
doc/InlineResponseDefault.md
doc/List.md
doc/MapTest.md
doc/MixedPropertiesAndAdditionalPropertiesClass.md
doc/Model200Response.md
doc/ModelEnumClass.md
doc/ModelReturn.md
doc/Name.md
doc/NullableClass.md
Expand Down Expand Up @@ -85,7 +84,6 @@ lib/model/client.dart
lib/model/dog.dart
lib/model/dog_all_of.dart
lib/model/enum_arrays.dart
lib/model/enum_class.dart
lib/model/enum_test.dart
lib/model/file.dart
lib/model/file_schema_test_class.dart
Expand All @@ -100,10 +98,10 @@ lib/model/inline_object3.dart
lib/model/inline_object4.dart
lib/model/inline_object5.dart
lib/model/inline_response_default.dart
lib/model/list.dart
lib/model/map_test.dart
lib/model/mixed_properties_and_additional_properties_class.dart
lib/model/model200_response.dart
lib/model/model_enum_class.dart
lib/model/model_return.dart
lib/model/name.dart
lib/model/nullable_class.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ Class | Method | HTTP request | Description
- [Dog](doc//Dog.md)
- [DogAllOf](doc//DogAllOf.md)
- [EnumArrays](doc//EnumArrays.md)
- [EnumClass](doc//EnumClass.md)
- [EnumTest](doc//EnumTest.md)
- [File](doc//File.md)
- [FileSchemaTestClass](doc//FileSchemaTestClass.md)
Expand All @@ -131,10 +130,10 @@ Class | Method | HTTP request | Description
- [InlineObject4](doc//InlineObject4.md)
- [InlineObject5](doc//InlineObject5.md)
- [InlineResponseDefault](doc//InlineResponseDefault.md)
- [List](doc//List.md)
- [MapTest](doc//MapTest.md)
- [MixedPropertiesAndAdditionalPropertiesClass](doc//MixedPropertiesAndAdditionalPropertiesClass.md)
- [Model200Response](doc//Model200Response.md)
- [ModelEnumClass](doc//ModelEnumClass.md)
- [ModelReturn](doc//ModelReturn.md)
- [Name](doc//Name.md)
- [NullableClass](doc//NullableClass.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Name | Type | Description | Notes
**number** | **num** | | [default to null]
**float** | **double** | | [optional] [default to null]
**double** | **double** | | [optional] [default to null]
**decimal** | [**Decimal**](Decimal.md) | | [optional] [default to null]
**decimal** | **double** | | [optional] [default to null]
**string** | **String** | | [optional] [default to null]
**byte** | **String** | | [default to null]
**binary** | [**Uint8List**](Uint8List.md) | | [optional] [default to null]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# openapi.model.EnumClass
# openapi.model.ModelEnumClass

## Load the model package
```dart
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:typed_data';
import 'package:openapi/model/decimal.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

Expand Down Expand Up @@ -34,7 +33,7 @@ abstract class FormatTest implements Built<FormatTest, FormatTestBuilder> {

@nullable
@BuiltValueField(wireName: r'decimal')
Decimal get decimal;
double get decimal;

@nullable
@BuiltValueField(wireName: r'string')
Expand Down

This file was deleted.

Loading

0 comments on commit 7fae4ab

Please sign in to comment.