Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !8198

[dart][dart-dio] Prevent name clashes with existing dart types

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Peter Leibiger requested to merge github/fork/kuhnroyal/dart/type-conflicts into master 4 years ago
  • Overview 0
  • Commits 3
  • Pipelines 0
  • Changes 53

Can not use dart import aliases for now as this is not supported by built_value. This means we need to add potentially clashing names/classes to an additionalReservedWords exclusion list. Starting with a basic list of some http/io classes.

Correctly use importMapping and defaultIncludes this time around. Improve reserved word checking.

This now successfully generates ModelList, ModelFile and ModelClient models which previously were not generated at all or were wrong types.

Also add some more test cases for correct model name generation.

This is a huge step as this fixes some of the last remaining problems in the dart-dio generator mentioned here: https://github.com/OpenAPITools/openapi-generator/issues/8179

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

CC @swipesight (2018/09) @jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12)

Compare
  • master (base)

and
  • latest version
    f2ef1c45
    3 commits, 2 years ago

53 files
+ 586
- 244

    Preferences

    File browser
    Compare changes
docs/ge‎nerators‎
dart-‎dio.md‎ +1 -11
dart-ja‎guar.md‎ +1 -11
dar‎t.md‎ +1 -11
modules/openap‎i-generator/src‎
main/java/org/…/‎codegen/languages‎
DartClientC‎odegen.java‎ +40 -22
DartDioClien‎tCodegen.java‎ +25 -31
test/java/org/ope‎napitools/codegen‎
da‎rt‎
DartModel‎Test.java‎ +6 -1
dar‎tdio‎
DartDioMod‎elTest.java‎ +29 -2
samples/openapi3‎/client/petstore‎
dart-dio/petstor‎e_client_lib_fake‎
.openapi-‎generator‎
FI‎LES‎ +6 -4
d‎oc‎
AnotherF‎akeApi.md‎ +5 -5
FakeA‎pi.md‎ +5 -5
FakeClassname‎Tags123Api.md‎ +5 -5
FileSchemaT‎estClass.md‎ +2 -2
ModelCl‎ient.md‎ +1 -1
ModelF‎ile.md‎ +1 -1
ModelL‎ist.md‎ +15 -0
l‎ib‎
a‎pi‎
another_fa‎ke_api.dart‎ +9 -9
fake_a‎pi.dart‎ +9 -9
fake_classname_‎tags123_api.dart‎ +9 -9
mo‎del‎
file_schema_t‎est_class.dart‎ +3 -3
model_cl‎ient.dart‎ +18 -0
model_f‎ile.dart‎ +5 -5
model_l‎ist.dart‎ +18 -0
serializ‎ers.dart‎ +15 -10
te‎st‎
model_clien‎t_test.dart‎ +4 -4
model_file‎_test.dart‎ +4 -4
model_list‎_test.dart‎ +17 -0
READ‎ME.md‎ +5 -4
da‎rt2‎
petstore_cl‎ient_lib/doc‎
InlineOb‎ject1.md‎ +1 -1
petstore_cli‎ent_lib_fake‎
.openapi-‎generator‎
FI‎LES‎ +6 -4
d‎oc‎
AnotherF‎akeApi.md‎ +5 -5
FakeA‎pi.md‎ +5 -5
FakeClassname‎Tags123Api.md‎ +5 -5
FileSchemaT‎estClass.md‎ +2 -2
Format‎Test.md‎ +1 -1
InlineOb‎ject1.md‎ +1 -1
InlineOb‎ject3.md‎ +1 -1
InlineOb‎ject5.md‎ +1 -1
ModelCl‎ient.md‎ +1 -1
ModelF‎ile.md‎ +1 -1
ModelL‎ist.md‎ +15 -0
l‎ib‎
a‎pi‎
another_fa‎ke_api.dart‎ +9 -9
fake_a‎pi.dart‎ +9 -9
fake_classname_‎tags123_api.dart‎ +9 -9
mo‎del‎
file_schema_t‎est_class.dart‎ +4 -4
model_cl‎ient.dart‎ +71 -0
model_f‎ile.dart‎ +72 -0
model_l‎ist.dart‎ +71 -0
api.‎dart‎ +3 -2
api_cli‎ent.dart‎ +6 -4
te‎st‎
model_clien‎t_test.dart‎ +3 -3
model_file‎_test.dart‎ +3 -3
model_list‎_test.dart‎ +17 -0
READ‎ME.md‎ +5 -4
docs/generators/dart-dio.md
+ 1
- 11
  • View file @ f2ef1c45

  • Edit in single-file editor

  • Open in Web IDE


@@ -33,19 +33,8 @@ 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
@@ -62,6 +51,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>String</li>
<li>bool</li>
<li>double</li>
<li>dynamic</li>
<li>int</li>
<li>num</li>
</ul>
docs/generators/dart-jaguar.md
+ 1
- 11
  • View file @ f2ef1c45

  • Edit in single-file editor

  • Open in Web IDE


@@ -30,17 +30,6 @@ 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
@@ -57,6 +46,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>String</li>
<li>bool</li>
<li>double</li>
<li>dynamic</li>
<li>int</li>
<li>num</li>
</ul>
docs/generators/dart.md
+ 1
- 11
  • View file @ f2ef1c45

  • Edit in single-file editor

  • Open in Web IDE


@@ -28,17 +28,6 @@ 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
@@ -55,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>String</li>
<li>bool</li>
<li>double</li>
<li>dynamic</li>
<li>int</li>
<li>num</li>
</ul>
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartClientCodegen.java
+ 40
- 22
  • View file @ f2ef1c45

  • Edit in single-file editor

  • Open in Web IDE


@@ -73,6 +73,10 @@ public class DartClientCodegen extends DefaultCodegen {
protected String apiTestPath = "test" + File.separator;
protected String modelTestPath = "test" + File.separator;
// Names that must not be used as model names because they clash with existing
// default imports (dart:io, dart:async, package:http etc.) but are not basic dataTypes.
protected Set<String> additionalReservedWords;
public DartClientCodegen() {
super();
@@ -131,7 +135,8 @@ public class DartClientCodegen extends DefaultCodegen {
"bool",
"int",
"num",
"double"
"double",
"dynamic"
);
instantiationTypes.put("array", "List");
instantiationTypes.put("map", "Map");
@@ -155,7 +160,7 @@ public class DartClientCodegen extends DefaultCodegen {
typeMapping.put("Date", "DateTime");
typeMapping.put("date", "DateTime");
typeMapping.put("DateTime", "DateTime");
typeMapping.put("File", "MultipartFile");
typeMapping.put("file", "MultipartFile");
typeMapping.put("binary", "MultipartFile");
typeMapping.put("UUID", "String");
typeMapping.put("URI", "String");
@@ -163,21 +168,29 @@ public class DartClientCodegen extends DefaultCodegen {
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
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");
defaultIncludes = new HashSet<>(Collections.singletonList("dart:core"));
// DataTypes of the above values which are automatically imported.
// They are also not allowed to be model names.
defaultIncludes = Sets.newHashSet(
"String",
"bool",
"int",
"num",
"double",
"dynamic",
"List",
"Set",
"Map",
"DateTime",
"Object",
"MultipartFile"
);
additionalReservedWords = Sets.newHashSet(
"File",
"Client",
"Future",
"Response"
);
cliOptions.add(new CliOption(PUB_LIBRARY, "Library name in generated code"));
cliOptions.add(new CliOption(PUB_NAME, "Name in generated pubspec"));
@@ -306,6 +319,15 @@ public class DartClientCodegen extends DefaultCodegen {
supportingFiles.add(new SupportingFile("travis.mustache", "", ".travis.yml"));
}
@Override
protected boolean isReservedWord(String word) {
// consider everything as reserved that is either a keyword,
// a default included type, or a type include through some library
return super.isReservedWord(word) ||
defaultIncludes().contains(word) ||
additionalReservedWords.contains(word);
}
@Override
public String escapeReservedWord(String name) {
return name + "_";
@@ -370,7 +392,7 @@ public class DartClientCodegen extends DefaultCodegen {
name = "n" + name;
}
if (isReservedWord(name) || importMapping().containsKey(name)) {
if (isReservedWord(name)) {
name = escapeReservedWord(name);
}
@@ -385,10 +407,6 @@ public class DartClientCodegen extends DefaultCodegen {
@Override
public String toModelName(final String name) {
if (importMapping().containsKey(name)) {
return name;
}
String nameWithPrefixSuffix = sanitizeName(name);
if (!StringUtils.isEmpty(modelNamePrefix)) {
// add '_' so that model name can be camelized correctly
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java
+ 25
- 31
  • View file @ f2ef1c45

  • Edit in single-file editor

  • Open in Web IDE


@@ -19,14 +19,9 @@ package org.openapitools.codegen.languages;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.ProcessUtils;
import org.slf4j.Logger;
@@ -35,8 +30,6 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
import io.swagger.v3.oas.models.media.Schema;
import static org.openapitools.codegen.utils.StringUtils.underscore;
public class DartDioClientCodegen extends DartClientCodegen {
@@ -50,7 +43,6 @@ public class DartDioClientCodegen extends DartClientCodegen {
private boolean nullableFields = true;
private String dateLibrary = "core";
private static final Set<String> reservedBuiltValueWords = Sets.newHashSet("EnumClass");
public DartDioClientCodegen() {
super();
@@ -76,6 +68,17 @@ public class DartDioClientCodegen extends DartClientCodegen {
typeMapping.put("object", "JsonObject");
typeMapping.put("AnyType", "JsonObject");
additionalReservedWords.addAll(Sets.newHashSet(
"EnumClass",
// The following are reserved dataTypes but can not be added to defaultIncludes
// as this would prevent them from being added to the imports.
"BuiltList",
"BuiltSet",
"BuiltMap",
"Uint8List",
"JsonObject"
));
importMapping.put("BuiltList", "package:built_collection/built_collection.dart");
importMapping.put("BuiltSet", "package:built_collection/built_collection.dart");
importMapping.put("BuiltMap", "package:built_collection/built_collection.dart");
@@ -109,11 +112,6 @@ public class DartDioClientCodegen extends DartClientCodegen {
return "Generates a Dart Dio client library.";
}
@Override
protected boolean isReservedWord(String word) {
return super.isReservedWord(word) || reservedBuiltValueWords.contains(word);
}
@Override
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
return super.addMustacheLambdas()
@@ -237,19 +235,18 @@ public class DartDioClientCodegen extends DartClientCodegen {
supportingFiles.add(new SupportingFile("auth/auth.mustache", authFolder, "auth.dart"));
if ("core".equals(dateLibrary)) {
// this option uses the same classes as normal dart generator
additionalProperties.put("core", "true");
typeMapping.put("Date", "DateTime");
typeMapping.put("date", "DateTime");
} else if ("timemachine".equals(dateLibrary)) {
additionalProperties.put("timeMachine", "true");
typeMapping.put("date", "OffsetDate");
typeMapping.put("Date", "OffsetDate");
typeMapping.put("DateTime", "OffsetDateTime");
typeMapping.put("datetime", "OffsetDateTime");
additionalReservedWords.addAll(Sets.newHashSet("OffsetDate", "OffsetDateTime"));
importMapping.put("OffsetDate", "package:time_machine/time_machine.dart");
importMapping.put("OffsetDateTime", "package:time_machine/time_machine.dart");
supportingFiles.add(new SupportingFile("local_date_serializer.mustache", libFolder, "local_date_serializer.dart"));
}
}
@@ -264,13 +261,12 @@ public class DartDioClientCodegen extends DartClientCodegen {
Set<String> modelImports = new HashSet<>();
CodegenModel cm = (CodegenModel) mo.get("model");
for (String modelImport : cm.imports) {
if (importMapping().containsKey(modelImport)) {
final String value = importMapping().get(modelImport);
if (needToImport(value)) {
modelImports.add(value);
if (needToImport(modelImport)) {
if (importMapping().containsKey(modelImport)) {
modelImports.add(importMapping().get(modelImport));
} else {
modelImports.add("package:" + pubName + "/model/" + underscore(modelImport) + ".dart");
}
} else {
modelImports.add("package:" + pubName + "/model/" + underscore(modelImport) + ".dart");
}
}
@@ -337,18 +333,16 @@ public class DartDioClientCodegen extends DartClientCodegen {
Set<String> imports = new HashSet<>();
for (String item : op.imports) {
if (importMapping().containsKey(item)) {
final String value = importMapping().get(item);
if (needToImport(value)) {
fullImports.add(value);
if (needToImport(item)) {
if (importMapping().containsKey(item) && needToImport(item)) {
fullImports.add(importMapping().get(item));
} else {
imports.add(underscore(item));
}
} else {
imports.add(underscore(item));
}
}
modelImports.addAll(imports);
op.imports = imports;
}
objs.put("modelImports", modelImports);
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
2
Client: Dart Enhancement: General
2
Client: Dart Enhancement: General
    Assign labels
  • Manage project labels

Milestone
5.0.0
5.0.0 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
1
1 participant
Peter Leibiger
Reference: OpenAPITools/openapi-generator!8198
Source branch: github/fork/kuhnroyal/dart/type-conflicts

Menu

Explore Projects Groups Snippets